rimlike/scenes/world/selection.gd
megaproxy bba1ce4334 Phase 17/18 closure: stockpile filter UI + day summary + atmospheric audio
Three-agent fan-out (gdscript-refactor x3) closing deferred polish:

- Stockpile chip filter UI: new StockpilePanel (layer 18, right-anchored,
  mirrors WorkbenchPanel). 5-priority segmented control + 21-chip 4-col
  filter grid using Item.ALL_TYPES; wildcard (empty accepted_types) shows
  all chips checked with 'All' hint, first explicit pick switches to
  explicit-list mode. Selection chain extended to pawn → workbench →
  stockpile with mutual exclusion. 12 ui.stockpile.* + 13 item.* keys.

- DaySummaryCard: layer-19 modal auto-opens at dusk→night via day_ended,
  auto-pauses sim, shows day+season header, weather row, stats grid with
  green/yellow/red tension bar, Continue dismiss + backdrop tap.
  Settings 'Show end-of-day summary' toggle persists via GameState.

- Atmospheric audio: rain ambient loop (Cozy Melodies Pack 6) on
  weather_changed rain/storm with 0.5s fade-out on clear; thunder sting
  (Magic and Spells 6) on rain→storm transition; raid warning sting
  (Sword Pack 1, 'blades drawn') on EventBus.wolf_spawned. All on SFX
  bus — inherits existing slider + suspend mute.

Contracts pre-written before fan-out: EventBus.stockpile_selected /
stockpile_deselected / wolf_spawned signals; WolfSpawner._trigger_raid
+ _on_request_wolf_spawn now emit wolf_spawned with the spawned array.

MCP runtime verified: StockpilePanel opens with 21 chips, DaySummaryCard
renders weather row + tension bar + auto-pause, rain_player.playing=true
on weather_changed(rain), all three new SFX keys in Audio.SFX_FILES.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 17:20:40 +01:00

