pathfind_astar.gd 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. extends TileMap
  2. const BASE_LINE_WIDTH = 3.0
  3. const DRAW_COLOR = Color.white
  4. # The Tilemap node doesn't have clear bounds so we're defining the map's limits here.
  5. export(Vector2) var map_size = Vector2.ONE * 16
  6. # The path start and end variables use setter methods.
  7. # You can find them at the bottom of the script.
  8. var path_start_position = Vector2() setget _set_path_start_position
  9. var path_end_position = Vector2() setget _set_path_end_position
  10. var _point_path = []
  11. # You can only create an AStar node from code, not from the Scene tab.
  12. onready var astar_node = AStar.new()
  13. # get_used_cells_by_id is a method from the TileMap node.
  14. # Here the id 0 corresponds to the grey tile, the obstacles.
  15. onready var obstacles = get_used_cells_by_id(0)
  16. onready var _half_cell_size = cell_size / 2
  17. func _ready():
  18. var walkable_cells_list = astar_add_walkable_cells(obstacles)
  19. astar_connect_walkable_cells(walkable_cells_list)
  20. func _draw():
  21. if not _point_path:
  22. return
  23. var point_start = _point_path[0]
  24. var point_end = _point_path[len(_point_path) - 1]
  25. set_cell(point_start.x, point_start.y, 1)
  26. set_cell(point_end.x, point_end.y, 2)
  27. var last_point = map_to_world(Vector2(point_start.x, point_start.y)) + _half_cell_size
  28. for index in range(1, len(_point_path)):
  29. var current_point = map_to_world(Vector2(_point_path[index].x, _point_path[index].y)) + _half_cell_size
  30. draw_line(last_point, current_point, DRAW_COLOR, BASE_LINE_WIDTH, true)
  31. draw_circle(current_point, BASE_LINE_WIDTH * 2.0, DRAW_COLOR)
  32. last_point = current_point
  33. # Loops through all cells within the map's bounds and
  34. # adds all points to the astar_node, except the obstacles.
  35. func astar_add_walkable_cells(obstacle_list = []):
  36. var points_array = []
  37. for y in range(map_size.y):
  38. for x in range(map_size.x):
  39. var point = Vector2(x, y)
  40. if point in obstacle_list:
  41. continue
  42. points_array.append(point)
  43. # The AStar class references points with indices.
  44. # Using a function to calculate the index from a point's coordinates
  45. # ensures we always get the same index with the same input point.
  46. var point_index = calculate_point_index(point)
  47. # AStar works for both 2d and 3d, so we have to convert the point
  48. # coordinates from and to Vector3s.
  49. astar_node.add_point(point_index, Vector3(point.x, point.y, 0.0))
  50. return points_array
  51. # Once you added all points to the AStar node, you've got to connect them.
  52. # The points don't have to be on a grid: you can use this class
  53. # to create walkable graphs however you'd like.
  54. # It's a little harder to code at first, but works for 2d, 3d,
  55. # orthogonal grids, hex grids, tower defense games...
  56. func astar_connect_walkable_cells(points_array):
  57. for point in points_array:
  58. var point_index = calculate_point_index(point)
  59. # For every cell in the map, we check the one to the top, right.
  60. # left and bottom of it. If it's in the map and not an obstalce.
  61. # We connect the current point with it.
  62. var points_relative = PoolVector2Array([
  63. point + Vector2.RIGHT,
  64. point + Vector2.LEFT,
  65. point + Vector2.DOWN,
  66. point + Vector2.UP,
  67. ])
  68. for point_relative in points_relative:
  69. var point_relative_index = calculate_point_index(point_relative)
  70. if is_outside_map_bounds(point_relative):
  71. continue
  72. if not astar_node.has_point(point_relative_index):
  73. continue
  74. # Note the 3rd argument. It tells the astar_node that we want the
  75. # connection to be bilateral: from point A to B and B to A.
  76. # If you set this value to false, it becomes a one-way path.
  77. # As we loop through all points we can set it to false.
  78. astar_node.connect_points(point_index, point_relative_index, false)
  79. # This is a variation of the method above.
  80. # It connects cells horizontally, vertically AND diagonally.
  81. func astar_connect_walkable_cells_diagonal(points_array):
  82. for point in points_array:
  83. var point_index = calculate_point_index(point)
  84. for local_y in range(3):
  85. for local_x in range(3):
  86. var point_relative = Vector2(point.x + local_x - 1, point.y + local_y - 1)
  87. var point_relative_index = calculate_point_index(point_relative)
  88. if point_relative == point or is_outside_map_bounds(point_relative):
  89. continue
  90. if not astar_node.has_point(point_relative_index):
  91. continue
  92. astar_node.connect_points(point_index, point_relative_index, true)
  93. func calculate_point_index(point):
  94. return point.x + map_size.x * point.y
  95. func clear_previous_path_drawing():
  96. if not _point_path:
  97. return
  98. var point_start = _point_path[0]
  99. var point_end = _point_path[len(_point_path) - 1]
  100. set_cell(point_start.x, point_start.y, -1)
  101. set_cell(point_end.x, point_end.y, -1)
  102. func is_outside_map_bounds(point):
  103. return point.x < 0 or point.y < 0 or point.x >= map_size.x or point.y >= map_size.y
  104. func get_astar_path(world_start, world_end):
  105. self.path_start_position = world_to_map(world_start)
  106. self.path_end_position = world_to_map(world_end)
  107. _recalculate_path()
  108. var path_world = []
  109. for point in _point_path:
  110. var point_world = map_to_world(Vector2(point.x, point.y)) + _half_cell_size
  111. path_world.append(point_world)
  112. return path_world
  113. func _recalculate_path():
  114. clear_previous_path_drawing()
  115. var start_point_index = calculate_point_index(path_start_position)
  116. var end_point_index = calculate_point_index(path_end_position)
  117. # This method gives us an array of points. Note you need the start and
  118. # end points' indices as input.
  119. _point_path = astar_node.get_point_path(start_point_index, end_point_index)
  120. # Redraw the lines and circles from the start to the end point.
  121. update()
  122. # Setters for the start and end path values.
  123. func _set_path_start_position(value):
  124. if value in obstacles:
  125. return
  126. if is_outside_map_bounds(value):
  127. return
  128. set_cell(path_start_position.x, path_start_position.y, -1)
  129. set_cell(value.x, value.y, 1)
  130. path_start_position = value
  131. if path_end_position and path_end_position != path_start_position:
  132. _recalculate_path()
  133. func _set_path_end_position(value):
  134. if value in obstacles:
  135. return
  136. if is_outside_map_bounds(value):
  137. return
  138. set_cell(path_start_position.x, path_start_position.y, -1)
  139. set_cell(value.x, value.y, 2)
  140. path_end_position = value
  141. if path_start_position != value:
  142. _recalculate_path()