class_name CombatMap extends Node2D @export var tile_set: DLTileset @export var map_layout: MapLayout @onready var tile_map: TileMapLayer = %TerrainLayer @onready var highlight_map: GridOverlay = %OverlayLayer const TILE_SIZE := 100.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() if map_layout: apply_layout(map_layout) func snap_to_grid(pos: Vector2) -> Vector2: return Vector2(floorf(pos.x / TILE_SIZE), floorf(pos.y / TILE_SIZE)) * TILE_SIZE func world_to_coords(pos: Vector2) -> Vector2i: return Vector2i(snap_to_grid(pos) / TILE_SIZE) func coords_to_world(coords: Vector2i) -> Vector2: return Vector2(coords) * TILE_SIZE func draw_wall(coords: Vector2i) -> void: draw_custom(coords, tile_set.wall_tile_coords) func draw_floor(coords: Vector2i) -> void: draw_custom(coords, tile_set.floor_tile_coords) 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 func remove_unit(unit: Unit) -> void: if unit.get_parent() == self: remove_child(unit) func target_tile(coords: Vector2i) -> void: highlight_map.target_tile(coords) func apply_layout(layout: MapLayout) -> void: map_layout = layout map_layout.initialize() load_from_layout() draw_room_walls() func is_tile_passable(from: Vector2i, to: Vector2i) -> bool: if map_layout: return map_layout.is_passable(from, to) # Fallback: no room system, use legacy wall check return not is_wall(to) func is_tile_valid(coords: Vector2i) -> bool: if map_layout: return map_layout.is_tile_valid(coords) # Fallback: no room system, any non-wall tile is valid return not is_wall(coords) func draw_room_walls() -> void: if not map_layout: return var walls := map_layout.get_walls() for wall in walls: var from_world := coords_to_world(wall[0]) + Vector2(TILE_SIZE / 2, TILE_SIZE / 2) var to_world := coords_to_world(wall[1]) + Vector2(TILE_SIZE / 2, TILE_SIZE / 2) var midpoint := (from_world + to_world) / 2 var diff := to_world - from_world var wall_dir := Vector2(-diff.y, diff.x).normalized() var half_len := TILE_SIZE / 2 var line := Line2D.new() line.add_point(midpoint - wall_dir * half_len) line.add_point(midpoint + wall_dir * half_len) line.width = 4.0 line.default_color = Color(0.6, 0.5, 0.4) add_child(line) func load_from_layout() -> void: if not map_layout: return for room in map_layout.rooms: for tile in room.tiles: draw_floor(tile)