extends Node class_name Selection ## Pawn selection + click-to-move input handler. ## ## A click on a pawn selects it; a click on a walkable tile while a pawn is ## selected pathfinds + commands the walk. Drags belong to the camera (pan) ## — we discriminate clicks from drags by motion + duration thresholds. ## ## Lives as a child of World; `_unhandled_input` runs after the camera rig ## and after any CanvasLayer UI swallows its own clicks. const CLICK_MAX_DRIFT_PX: float = 8.0 const CLICK_MAX_DURATION_MS: int = 300 var _pathfinder: Pathfinder = null var _selected_pawn: Pawn = null var _press_screen_pos: Vector2 = Vector2.ZERO var _press_time_ms: int = 0 var _pressing: bool = false func bind(pathfinder: Pathfinder) -> void: assert(pathfinder != null, "Selection.bind: pathfinder is null") _pathfinder = pathfinder func selected() -> Pawn: return _selected_pawn func _unhandled_input(event: InputEvent) -> void: if not (event is InputEventMouseButton): return if event.button_index != MOUSE_BUTTON_LEFT: return if event.pressed: _press_screen_pos = event.position _press_time_ms = Time.get_ticks_msec() _pressing = true return if not _pressing: return _pressing = false var drift: float = event.position.distance_to(_press_screen_pos) var dt_ms: int = Time.get_ticks_msec() - _press_time_ms # Anything that drifted more than a few pixels or sat for more than 300 ms # is the camera's drag-pan; ignore it as a select/move action. if drift > CLICK_MAX_DRIFT_PX or dt_ms > CLICK_MAX_DURATION_MS: return _handle_click(event.position) func _handle_click(screen_pos: Vector2) -> void: if _pathfinder == null: Audit.log("selection", "click before bind() — ignored") return var world_pos: Vector2 = get_viewport().get_canvas_transform().affine_inverse() * screen_pos var tile: Vector2i = Vector2i( floori(world_pos.x / float(Pawn.TILE_SIZE_PX)), floori(world_pos.y / float(Pawn.TILE_SIZE_PX)), ) # Click on a pawn → select. var hit_pawn: Pawn = World.pawn_at_tile(tile) if hit_pawn != null: _select(hit_pawn) return # Empty tile with no current selection → no-op. if _selected_pawn == null: return # Empty walkable tile with a selection → pathfind + command move. if not _pathfinder.is_walkable(tile): Audit.log("selection", "destination %s not walkable" % tile) return var path: Array[Vector2i] = _pathfinder.find_path(_selected_pawn.tile, tile) if path.is_empty(): Audit.log("selection", "no path %s → %s" % [_selected_pawn.tile, tile]) return _selected_pawn.walk_along_path(path) func _select(pawn: Pawn) -> void: if _selected_pawn == pawn: return if _selected_pawn != null: _selected_pawn.set_selected(false) _selected_pawn = pawn pawn.set_selected(true) Audit.log("selection", "selected %s at %s" % [pawn.pawn_name, pawn.tile])