Browse Source

Update "Grid-based Navigation with AStarGrid2D" demo for 4.1 (#914)

Update "Grid-based Navigation with AStarGrid2D" demo for 4.1
Hugo Locurcio 2 years ago
parent
commit
e9f0f75c5b
2 changed files with 5 additions and 10 deletions
  1. 4 9
      2d/navigation_astar/pathfind_astar.gd
  2. 1 1
      2d/navigation_astar/project.godot

+ 4 - 9
2d/navigation_astar/pathfind_astar.gd

@@ -8,18 +8,13 @@ const DRAW_COLOR = Color.WHITE
 
 
 # The object for pathfinding on 2D grids.
 # The object for pathfinding on 2D grids.
 var _astar = AStarGrid2D.new()
 var _astar = AStarGrid2D.new()
-var _map_rect = Rect2i()
 
 
 var _start_point = Vector2i()
 var _start_point = Vector2i()
 var _end_point = Vector2i()
 var _end_point = Vector2i()
 var _path = PackedVector2Array()
 var _path = PackedVector2Array()
 
 
 func _ready():
 func _ready():
-	# Let's assume that the entire map is located at non-negative coordinates.
-	var map_size = get_used_rect().end
-	_map_rect = Rect2i(Vector2i(), map_size)
-
-	_astar.size = map_size
+	_astar.region = get_used_rect()
 	_astar.cell_size = CELL_SIZE
 	_astar.cell_size = CELL_SIZE
 	_astar.offset = CELL_SIZE * 0.5
 	_astar.offset = CELL_SIZE * 0.5
 	_astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
 	_astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
@@ -27,8 +22,8 @@ func _ready():
 	_astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
 	_astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
 	_astar.update()
 	_astar.update()
 
 
-	for i in map_size.x:
-		for j in map_size.y:
+	for i in range(_astar.region.position.x, _astar.region.end.x):
+		for j in range(_astar.region.position.y, _astar.region.end.y):
 			var pos = Vector2i(i, j)
 			var pos = Vector2i(i, j)
 			if get_cell_source_id(0, pos) == Tile.OBSTACLE:
 			if get_cell_source_id(0, pos) == Tile.OBSTACLE:
 				_astar.set_point_solid(pos)
 				_astar.set_point_solid(pos)
@@ -52,7 +47,7 @@ func round_local_position(local_position):
 
 
 func is_point_walkable(local_position):
 func is_point_walkable(local_position):
 	var map_position = local_to_map(local_position)
 	var map_position = local_to_map(local_position)
-	if _map_rect.has_point(map_position):
+	if _astar.is_in_boundsv(map_position):
 		return not _astar.is_point_solid(map_position)
 		return not _astar.is_point_solid(map_position)
 	return false
 	return false
 
 

+ 1 - 1
2d/navigation_astar/project.godot

@@ -14,7 +14,7 @@ config/name="Grid-based Pathfinding with AStarGrid2D"
 config/description="This is an example of using AStarGrid2D for navigation in 2D,
 config/description="This is an example of using AStarGrid2D for navigation in 2D,
 complete with Steering Behaviors in order to smooth the movement out."
 complete with Steering Behaviors in order to smooth the movement out."
 run/main_scene="res://game.tscn"
 run/main_scene="res://game.tscn"
-config/features=PackedStringArray("4.0")
+config/features=PackedStringArray("4.1")
 config/icon="res://icon.webp"
 config/icon="res://icon.webp"
 config/tags=PackedStringArray("2d", "ai", "demo", "official", "tilemap")
 config/tags=PackedStringArray("2d", "ai", "demo", "official", "tilemap")