Files
turnbasedstrategygame/Scenes/Map/map_new.gd
2025-12-26 04:10:39 +01:00

54 lines
1.6 KiB
GDScript

extends Node2D
@onready var GroundLayer: TileMapLayer = $Ground
@onready var ObstacleLayer: TileMapLayer = $Obstacles
# Maybe a SubClass that is a Singleton, so we can create one grid
# and then change it per Map
var astarGrid: AStarGrid2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#astarGrid = generateSimpleGrid()
astarGrid = generateObstacleGrid()
%GridDisplay.grid = astarGrid
func generateSimpleGrid() -> AStarGrid2D:
var grid = newGrid()
for id in GroundLayer.get_used_cells():
var data: TileData = GroundLayer.get_cell_tile_data(id)
if data and data.get_custom_data('obstacle'):
grid.set_point_solid(id)
return grid
func generateObstacleGrid() -> AStarGrid2D:
var grid = newGrid()
var layers = ObstacleLayer.get_children()
for layer in layers:
for id in layer.get_used_cells():
var data: TileData = layer.get_cell_tile_data(id)
if data and data.get_custom_data('obstacle'):
grid.set_point_solid(id)
return grid
func newGrid() -> AStarGrid2D:
var _astarGrid = AStarGrid2D.new()
_astarGrid.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
_astarGrid.default_estimate_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
_astarGrid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES
_astarGrid.cell_size = Vector2(GroundLayer.tile_set.tile_size)
_astarGrid.region = GroundLayer.get_used_rect()
_astarGrid.update()
return _astarGrid
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func getGrid():
return astarGrid