46 lines
1.4 KiB
GDScript
46 lines
1.4 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 = AStarGrid2D.new()
|
|
grid.cell_size = Vector2(GroundLayer.tile_set.tile_size)
|
|
grid.region = GroundLayer.get_used_rect()
|
|
grid.update()
|
|
|
|
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:
|
|
astarGrid = AStarGrid2D.new()
|
|
astarGrid.cell_size = Vector2(GroundLayer.tile_set.tile_size)
|
|
astarGrid.region = GroundLayer.get_used_rect()
|
|
astarGrid.update()
|
|
|
|
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'):
|
|
astarGrid.set_point_solid(id)
|
|
|
|
return astarGrid
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
pass
|