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>
This commit is contained in:
parent
ff2ace9d3c
commit
df10a1967c
10 changed files with 272 additions and 21 deletions
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue