Adding pathing

This commit is contained in:
Dan 2024-06-01 19:29:26 +01:00
parent a91635a68c
commit 9f9f1a7502
5 changed files with 187 additions and 7 deletions

45
levels/scripts/map.gd Normal file
View file

@ -0,0 +1,45 @@
extends TileMap
var astar : AStarGrid2D = AStarGrid2D.new()
@onready var tilemap_size : Vector2i = get_used_rect().end - get_used_rect().position
@onready var map_rect : Rect2i = Rect2i(Vector2i.ZERO, tilemap_size)
func _ready() -> void:
setup_pathing()
pass
func setup_pathing() -> void:
# Setup the AStarGrid2D for pathfinding
var tile_size : Vector2i = get_tileset().tile_size
Log.pr("Tilemap size: " + str(tilemap_size))
astar.region = map_rect
astar.cell_size = tile_size
astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
astar.default_estimate_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar.update()
update_passable()
pass
func update_passable() -> void:
# Update the AStarGrid2D with the new passable tiles
for i in tilemap_size.x:
for j in tilemap_size.y:
var coords : Vector2i = Vector2i(i, j)
var tile_data = get_cell_tile_data(0, coords)
if tile_data and tile_data.get_custom_data('navtype') == 'wall':
astar.set_point_solid(coords)
pass
func is_point_walkable(check_position) -> bool:
var map_position = local_to_map(check_position)
if map_rect.has_point(map_position) and not astar.is_point_solid(map_position):
return true
return false