harvest/game/scripts/workbench.gd
Forrest df10a1967c Task #9: Workbench auto-grants after Night 1, destructible/repairable
Gate the Workbench to appear once Night 1 is survived instead of being
buildable: StructureSystem.grant_workbench() (an unvalidated, unpurchased
place() variant, grant()) adds a "workbench" structure record at the scene
node's position, reusing the existing damage/repair/targeting/save pipeline
end to end. The grant happens on CampaignController.night_ending (so it
lands in state before the morning snapshot, like #13's crop growth) and is
re-derived from FarmState.nights_survived on load/retry (sync_from_state).

The Workbench's view is the scene's own node (hidden until granted, shown
by StructureSystem._add_view) instead of a second built mesh, so it gets
the standard HP bar, damage tint, and wobble for free. It's repair-only:
damage() never removes its record at 0 HP ("broken" instead of destroyed),
and it can't be dismantled. Interactions refuses to open its shop before
it exists or while broken (and no longer treats it as focusable), and
coop_session.gd's "buy" request handler refuses a workbench-shop item
(Upgrades.shop_of) the same way for a co-op partner. review_panel.gd's
pre-grant summary line no longer promises the work area from night one.

No FarmState.VERSION bump: a "workbench" record is a new type string, not
a new field, and from_dict() already validates types generically.

Tests: a new test_structures.gd check covers absent/no-prompt/shop-refused
before Night 1 (including a co-op "buy" request), granted after, damaged
by an enemy (thief_behavior.gd's generic BREAK state), broken-not-destroyed
at 0 HP with its shop refused, repaired at the normal proportional cost,
and persistence through a save/retry round trip. test_cycle.gd and
test_nights.gd pre-grant the workbench in setup (grant_workbench()) so
their per-night structure-count invariants aren't thrown off by the
night-1 grant itself.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 16:27:55 -04:00

50 lines
1.8 KiB
GDScript

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
_label.shaded = false
_label.font_size = 40
_label.outline_size = 10
_label.pixel_size = 0.0075
_label.modulate = NORMAL_COLOR
_label.position = Vector3(0.0, 2.2, 0.0)
_label.visible = false
add_child(_label)
## 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):
done += 1
var hint := ("[E / X] Upgrades" if campaign.is_daytime() else "Upgrades by day") if focused else "Upgrades"
_label.text = "%s\n%d of %d done" % [hint, done, Upgrades.ORDER.size()]