Chaos_Kitchen/Scripts/NavigationTiles.gd

39 lines
1 KiB
GDScript3
Raw Permalink Normal View History

2024-02-17 12:42:08 +01:00
extends TileMap
var astar = AStarGrid2D.new()
var map_rect = Rect2i()
const main_layer = 0
# Called when the node enters the scene tree for the first time.
func _ready():
var tile_size = get_tileset().tile_size
var tilemap_size = get_used_rect().end - get_used_rect().position
map_rect = Rect2i(Vector2i(), tilemap_size)
astar.region = map_rect
astar.cell_size = tile_size
astar.offset = tile_size * 0.5
astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar.update()
for cell in get_used_cells(main_layer):
astar.set_point_solid(cell, is_spot_solid(main_layer,cell))
for cell in get_used_cells(2):
astar.set_point_solid(cell, is_spot_solid(2,cell))
func is_spot_solid(layer : int,spot : Vector2i) -> bool :
return get_cell_tile_data(layer,spot).get_custom_data("is_solid")
func is_spot_walkable(spot) -> bool :
var map_pos = local_to_map(spot)
if map_rect.has_point(map_pos):
return not astar.is_point_solid(map_pos)
return false