Change how map loading works

This commit is contained in:
gamer147
2026-04-01 17:48:47 -04:00
parent 0233cb6f46
commit 470e89b15b
6 changed files with 84 additions and 56 deletions

View File

@@ -10,6 +10,17 @@ signal tile_hovered(coords: Vector2i)
const TILE_SIZE := 48.0
const SOURCE_ID: int = 0
var _pending_layout: String
var _pending_units: Array[Dictionary] = []
func _ready() -> void:
if _pending_layout:
_apply_layout(_pending_layout)
for entry in _pending_units:
_apply_deploy(entry.unit, entry.coords)
_pending_units.clear()
func snap_to_grid(pos: Vector2) -> Vector2:
return Vector2(floorf(pos.x / TILE_SIZE), floorf(pos.y / TILE_SIZE)) * TILE_SIZE
@@ -31,6 +42,37 @@ func draw_floor(coords: Vector2i) -> void:
func draw_custom(coords: Vector2i, tile_coords: Vector2i) -> void:
tile_map.set_cell(coords, SOURCE_ID, tile_coords)
func load_map(layout: String) -> void:
if is_node_ready():
_apply_layout(layout)
else:
_pending_layout = layout
func deploy_unit(unit: Unit, coords: Vector2i) -> void:
if is_node_ready():
_apply_deploy(unit, coords)
else:
_pending_units.append({unit = unit, coords = coords})
func _apply_layout(layout: String) -> void:
var rows := layout.split("\n")
for y in rows.size():
for x in rows[y].length():
var coords := Vector2i(x, y)
match rows[y][x]:
"#":
draw_wall(coords)
".":
draw_floor(coords)
func _apply_deploy(unit: Unit, coords: Vector2i) -> void:
unit.position = coords_to_world(coords)
add_child(unit)
func is_wall(coords: Vector2i) -> bool:
return tile_map.get_cell_atlas_coords(coords) == tile_set.wall_tile_coords

View File

@@ -72,9 +72,8 @@ func _select_unit(unit: Unit) -> void:
func _get_unit_at(world_pos: Vector2) -> Unit:
var snapped := dl_map.snap_to_grid(world_pos)
for child in get_parent().get_children():
if child is Unit:
var unit_snapped := dl_map.snap_to_grid(child.global_position)
if unit_snapped == snapped:
return child
for unit: Unit in get_tree().get_nodes_in_group("units"):
var unit_snapped := dl_map.snap_to_grid(unit.global_position)
if unit_snapped == snapped:
return unit
return null