Merge task #9: Workbench arrives after Night 1, targetable, repair-only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-05 16:47:05 -04:00
commit 4e3692b99d
12 changed files with 312 additions and 23 deletions

View file

@ -547,7 +547,11 @@ func _do_request(r: Array) -> void:
_events.append(["toast", "Dismantled the %s%s" % [StructureSystem.NAMES[e["type"]].to_lower(),
(": %s back in the shed" % ResourceStore.describe(back)) if not back.is_empty() else ""], Color(0.85, 0.95, 1.0)])
"buy":
var text := Upgrades.buy(c, String(r[1]), sim.structures)
var id := String(r[1])
if Upgrades.shop_of(c.config, id) == "workbench" and (sim.structures == null or not sim.structures.workbench_available()):
_events.append(["toast", "The work area isn't available", Color(1.0, 0.8, 0.5)])
return
var text := Upgrades.buy(c, id, sim.structures)
_events.append(["toast", text, Color(0.85, 0.75, 1.0) if text.contains("done:") else Color(1.0, 0.8, 0.5)])

View file

@ -289,7 +289,11 @@ func _rasterize(hull: PackedVector2Array, inflate: float) -> void:
## Marks the cells within inflate of a convex outline: as blocked (id < 0) or, for free cells,
## as belonging to structure id.
## as belonging to structure id. A structure (id >= 0) is tagged even over an already-blocked
## cell (#9: the Workbench, granted onto its own scene prop's real collision, bypassing the
## build-on-free-ground check every player-placed structure goes through in validate() -- without
## this, it would be invisible to _adjacent_structure()/barrier_ahead(), undetectable and
## unbreakable); this never changes anything for a structure that is on free ground to begin with.
func _rasterize_into(hull: PackedVector2Array, inflate: float, id: int) -> void:
var lo := hull[0]
var hi := hull[0]
@ -304,7 +308,7 @@ func _rasterize_into(hull: PackedVector2Array, inflate: float, id: int) -> void:
for cz in range(c0 / width, c1 / width + 1):
for cx in range(c0 % width, c1 % width + 1):
var i := cz * width + cx
if blocked[i] == 1:
if blocked[i] == 1 and id < 0:
continue
var p := cell_center(i)
if (solid and Geometry2D.is_point_in_polygon(p, hull)) or _dist2_to_outline(p, hull) <= r2:

View file

@ -693,7 +693,12 @@ func _breaker_goals() -> PackedInt32Array:
for e in campaign.state.structures:
if not structures.blocks(e["type"]):
continue
if e["type"] == "workbench" and int(e["hp"]) <= 0:
continue # broken: nothing left to gain by sending a breaker at it
barriers.append(e)
if e["type"] == "workbench":
guarding.append(e) # #9 Decision 3: it guards its own work area, unconditionally
continue
var at := Vector2(e["x"], e["z"])
for a in areas:
if a.grow(config.guard_range).has_point(at):

View file

@ -190,6 +190,10 @@ extends Resource
@export_range(1, 500) var gate_hp: int = 30
@export_range(1, 500) var trap_triggers: int = 10
@export_range(1, 500) var turret_hp: int = 25
## The Workbench (#9): not built by the player (it's granted once Night 1 is survived --
## StructureSystem.grant_workbench()), but it has HP, can be damaged, and is repaired like any
## other structure (repair-only: it stays, broken, at 0 HP instead of being destroyed).
@export_range(1, 500) var workbench_hp: int = 50
## A ground enemy crossing a slowing trap moves at this share of its speed for this long.
@export_range(0.05, 1.0) var trap_slow_factor: float = 0.5
@export var trap_slow_time: float = 2.0
@ -391,6 +395,8 @@ func structure_hp(type: String) -> int:
return gate_hp
"slowing_trap":
return trap_triggers
"workbench":
return workbench_hp
return turret_hp

View file

@ -13,7 +13,9 @@ extends Node3D
## Letting go, a hit, fear, nightfall, or a full pouch cancels it.
## * Workbench / shed counter: press interact by day to open that shop's menu (UpgradePanel,
## shop "workbench" or "shed"); interact again, walking away, or nightfall closes it. Nothing
## else happens here while one is open.
## else happens here while one is open. The Workbench itself doesn't exist (no prompt, no
## focus, its shop can't be opened) until StructureSystem grants it, after Night 1 (#9); once
## it exists but is broken (0 HP), its shop is refused until it's repaired instead.
## * Repair: a damaged structure within REPAIR_REACH comes first. Hold interact for
## repair_time (any phase) to repair it fully for StructureSystem.repair_cost (paid at the
## end); it is refused, once per press, without the materials.
@ -258,7 +260,9 @@ func step(dt: float) -> void:
_denied = true
message.emit(why, Color(1.0, 0.8, 0.5))
elif focus is Workbench and pressed:
if not campaign.is_daytime():
if structures != null and structures.is_broken(structures.workbench_id()):
message.emit("Broken -- repair it", Color(1.0, 0.8, 0.5))
elif not campaign.is_daytime():
message.emit("The work area is for daytime", Color(1.0, 0.8, 0.5))
elif upgrade_panel != null:
upgrade_panel.open()
@ -349,7 +353,7 @@ func _nearest(p: Vector2) -> Node3D:
if d <= n.reach and score < best_d:
best = n
best_d = score
if workbench != null:
if workbench != null and structures != null and structures.has_workbench():
var d := Vector2(workbench.global_position.x, workbench.global_position.z).distance_to(p)
if d <= workbench.reach and d < best_d:
best = workbench
@ -389,8 +393,9 @@ func _process(delta: float) -> void:
prompt = "[E / X] Gather" if why == "" else why
n.show_state(charges(n), near and kit.alive, prompt)
if workbench != null and show_labels:
var near := Vector2(workbench.global_position.x - p.x, workbench.global_position.z - p.z).length() < LABEL_RANGE
workbench.show_state(campaign, near and kit.alive, focus == workbench)
var granted := structures != null and structures.has_workbench()
var near := granted and Vector2(workbench.global_position.x - p.x, workbench.global_position.z - p.z).length() < LABEL_RANGE
workbench.show_state(campaign, near and kit.alive, focus == workbench, granted and structures.is_broken(structures.workbench_id()))
if shed_counter != null and show_labels:
var near := Vector2(shed_counter.global_position.x - p.x, shed_counter.global_position.z - p.z).length() < LABEL_RANGE
shed_counter.show_state(campaign, near and kit.alive, focus == shed_counter)

View file

@ -131,7 +131,10 @@ func refresh() -> void:
lines.append("In the shed: %s. In the pouch: %s." % [_or_none(ResourceStore.describe(r.shed)), _or_none(ResourceStore.describe(r.pouch))])
var plan := waves.tonight() if waves != null else null
if plan != null and plan.title != "":
lines.append("Tonight: %s. Upgrades at the work area: %d of %d done." % [plan.title, _upgrades_done(), Upgrades.ORDER.size()])
if structures != null and structures.has_workbench():
lines.append("Tonight: %s. Upgrades at the work area: %d of %d done." % [plan.title, _upgrades_done(), Upgrades.ORDER.size()])
else: # #9: the Workbench hasn't been granted yet -- it arrives once Night 1 is survived
lines.append("Tonight: %s. The work area will be set up once you survive tonight." % plan.title)
_body.text = "\n".join(lines)
_start.grab_focus.call_deferred()

View file

@ -24,6 +24,15 @@ extends Node3D
## fires turret_l3_projectiles shots at once in a fan. A tar patch's footprint and visual grow by
## trap_level_size_mult[level - 1] (L2 double, L3 triple its L1 size); upgrading refuses with a
## reason if the bigger footprint would overlap another structure or the world.
##
## workbench (#9): not built by the player -- not in ORDER, never validated or paid for. Granted
## once (grant_workbench(), idempotent) at the scene's Workbench node when Night 1 is survived
## (CampaignController.night_ending), and derived again on load/retry (sync_from_state(), when
## FarmState.nights_survived >= 1 and no "workbench" record exists yet) in case it was missed.
## Its view is the existing Workbench scene node (hidden until granted), not a built mesh, so it
## shares every other structure's damage/repair/HP bar/tint/wobble/nav/save pipeline for free.
## Repair-only: at 0 HP it's "broken" (Interactions refuses to open its shop) but, unlike every
## other structure, damage() never removes its record -- there's no rebuilding it from scratch.
signal layout_changed
signal built(id: int, type: String)
@ -43,6 +52,7 @@ const NAMES := {
"gate": "Gate",
"slowing_trap": "Tar patch",
"automated_defense": "Turret",
"workbench": "Workbench",
}
## Structure types with a per-instance level (#10); every other type is always level 1.
const LEVELABLE: Array[String] = ["automated_defense", "slowing_trap"]
@ -67,6 +77,10 @@ var player: PlayerCharacter
## Co-op: player 2's scarecrow (gates open for it too, and nothing is built onto it).
var partner: Node3D
## The scene's Workbench node (Zones/WorkArea/Workbench), found in setup(): its view once
## granted, not built -- see grant_workbench().
var _workbench_node: Workbench
var _views := {} # id -> Node3D
var _bars := {} # id -> MeshInstance3D
var _bodies := {} # id -> Array[RID] (physics bodies to leave out of placement queries)
@ -93,6 +107,11 @@ func setup(p_campaign: CampaignController, p_nav: FarmNavigation, p_zones: FarmZ
player = p_player
nav.set_barrier_cost(roundi(config.barrier_detour / nav.cell_size * FarmNavigation.COST_ORTH))
_bar_shader = load("res://game/resources/hp_bar.gdshader") as Shader
for n in zones.find_children("*", "Node3D", true, false):
if n is Workbench:
_workbench_node = n
break
campaign.night_ending.connect(_on_night_ending)
for u in zones.crop_units():
var lo := Vector2(INF, INF)
var hi := Vector2(-INF, -INF)
@ -233,11 +252,65 @@ func place(type: String, x: float, z: float, rot: int) -> int:
return id
## A non-purchased, unvalidated place(): adds a structure record at (x, z) without validate() or
## pay(), through the rest of the normal pipeline (record, view, nav, built). For structures the
## game grants itself (the Workbench -- grant_workbench()), not ones the player builds.
func grant(type: String, x: float, z: float, rot: int) -> int:
var s := campaign.state
var id := s.next_structure_id
s.next_structure_id += 1
s.structures.append({"id": id, "type": type, "x": x, "z": z, "rot": rot, "hp": max_hp(type), "level": 1})
_add_view(s.structures[s.structures.size() - 1])
_update_nav()
built.emit(id, type)
return id
## Idempotent: grants the Workbench at the scene node's position if it isn't a structure record
## yet. Called when Night 1 is survived (_on_night_ending) and, to derive it on load/retry, from
## sync_from_state() (guarded there to FarmState.nights_survived >= 1); tests may call it directly
## to have the workbench from the start, regardless of the night.
func grant_workbench() -> void:
if has_workbench() or _workbench_node == null:
return
var p := _workbench_node.global_position
grant("workbench", p.x, p.z, 0)
func has_workbench() -> bool:
return workbench_id() >= 0
## The granted Workbench's structure id, or -1 before it exists.
func workbench_id() -> int:
for e in campaign.state.structures:
if e["type"] == "workbench":
return int(e["id"])
return -1
## 0 HP: still a record (repair-only, never removed -- see damage()), but its shop can't be opened.
func is_broken(id: int) -> bool:
var e := campaign.state.structure(id)
return not e.is_empty() and int(e["hp"]) <= 0
## Granted and not broken: whether its shop (and a co-op "buy" request for one of its items) may
## open right now.
func workbench_available() -> bool:
var id := workbench_id()
return id >= 0 and not is_broken(id)
func _on_night_ending() -> void:
grant_workbench()
## Takes a structure down, refunding dismantle_refund of its cost into the shed. Returns the
## refund.
## refund. The Workbench is never dismantlable (it isn't built in the first place).
func dismantle(id: int) -> Dictionary:
var e := campaign.state.structure(id)
if e.is_empty():
if e.is_empty() or e["type"] == "workbench":
return {}
var refund := {}
var cost: Dictionary = config.costs.get(e["type"], {})
@ -250,7 +323,8 @@ func dismantle(id: int) -> Dictionary:
return refund
## Damages a structure; at 0 HP it breaks (debris, destroyed). Returns true if it broke.
## Damages a structure; at 0 HP it breaks (debris, destroyed). Returns true if it broke. The
## Workbench is repair-only: at 0 HP it's "broken" (hurt, not destroyed) and its record stays.
func damage(id: int, amount: int) -> bool:
var e := campaign.state.structure(id)
if e.is_empty():
@ -258,7 +332,7 @@ func damage(id: int, amount: int) -> bool:
campaign.state.record_event("structures_damaged", mini(amount, int(e["hp"])))
e["hp"] = maxi(int(e["hp"]) - amount, 0)
_wobble[id] = WOBBLE_TIME
if int(e["hp"]) > 0:
if int(e["hp"]) > 0 or e["type"] == "workbench":
hurt.emit(id, Vector3(e["x"], 0.0, e["z"]))
return false
campaign.state.record_event("structures_destroyed")
@ -455,10 +529,20 @@ func step(dt: float) -> void:
head.rotation.y = atan2(-dir.x, -dir.z)
## Rebuilds every view and the navigation layer from the farm state (new vacation, retry).
## Rebuilds every view and the navigation layer from the farm state (new vacation, retry). The
## Workbench's view is the scene's own node, not a built one: hide it rather than free it, and
## derive its presence again (nights_survived >= 1, no record yet -- an older save, or a retry
## that somehow missed it) once the state's own structures are back.
func sync_from_state() -> void:
for id in _views.keys():
(_views[id] as Node3D).queue_free()
var v: Node3D = _views[id]
if v == _workbench_node:
v.visible = false
var old_bar: MeshInstance3D = _bars.get(id)
if old_bar != null: # its own HP bar is a real child of the scene node: free it by hand
old_bar.queue_free()
else:
v.queue_free()
_views.clear()
_bars.clear()
_bodies.clear()
@ -469,6 +553,8 @@ func sync_from_state() -> void:
_gate_open.clear()
for e in campaign.state.structures:
_add_view(e)
if campaign.state.nights_survived >= 1:
grant_workbench()
_update_nav()
@ -525,7 +611,7 @@ func _remove(id: int) -> void:
s.structures.remove_at(i)
break
var view: Node3D = _views.get(id)
if view != null:
if view != null and view != _workbench_node: # the scene's own node is hidden, never freed
view.queue_free()
for d in [_views, _bars, _bodies, _tints, _wobble, _heads, _turret_cd, _gate_open]:
(d as Dictionary).erase(id)
@ -555,13 +641,20 @@ func _update_nav() -> void:
layout_changed.emit()
## The Workbench reuses the scene's own node (shown, not built) instead of a fresh _build_view(),
## so there's only ever the one mesh; every other type is built and parented here as usual.
func _add_view(e: Dictionary) -> void:
var id: int = e["id"]
var view := _build_view(e["type"], e["rot"], int(e.get("level", 1)))
view.name = "%s_%d" % [e["type"], id]
add_child(view)
var view: Node3D
if e["type"] == "workbench" and _workbench_node != null:
view = _workbench_node
view.visible = true
else:
view = _build_view(e["type"], e["rot"], int(e.get("level", 1)))
view.name = "%s_%d" % [e["type"], id]
add_child(view)
view.global_position = Vector3(e["x"], _ground_y(Vector2(e["x"], e["z"])), e["z"])
_views[id] = view
view.global_position = Vector3(e["x"], _ground_y(Vector2(e["x"], e["z"])), e["z"])
var rids: Array[RID] = []
for body in view.find_children("*", "StaticBody3D", true, false):
rids.append((body as StaticBody3D).get_rid())

View file

@ -2,14 +2,23 @@ class_name Workbench
extends Node3D
## The work area's tool table, where the scarecrow buys upgrades (Upgrades, UpgradePanel): press
## interact here by day to open the menu. Its label shows how many are done.
## #9: not there from the start -- StructureSystem grants it once Night 1 is survived, reusing
## this very node as its view (hidden until then, StructureSystem._add_view toggles it back on;
## see grant_workbench()). It then has HP like any other structure, and can be damaged and
## repaired; at 0 HP it's broken (show_state's broken prompt) until repaired, instead of being
## destroyed.
## How close the scarecrow's center has to be to this node's origin.
@export var reach: float = 2.2
const NORMAL_COLOR := Color(0.85, 0.75, 1.0)
const BROKEN_COLOR := Color(1.0, 0.55, 0.45)
var _label: Label3D
func _ready() -> void:
visible = false # granted, not built -- StructureSystem shows it once that's happened
_label = Label3D.new()
_label.billboard = BaseMaterial3D.BILLBOARD_ENABLED
_label.no_depth_test = true
@ -17,16 +26,22 @@ func _ready() -> void:
_label.font_size = 40
_label.outline_size = 10
_label.pixel_size = 0.0075
_label.modulate = Color(0.85, 0.75, 1.0)
_label.modulate = NORMAL_COLOR
_label.position = Vector3(0.0, 2.2, 0.0)
_label.visible = false
add_child(_label)
func show_state(campaign: CampaignController, show: bool, focused: bool) -> void:
## broken (0 HP): the shop can't be opened until it's repaired.
func show_state(campaign: CampaignController, show: bool, focused: bool, broken: bool = false) -> void:
_label.visible = show
if not show:
return
if broken:
_label.modulate = BROKEN_COLOR
_label.text = "Broken -- repair it" if focused else "Broken"
return
_label.modulate = NORMAL_COLOR
var done := 0
for id in Upgrades.ORDER:
if Upgrades.owned(campaign.state, id):