53 lines
1.5 KiB
GDScript
53 lines
1.5 KiB
GDScript
extends TileMap
|
|
class_name CustomTileMap
|
|
|
|
var astar : AStarGrid2D = AStarGrid2D.new()
|
|
@onready var fog_overlay : ColorRect = $FogOverlay
|
|
|
|
@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:
|
|
|
|
var tile_size : Vector2i = get_tileset().tile_size
|
|
var map_pixel_size : Vector2i = tilemap_size * tile_size
|
|
fog_overlay.size = map_pixel_size
|
|
|
|
|
|
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 : TileData = 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 : Vector2i) -> bool:
|
|
var map_position : Vector2i = local_to_map(check_position)
|
|
if map_rect.has_point(map_position) and not astar.is_point_solid(map_position):
|
|
return true
|
|
|
|
return false
|