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>
163 lines
6 KiB
GDScript
163 lines
6 KiB
GDScript
## Tree entity — choppable by a pawn with a Chop job. Drops wood Item nodes
|
||
## when felled.
|
||
##
|
||
## Chopping model (docs/implementation.md Phase 4):
|
||
## A ChopProvider creates a Job whose INTERACT toil calls on_chop_tick() once
|
||
## per sim tick via JobRunner. After CHOP_TICKS ticks the tree is felled.
|
||
##
|
||
## World registration (World.register_tree / World.unregister_tree) is called
|
||
## here but the methods land in World during Opus integration.
|
||
|
||
class_name HarvestableTree extends Node2D
|
||
## NOTE: class_name is HarvestableTree because Godot 4 ships a built-in `Tree`
|
||
## Control node — using "Tree" would shadow that. Filename / scene name stay
|
||
## as `tree` because the game-side concept is still just "tree".
|
||
|
||
const TILE_SIZE_PX: int = 16
|
||
|
||
## Sim ticks to fell a tree at 1× speed (80 ticks = ~4 sim seconds at 20 Hz).
|
||
const CHOP_TICKS: int = 80
|
||
## Number of separate wood Item nodes dropped on fell.
|
||
const WOOD_DROPS_ON_FELL: int = 3
|
||
## Stack size per dropped Item (Phase 4 simplicity: 3 items of stack 1 each).
|
||
const STACK_SIZE_PER_DROP: int = 1
|
||
|
||
# ── state ─────────────────────────────────────────────────────────────────────
|
||
|
||
var tile: Vector2i = Vector2i.ZERO
|
||
## 0..CHOP_TICKS. Advanced by on_chop_tick(); tree is felled when equal to CHOP_TICKS.
|
||
var chop_progress: int = 0
|
||
|
||
# Preloaded scene for spawned wood items.
|
||
const ITEM_SCENE: PackedScene = preload("res://scenes/entities/item.tscn")
|
||
|
||
|
||
# ── lifecycle ─────────────────────────────────────────────────────────────────
|
||
|
||
func _ready() -> void:
|
||
position = _tile_to_world(tile)
|
||
World.register_tree(self)
|
||
|
||
|
||
func _exit_tree() -> void:
|
||
World.unregister_tree(self)
|
||
|
||
|
||
# ── public API ────────────────────────────────────────────────────────────────
|
||
|
||
## One-shot initialiser. Call after add_child() so _ready() already fired.
|
||
func setup(start_tile: Vector2i) -> void:
|
||
tile = start_tile
|
||
chop_progress = 0
|
||
position = _tile_to_world(tile)
|
||
queue_redraw()
|
||
Audit.log("tree", "spawned at %s" % tile)
|
||
|
||
|
||
## True when the tree hasn't been fully chopped yet.
|
||
func is_choppable() -> bool:
|
||
return chop_progress < CHOP_TICKS
|
||
|
||
|
||
## Called by the INTERACT toil in JobRunner once per sim tick while the pawn
|
||
## works this tree. Advances chop_progress and fells the tree when complete.
|
||
func on_chop_tick() -> void:
|
||
if not is_choppable():
|
||
return
|
||
chop_progress += 1
|
||
queue_redraw()
|
||
if chop_progress >= CHOP_TICKS:
|
||
fell()
|
||
|
||
|
||
## Drop wood Items and free this node. Called by on_chop_tick() automatically,
|
||
## but also accessible for scripted felling (debug, storyteller events).
|
||
func fell() -> void:
|
||
var drop_tiles := _pick_drop_tiles()
|
||
var drops_count := 0
|
||
for drop_tile in drop_tiles:
|
||
var item: Item = ITEM_SCENE.instantiate()
|
||
get_parent().add_child(item)
|
||
item.setup(Item.TYPE_WOOD, STACK_SIZE_PER_DROP, drop_tile)
|
||
drops_count += 1
|
||
Audit.log("tree", "felled at %s; %d wood drops" % [tile, drops_count])
|
||
queue_free()
|
||
|
||
|
||
# ── save / load ───────────────────────────────────────────────────────────────
|
||
|
||
func to_dict() -> Dictionary:
|
||
return {
|
||
"class_id": &"tree",
|
||
"tile_x": tile.x,
|
||
"tile_y": tile.y,
|
||
"chop_progress": chop_progress,
|
||
}
|
||
|
||
|
||
static func from_dict(d: Dictionary) -> Dictionary:
|
||
return {
|
||
"tile_x": int(d.get("tile_x", 0)),
|
||
"tile_y": int(d.get("tile_y", 0)),
|
||
"chop_progress": int(d.get("chop_progress", 0)),
|
||
}
|
||
|
||
|
||
# ── render ────────────────────────────────────────────────────────────────────
|
||
|
||
func _draw() -> void:
|
||
# Brown trunk: small filled rect at centre-bottom (~4 wide × 6 tall).
|
||
var trunk_color := Color(0.45, 0.28, 0.12)
|
||
draw_rect(Rect2(Vector2(-2.0, 1.0), Vector2(4.0, 6.0)), trunk_color)
|
||
|
||
# Green canopy: large filled circle centered near the top.
|
||
var canopy_color := Color(0.22, 0.60, 0.18)
|
||
draw_circle(Vector2(0.0, -3.0), 7.0, canopy_color)
|
||
|
||
# Canopy outline.
|
||
draw_arc(Vector2(0.0, -3.0), 7.0, 0.0, TAU, 24, Color(0.0, 0.0, 0.0, 0.4), 1.0)
|
||
|
||
# Chop-progress wedge: a dark angled line on the trunk when partially chopped.
|
||
if chop_progress > 0:
|
||
var ratio := float(chop_progress) / float(CHOP_TICKS)
|
||
var notch_depth := ratio * 3.0
|
||
draw_line(
|
||
Vector2(-2.0, 2.0 + notch_depth),
|
||
Vector2(2.0, 2.0),
|
||
Color(0.15, 0.08, 0.02, 0.9),
|
||
1.5
|
||
)
|
||
|
||
|
||
# ── helpers ───────────────────────────────────────────────────────────────────
|
||
|
||
## Returns up to WOOD_DROPS_ON_FELL tile positions for wood drops.
|
||
## Prefers the tree's own tile then walkable 4-neighbours; falls back to the
|
||
## tree tile for any remaining drops when neighbours are scarce.
|
||
func _pick_drop_tiles() -> Array[Vector2i]:
|
||
var chosen: Array[Vector2i] = []
|
||
|
||
# First drop always goes on the tree's tile itself.
|
||
chosen.append(tile)
|
||
|
||
# Remaining drops prefer walkable neighbours.
|
||
var offsets: Array[Vector2i] = [Vector2i(1, 0), Vector2i(-1, 0), Vector2i(0, 1), Vector2i(0, -1)]
|
||
for offset in offsets:
|
||
if chosen.size() >= WOOD_DROPS_ON_FELL:
|
||
break
|
||
var candidate: Vector2i = tile + offset
|
||
if World.pathfinder != null and World.pathfinder.is_walkable(candidate):
|
||
chosen.append(candidate)
|
||
|
||
# Fill any remaining slots with the tree tile (all 3 land there if boxed in).
|
||
while chosen.size() < WOOD_DROPS_ON_FELL:
|
||
chosen.append(tile)
|
||
|
||
return chosen
|
||
|
||
|
||
func _tile_to_world(t: Vector2i) -> Vector2:
|
||
return Vector2(
|
||
t.x * TILE_SIZE_PX + TILE_SIZE_PX / 2.0,
|
||
t.y * TILE_SIZE_PX + TILE_SIZE_PX / 2.0
|
||
)
|