Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func _generate_grid():
- var tile_index := 0
- var previous_mat: int = -1
- for x in range(0, grid_size):
- var tile_coordinates:= Vector2.ZERO
- tile_coordinates.x = x * TILE_SIZE * cos(deg_to_rad(30))
- tile_coordinates.y = 0 if x % 2 == 0 else TILE_SIZE/2
- for y in range(0, grid_size):
- var tile = HEX_TILE.instantiate() as HexTile
- tile.set_tile_type(randi() % 4)
- add_child(tile)
- tile.q=x
- tile.r=y
- tile.translate(Vector3(tile_coordinates.x, 0, -tile_coordinates.y))
- tile_coordinates.y += TILE_SIZE
- set_tile(tile)
- tile_index += 1
- Few notes, I have stripped code a bit to the basic here.
- HexTile is a Node3D which basically is a hexagon mesh, it also holds q,r values for coordinates.
- has traversable boolean(is tile traversable or not)
- And has a function set_tile_type which sets material to mesh. This will just random between 4 materials.
- I also have a dictionary tiles witch holds all tiles and it is usable when pathfinding and for other stuff
- var tiles:Dictionary = {}
- func set_tile(tile:HexTile):
- var key = str(tile.q) + "," + str(tile.r)
- tiles[key] = tile
- func get_tile(q, r):
- var key = str(q) + "," + str(r)
- return tiles[key] if key in tiles else null
- #And here are some helper functions for pathfinding and getting neighbouring tiles
- ##Finds a traversable path between 2 tiles
- func bfs_pathfinding(start : HexTile, goal : HexTile, grid : Dictionary = tiles) -> Array[HexTile]:
- var queue: Array[HexTile] = []
- if !start or !goal: return queue
- queue.append(start)
- var visited = {}
- visited[start] = null
- while queue.size() > 0:
- var current = queue.pop_front()
- if current.q == goal.q and current.r == goal.r:
- var result:Array[HexTile] = _reconstruct_path(visited, start, goal)
- result.append(goal)
- return result
- for neighbor in _get_neighbors(current, grid, true):
- if not visited.has(neighbor):
- queue.append(neighbor)
- visited[neighbor] = current
- return [] # Return an empty array if no path is found
- ## returns neighbor tiles, set traversable to return only traversable neighbors
- func _get_neighbors(tile : HexTile, grid : Dictionary = tiles, traversable: bool = true) -> Array[HexTile]:
- var neighbors:Array[HexTile] = []
- var directions = [
- Vector2(1, 0), Vector2(1, -1), Vector2(0, -1),
- Vector2(-1, 0), Vector2(-1, 1), Vector2(0, 1)
- ]
- if tile.q % 2 == 1:
- directions[1] = Vector2(1, 0)
- directions[0] = Vector2(1, 1)
- else:
- directions[3] = Vector2(-1, -1)
- directions[4] = Vector2(-1, 0)
- for direction in directions:
- var neighbor_q = tile.q + direction.x
- var neighbor_r = tile.r + direction.y
- var neighbor_key = str(neighbor_q) + "," + str(neighbor_r)
- if grid.has(neighbor_key) and (grid[neighbor_key].traversable or !traversable):
- neighbors.append(grid[neighbor_key])
- return neighbors
- func _reconstruct_path(visited, start : HexTile, goal : HexTile) -> Array[HexTile]:
- var path:Array[HexTile] = []
- var current = goal
- while current != start:
- current = visited[current]
- path.append(current)
- path.reverse()
- return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement