Advertisement
Guest User

Godot HexGrid

a guest
Mar 3rd, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. func _generate_grid():
  3.     var tile_index := 0
  4.     var previous_mat: int = -1
  5.     for x in range(0, grid_size):
  6.         var tile_coordinates:= Vector2.ZERO
  7.         tile_coordinates.x = x * TILE_SIZE * cos(deg_to_rad(30))
  8.         tile_coordinates.y = 0 if x % 2 == 0 else TILE_SIZE/2
  9.         for y in range(0, grid_size):
  10.             var tile = HEX_TILE.instantiate() as HexTile   
  11.             tile.set_tile_type(randi() % 4)
  12.             add_child(tile)
  13.             tile.q=x
  14.             tile.r=y
  15.             tile.translate(Vector3(tile_coordinates.x, 0, -tile_coordinates.y))
  16.             tile_coordinates.y += TILE_SIZE
  17.             set_tile(tile)
  18.             tile_index += 1
  19.  
  20. Few notes, I have stripped code a bit to the basic here.
  21. HexTile is a Node3D which basically is a hexagon mesh, it also holds q,r values for coordinates.
  22. has traversable boolean(is tile traversable or not)
  23.  And has a function set_tile_type which sets material to mesh. This will just random between 4 materials.
  24.  
  25. I also have a dictionary tiles witch holds all tiles and it is usable when pathfinding and for other stuff
  26. var tiles:Dictionary  = {}
  27. func set_tile(tile:HexTile):
  28.     var key = str(tile.q) + "," + str(tile.r)
  29.     tiles[key] = tile
  30.    
  31. func get_tile(q, r):
  32.     var key = str(q) + "," + str(r)
  33.     return tiles[key] if key in tiles else null
  34.  
  35.  
  36. #And here are some helper functions for pathfinding and getting neighbouring tiles
  37.  
  38.  
  39. ##Finds a traversable path between 2 tiles
  40. func bfs_pathfinding(start : HexTile, goal : HexTile, grid : Dictionary = tiles) -> Array[HexTile]:
  41.      
  42.     var queue: Array[HexTile] = []
  43.     if !start or !goal: return queue
  44.     queue.append(start)
  45.     var visited = {}
  46.     visited[start] = null
  47.     while queue.size() > 0:
  48.         var current = queue.pop_front()
  49.         if current.q == goal.q and current.r == goal.r:
  50.             var result:Array[HexTile] = _reconstruct_path(visited, start, goal)
  51.             result.append(goal)
  52.             return result
  53.        
  54.         for neighbor in _get_neighbors(current, grid, true):
  55.             if not visited.has(neighbor):
  56.                 queue.append(neighbor)
  57.                 visited[neighbor] = current
  58.    
  59.     return [] # Return an empty array if no path is found
  60.  
  61. ## returns neighbor tiles, set traversable to return only traversable neighbors
  62. func _get_neighbors(tile : HexTile, grid : Dictionary = tiles, traversable: bool = true) -> Array[HexTile]:
  63.     var neighbors:Array[HexTile] = []
  64.     var directions = [
  65.         Vector2(1, 0), Vector2(1, -1), Vector2(0, -1),
  66.         Vector2(-1, 0), Vector2(-1, 1), Vector2(0, 1)
  67.     ]
  68.     if tile.q % 2 == 1:
  69.         directions[1] = Vector2(1, 0)
  70.         directions[0] = Vector2(1, 1)  
  71.     else:
  72.         directions[3] = Vector2(-1, -1)
  73.         directions[4] = Vector2(-1, 0)
  74.     for direction in directions:
  75.         var neighbor_q = tile.q + direction.x
  76.         var neighbor_r = tile.r + direction.y  
  77.         var neighbor_key = str(neighbor_q) + "," + str(neighbor_r)
  78.         if grid.has(neighbor_key) and (grid[neighbor_key].traversable or !traversable):
  79.             neighbors.append(grid[neighbor_key])
  80.  
  81.     return neighbors
  82.  
  83. func _reconstruct_path(visited, start : HexTile, goal : HexTile) -> Array[HexTile]:
  84.     var path:Array[HexTile] = []
  85.     var current = goal
  86.     while current != start:
  87.         current = visited[current]
  88.         path.append(current)
  89.     path.reverse()
  90.     return path
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement