rimlike/scenes/ui/resume_toast.gd
megaproxy 19d28ca9f8 Phase 16: Save/load full coverage + autosave + UI
Three-agent fan-out reusing the contracts-first pattern: Opus pre-wrote
World.clear_all + 4 EventBus signals (save_started/finished, load_started/
finished) before dispatch. Pattern proven across Phases 12/13/14/15/16.

Entity to_dict/from_dict + class_id tagging (Agent A):
- class_id tag added to all 18 entity to_dict methods for loader routing
- Missing pairs filled in: wolf, grave_slot, graveyard_zone, stockpile_zone,
  crate (from_dict). All defensive with d.get(field, default).
- Workbench round-trips label_text so Carpenter/Smelter/Millstone/Hearth/
  Pyre kinds survive reload
- BeautySystem + DirtinessSystem save_dict/apply_dict for sparse maps
- World.save_tilemap_layers / apply_tilemap_layers covering 5 layers
  (Terrain/Floor/Wall/Designation/Roof; Fog runtime-only skipped)

SaveSystem v2 rewrite (Agent B):
- SAVE_VERSION bumped from 1 to 2
- write_save(slot) pauses Sim, emits save_started, collects every entity
  via _collect_entities iterating all World registries, writes payload to
  user://save_<slot>.json
- apply_save full rewrite: pause sim → emit load_started → World.clear_all
  → apply autoloads (GameState/Clock/Weather/Storyteller) → apply tilemap
  layers → iterate payload.entities and dispatch to per-class factories
  → apply beauty/dirt maps → emit load_finished(slot, ok, real_seconds_away)
- Per-class factory registry: 18 class_ids dispatched to setup+add_child+
  from_dict patterns. CremationPyre detected via workbench.label_text == 'Pyre'
- Public slot API: save_to_slot/load_from_slot/has_save/delete_save/
  peek_save_metadata. Slots locked: &manual + &autosave

Autosave + UI + Resume toast (Agent C):
- autoload/autosave.gd — new Autosave autoload. Periodic every
  AUTOSAVE_INTERVAL_TICKS = 6000 (~5 in-game min at 20 Hz) + NOTIFICATION_
  APPLICATION_PAUSED (mobile) + NOTIFICATION_WM_WINDOW_FOCUS_OUT (desktop).
  Gated by _busy flag tied to EventBus.save_started/save_finished.
- TopBar extended with SaveBtn (💾) + LoadBtn buttons, 48×48 min hit area
- scenes/ui/load_menu.gd — CanvasLayer slot picker. Reads peek_save_metadata
  to show 'Manual save (Date Time)' / 'Autosave (Date Time)' rows.
  Version-mismatch warning dialog before continuing on older saves.
- scenes/ui/resume_toast.gd — top-center toast. On load_finished(ok=true):
  'Welcome back — N minutes/hours away' for 5s + 0.8s fade.
  On ok=false: 'Load failed (corrupt or version mismatch)'.
- Strings catalog: 14 new keys (ui.save / ui.load / ui.welcome_back_* /
  ui.load_failed etc.)
- main.gd mounts LoadMenu + ResumeToast as runtime CanvasLayer children

MCP runtime verified:
- Saved at tick 1137 → [save] wrote slot 'manual': 113 entities at tick 1137
- Advanced sim to tick 4600 at ULTRA speed (different state)
- load_from_slot(&manual) → [save] applied slot 'manual': 113 entities,
  0 errors, tick=1137, away=34s
- post-load: Sim.tick=1137 (restored), pawns alive=3, all furniture +
  workbenches + crops + walls + floors back in place
- Resume toast fires: [resume_toast] showing — ok=true seconds_away=34
- Autosave on focus-loss verified: [autosave] focus-loss → wrote autosave
- Screenshot shows TopBar with Save + Load buttons + post-load Lone Wolf
  storyteller modal from fresh dawn roll

Known acceptable gaps (deferred to Phase 20 tuning):
- Pawn JobRunner mid-INTERACT/mid-BUILD restarts from toil 0 on reload
  (walk toil round-trips; multi-step interact does not). Pawns lose a few
  seconds of work.
- Workbench bill mid-craft fetch state isn't fully serialized.
- Wolf.target_pawn re-resolution from name string is Agent A's documented
  pattern; Agent B's apply_save respects pawn-restoration ordering so the
  resolution works after pawns are back.

Delegation: 3× gdscript-refactor (Sonnet) agents in parallel; integration
+ MCP verify on Opus.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 19:24:59 +01:00

124 lines
4.2 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name ResumeToast extends CanvasLayer
## Phase 16 — "Welcome back / Load failed" toast.
##
## Layer 22 — above Modal (20) but below LoadMenu (25).
## Listens to EventBus.load_finished.
## On ok=true: "Welcome back — away N minutes/hours" for SHOW_DURATION_SEC, then fades.
## On ok=false: "Load failed (corrupt or version mismatch)" — same fade cadence.
const SHOW_DURATION_SEC: float = 5.0
const FADE_DURATION_SEC: float = 0.8
var _root: Control = null
var _label: Label = null
var _show_timer: Timer = null
var _fade_time: float = 0.0
var _fading: bool = false
func _ready() -> void:
layer = 22
_build_ui()
_root.visible = false
EventBus.load_finished.connect(_on_load_finished)
Audit.log("resume_toast", "ResumeToast ready")
func _exit_tree() -> void:
if EventBus.load_finished.is_connected(_on_load_finished):
EventBus.load_finished.disconnect(_on_load_finished)
func _process(delta: float) -> void:
if not _fading:
return
_fade_time -= delta
if _fade_time <= 0.0:
_fading = false
_root.visible = false
_root.modulate = Color.WHITE
return
_root.modulate.a = clampf(_fade_time / FADE_DURATION_SEC, 0.0, 1.0)
# ── UI construction ───────────────────────────────────────────────────────────
func _build_ui() -> void:
# Top-center strip — sits just below the top bar.
_root = Control.new()
_root.name = "ToastRoot"
_root.set_anchors_preset(Control.PRESET_TOP_WIDE)
_root.custom_minimum_size = Vector2(0, 56)
_root.offset_top = 56 # below the 48 px top bar + a little gap
_root.offset_bottom = 112
_root.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(_root)
var panel := PanelContainer.new()
panel.name = "Panel"
panel.set_anchors_preset(Control.PRESET_CENTER_TOP)
panel.custom_minimum_size = Vector2(400, 48)
panel.offset_left = -200
panel.offset_right = 200
panel.offset_top = 0
panel.offset_bottom = 48
panel.mouse_filter = Control.MOUSE_FILTER_IGNORE
_root.add_child(panel)
_label = Label.new()
_label.name = "ToastLabel"
_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
_label.autowrap_mode = TextServer.AUTOWRAP_OFF
panel.add_child(_label)
_show_timer = Timer.new()
_show_timer.name = "ShowTimer"
_show_timer.one_shot = true
_show_timer.wait_time = SHOW_DURATION_SEC
_show_timer.timeout.connect(_start_fade)
add_child(_show_timer)
# ── event handling ────────────────────────────────────────────────────────────
func _on_load_finished(_slot: StringName, ok: bool, real_seconds_away: int) -> void:
if ok:
_label.text = _format_welcome(real_seconds_away)
else:
_label.text = Strings.t(&"ui.load_failed")
_root.modulate = Color.WHITE
_root.visible = true
_fading = false
_show_timer.start()
Audit.log("resume_toast", "showing — ok=%s seconds_away=%d" % [ok, real_seconds_away])
func _start_fade() -> void:
_fading = true
_fade_time = FADE_DURATION_SEC
# ── helpers ───────────────────────────────────────────────────────────────────
func _format_welcome(seconds_away: int) -> String:
var duration_str: String
if seconds_away < 120:
# Less than 2 minutes — show as "1 minute"
duration_str = Strings.t(&"ui.welcome_back_min").format({"n": 1})
elif seconds_away < 3600:
# Under an hour — show in minutes.
var mins: int = seconds_away / 60
var key: StringName = &"ui.welcome_back_min" if mins == 1 else &"ui.welcome_back_mins"
duration_str = Strings.t(key).format({"n": mins})
elif seconds_away < 7200:
# 12 hours exactly → singular.
duration_str = Strings.t(&"ui.welcome_back_hour").format({"n": 1})
else:
var hrs: int = seconds_away / 3600
var key: StringName = &"ui.welcome_back_hour" if hrs == 1 else &"ui.welcome_back_hours"
duration_str = Strings.t(key).format({"n": hrs})
return Strings.t(&"ui.welcome_back").format({"n": duration_str})