274 lines
9.6 KiB
GDScript

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
## Currently selected workbench, or null. Mutually exclusive with _selected_pawn —
## selecting one clears the other (see _select / _select_workbench).
var _selected_workbench: Workbench = null
## Currently selected stockpile zone, or null. Mutually exclusive with the above.
var _selected_stockpile = null # StockpileZone (duck-typed to avoid circular preload)
var _camera = null # Camera2D (CameraRig) — set via bind_camera(); duck-typed to avoid circular preload
# When Designation paint mode is active this flag is raised by Designation so
# Selection does not also try to select/move on the same click.
var designation_active: bool = false
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
## Inject the CameraRig so Tab-cycle and center-on-selection can pan to the
## selected pawn's tile. Call from world.gd after selection.bind().
func bind_camera(rig) -> void:
_camera = rig
func selected() -> Pawn:
return _selected_pawn
func _unhandled_input(event: InputEvent) -> void:
# ── Keyboard: Tab → cycle pawns (Shift+Tab reverses) ───────────────────────
if event.is_action_pressed("pawn_next"):
# Read shift from the event itself (set by the OS on the key event); more
# reliable than Input.is_key_pressed(KEY_SHIFT) which doesn't reflect modifier
# state from synthetic input events (MCP test injections).
var reverse: bool = event is InputEventKey and event.shift_pressed
_cycle_pawn(-1 if reverse else 1)
get_viewport().set_input_as_handled()
return
# ── Keyboard: C / Home → center camera on selected pawn ─────────────────────
if event.is_action_pressed("center_on_selection"):
if _selected_pawn != null and _camera != null:
_camera.pan_to_tile(_selected_pawn.tile)
Audit.log("selection", "center_on_selection → %s" % _selected_pawn.tile)
get_viewport().set_input_as_handled()
return
# ── Keyboard: Escape → deselect (lowest-priority; consumed last) ─────────────
# Designation._input handles Escape first; panels handle it in _unhandled_input
# before reaching here. If we still see it and have a selection, consume it.
if event.is_action_pressed("cancel"):
if _selected_pawn != null:
_deselect()
get_viewport().set_input_as_handled()
Audit.log("selection", "escape: deselected pawn")
return
if _selected_workbench != null:
_deselect_workbench()
get_viewport().set_input_as_handled()
Audit.log("selection", "escape: deselected workbench")
return
if _selected_stockpile != null:
_deselect_stockpile()
get_viewport().set_input_as_handled()
Audit.log("selection", "escape: deselected stockpile")
return
# ── Mouse: only handle button events below ───────────────────────────────────
if not (event is InputEventMouseButton):
return
# ── Right-click: cancel designation (if active) or deselect pawn / workbench / stockpile ──
if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
# Designation cancellation is handled by Designation._input; if we see
# this right-click, no designation was active. Deselect whatever is selected.
if _selected_pawn != null:
_deselect()
get_viewport().set_input_as_handled()
elif _selected_workbench != null:
_deselect_workbench()
get_viewport().set_input_as_handled()
elif _selected_stockpile != null:
_deselect_stockpile()
get_viewport().set_input_as_handled()
return
if event.button_index != MOUSE_BUTTON_LEFT:
return
# Designation paint mode owns input while active; Selection steps aside.
if designation_active:
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. Pawn wins over workbench when they share a tile
# (a pawn working at a bench is selectable; tap empty bench tile to inspect bills).
var hit_pawn: Pawn = World.pawn_at_tile(tile)
if hit_pawn != null:
_select(hit_pawn)
return
# Click on a workbench → open the bill-editor panel.
var hit_workbench = World.workbench_at_tile(tile)
if hit_workbench != null:
_select_workbench(hit_workbench)
return
# Click on a stockpile zone → open the filter editor panel.
var hit_stockpile = World.stockpile_at_tile(tile)
if hit_stockpile != null:
_select_stockpile(hit_stockpile)
return
# Empty tile with no current pawn selection → also clear any workbench / stockpile selection.
if _selected_pawn == null:
if _selected_workbench != null:
_deselect_workbench()
if _selected_stockpile != null:
_deselect_stockpile()
return
# Empty walkable tile with a selection → queue a forced job. Decision picks
# it up on the next sim tick (preempts whatever RestProvider had assigned).
if not _pathfinder.is_walkable(tile):
Audit.log("selection", "destination %s not walkable" % tile)
return
var go_job := Job.new()
go_job.label = "Go to %s" % tile
go_job.toils.append(Toil.walk_to(tile))
go_job.toils.append(Toil.idle())
_selected_pawn.forced_job = go_job
Audit.log("selection", "forced %s%s" % [_selected_pawn.pawn_name, tile])
func _select(pawn: Pawn) -> void:
if _selected_pawn == pawn:
return
# Mutual exclusion with workbench and stockpile: clear them before promoting pawn.
if _selected_workbench != null:
_deselect_workbench()
if _selected_stockpile != null:
_deselect_stockpile()
if _selected_pawn != null:
_selected_pawn.set_selected(false)
EventBus.pawn_deselected.emit()
_selected_pawn = pawn
pawn.set_selected(true)
EventBus.pawn_selected.emit(pawn)
Audit.log("selection", "selected %s at %s" % [pawn.pawn_name, pawn.tile])
## Clear the current selection without selecting another pawn.
func _deselect() -> void:
if _selected_pawn == null:
return
_selected_pawn.set_selected(false)
EventBus.pawn_deselected.emit()
Audit.log("selection", "deselected %s" % _selected_pawn.pawn_name)
_selected_pawn = null
## Select a workbench → opens the bill-editor panel via EventBus.
## Mutually exclusive with pawn and stockpile selections.
func _select_workbench(wb) -> void:
if _selected_workbench == wb:
return
if _selected_pawn != null:
_deselect()
if _selected_stockpile != null:
_deselect_stockpile()
if _selected_workbench != null:
EventBus.workbench_deselected.emit()
_selected_workbench = wb
EventBus.workbench_selected.emit(wb)
Audit.log("selection", "selected workbench %s at %s" % [wb.label_text, wb.tile])
func _deselect_workbench() -> void:
if _selected_workbench == null:
return
Audit.log("selection", "deselected workbench %s" % _selected_workbench.label_text)
_selected_workbench = null
EventBus.workbench_deselected.emit()
## Select a stockpile zone → opens the filter editor panel via EventBus.
## Mutually exclusive with pawn and workbench selections.
func _select_stockpile(zone) -> void:
if _selected_stockpile == zone:
return
if _selected_pawn != null:
_deselect()
if _selected_workbench != null:
_deselect_workbench()
if _selected_stockpile != null:
EventBus.stockpile_deselected.emit()
_selected_stockpile = zone
EventBus.stockpile_selected.emit(zone)
Audit.log("selection", "selected stockpile at %s" % str(zone.position))
func _deselect_stockpile() -> void:
if _selected_stockpile == null:
return
Audit.log("selection", "deselected stockpile")
_selected_stockpile = null
EventBus.stockpile_deselected.emit()
## Cycle the selection forward (dir=1) or backward (dir=-1) through World.pawns.
## Wraps around. If no pawn currently selected, picks World.pawns[0].
## Pans the camera to the newly selected pawn's tile.
func _cycle_pawn(dir: int) -> void:
var pawns: Array = World.pawns
if pawns.is_empty():
return
var next_pawn: Pawn
if _selected_pawn == null:
next_pawn = pawns[0]
else:
var idx: int = pawns.find(_selected_pawn)
if idx == -1:
next_pawn = pawns[0]
else:
idx = posmod(idx + dir, pawns.size())
next_pawn = pawns[idx]
_select(next_pawn)
if _camera != null:
_camera.pan_to_tile(next_pawn.tile)
Audit.log("selection", "pawn_cycle dir=%d%s" % [dir, next_pawn.pawn_name])