harvest/game/scripts/interactions.gd
Forrest 308854d52c Task #6: Supply Shed as its own weapon shop, separate from the Workbench
Adds a second UpgradePanel instance (shop "shed") at the Supply Shed's new
ShedCounter node, opened by interact like the Workbench. corn_shooter and
pumpkin_bomb move there ("shop": "shed" in GameConfig.upgrades); the
Workbench's panel (shop "workbench", the default) keeps rapid_fire,
heavy_seeds, and the rest, with "requires" still crossing shops. Co-op buys
go through the same ["buy", id] host gate; a shed_requested signal mirrors
workbench_requested so the partner's own shed panel opens. The shed's
walk-in pouch deposit is unaffected (no interact, not gated by the
counter's reach).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 15:13:48 -04:00

352 lines
13 KiB
GDScript

class_name Interactions
extends Node3D
## The scarecrow's hands: gathering at GatherNodes, buying at the Workbench or the supply shed's
## ShedCounter, repairing damaged structures, and dropping the pouch off at the supply shed.
## Nothing here works in build mode.
## * Focus: the nearest gather point, workbench, or shed counter within its reach, preferring
## gather points with charges left over used-up ones; nearby points show their charges, the
## focused one its prompt (or why it can't be used).
## * Gather: hold interact by day (D8) at a point with charges left and room in the pouch for
## the whole yield (so a charge is never half wasted).
## The scarecrow is rooted for gather_time looping the point's clip, then yield_amount goes
## into the pouch ("+2 wood" pops up) and one charge is used. Keep holding to gather again.
## 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.
## * 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.
## * Deposit: walking into the Supply Shed zone empties the pouch into the shed (no interact,
## and not gated by the shed counter's reach or the shed shop being open).
## * Overnight (the end of the dawn grace) every used point regains regen_per_night charges.
## In co-op each scarecrow has its own Interactions. The partner's, on the host, reads its
## RemoteControls (remote), asks the partner's game to open its own upgrade menu
## (workbench_requested / shed_requested), and leaves the gather labels to the local player's
## (show_labels).
signal gathered(material: String, amount: int, node: GatherNode)
signal deposited(moved: Dictionary)
signal message(text: String, color: Color)
## A remote kit pressed interact at the workbench by day (its game opens the upgrade menu).
signal workbench_requested
## A remote kit pressed interact at the shed counter by day (its game opens the shed's shop).
signal shed_requested
const LABEL_RANGE := 7.0
const REPAIR_REACH := 1.4
const POPUP_TIME := 1.1
const POPUPS := 6
var config: GameConfig
var campaign: CampaignController
var player: PlayerCharacter
var kit: Scarecrow
var zones: FarmZones
## Set by FarmSim.
var structures: StructureSystem
var build: BuildMode
var upgrade_panel: UpgradePanel
var shed_panel: UpgradePanel
## The partner's input on the host (co-op), and whether this one draws the gather labels.
var remote: RemoteControls
var show_labels := true
var nodes: Array[GatherNode] = []
var workbench: Workbench
var shed_counter: ShedCounter
var focus: Node3D
var gathering: GatherNode
var gather_elapsed: float = 0.0
## Damaged structure in reach (-1 = none) and the one being repaired.
var repair_target: int = -1
var repairing: int = -1
var repair_elapsed: float = 0.0
var _shed: FarmZone
var _interact_requested := false
var _interact_was_held := false
var _denied := false
var _popups: Array[Label3D] = []
var _repair_label: Label3D
var _popup_left := PackedFloat32Array()
func setup(cfg: GameConfig, p_campaign: CampaignController, p_player: PlayerCharacter, p_kit: Scarecrow,
p_zones: FarmZones) -> void:
config = cfg
campaign = p_campaign
player = p_player
kit = p_kit
zones = p_zones
_shed = zones.zone(FarmZone.Kind.SUPPLY_SHED)
for n in zones.find_children("*", "Node3D", true, false):
if n is GatherNode:
nodes.append(n)
elif n is Workbench and workbench == null:
workbench = n
elif n is ShedCounter and shed_counter == null:
shed_counter = n
kit.damaged.connect(func(_hp: int) -> void: cancel())
kit.fear_used.connect(func(_p: Vector3, _n: int) -> void: cancel())
_repair_label = Label3D.new()
_repair_label.billboard = BaseMaterial3D.BILLBOARD_ENABLED
_repair_label.no_depth_test = true
_repair_label.shaded = false
_repair_label.font_size = 40
_repair_label.outline_size = 10
_repair_label.pixel_size = 0.0075
_repair_label.modulate = Color(1.0, 0.9, 0.7)
_repair_label.top_level = true
_repair_label.visible = false
add_child(_repair_label)
_popup_left.resize(POPUPS)
for i in POPUPS:
var l := Label3D.new()
l.billboard = BaseMaterial3D.BILLBOARD_ENABLED
l.no_depth_test = true
l.shaded = false
l.font_size = 56
l.outline_size = 12
l.pixel_size = 0.01
l.visible = false
l.top_level = true
l.physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
add_child(l)
_popups.append(l)
func charges(n: GatherNode) -> int:
return int(campaign.state.gather_charges.get(String(n.name), n.max_charges))
func channeling() -> bool:
return gathering != null or repairing >= 0
func can_shoot() -> bool:
return not channeling() and not (upgrade_panel != null and upgrade_panel.is_open) \
and not (shed_panel != null and shed_panel.is_open) and not (remote != null and remote.ui_open)
## One physics tick, after the scarecrow kit.
func step(dt: float) -> void:
var held := remote.held(&"interact") if remote != null else Input.is_action_pressed("interact")
var pressed := _interact_requested or (held and not _interact_was_held)
if remote != null:
pressed = remote.take(&"interact")
_interact_requested = false
_interact_was_held = held
if not held:
_denied = false
if not kit.alive or (build != null and build.active) or (remote != null and (remote.build_active or remote.ui_open)):
if upgrade_panel != null:
upgrade_panel.close()
if shed_panel != null:
shed_panel.close()
cancel()
repair_target = -1
focus = null
return
var p := Vector2(player.global_position.x, player.global_position.z)
if upgrade_panel != null and upgrade_panel.is_open:
_step_menu(upgrade_panel, workbench, p, pressed)
return
if shed_panel != null and shed_panel.is_open:
_step_menu(shed_panel, shed_counter, p, pressed)
return
if _shed != null and _shed.contains(player.global_position) and campaign.state.resources.pouch_total() > 0:
var moved := campaign.state.resources.deposit()
deposited.emit(moved)
message.emit("Stored %s in the shed" % ResourceStore.describe(moved), Color(0.85, 0.95, 1.0))
if repairing >= 0:
if not held or structures.repair_cost(repairing).is_empty():
cancel()
return
repair_elapsed += dt
if repair_elapsed >= config.repair_time:
var id := repairing
var cost := structures.repair_cost(id)
cancel()
if structures.repair(id):
message.emit("Repaired (%s)" % ResourceStore.describe(cost), Color(0.6, 1.0, 0.55))
return
if gathering != null:
if not held or _blocker(gathering) != "":
cancel()
return
gather_elapsed += dt
var to := Vector2(gathering.global_position.x - p.x, gathering.global_position.z - p.y)
if to.length_squared() > 1e-4:
player.face_aim(to.normalized(), 0.3)
if gather_elapsed >= config.gather_time:
_finish_gather()
return
repair_target = structures.structure_near(p, REPAIR_REACH, true) if structures != null else -1
if repair_target >= 0:
focus = null
if held and not kit.patching:
var cost := structures.repair_cost(repair_target)
if campaign.state.resources.can_afford(cost):
repairing = repair_target
repair_elapsed = 0.0
player.start_channel(&"Hammering")
elif not _denied:
_denied = true
message.emit("Not enough to repair: need %s more" % ResourceStore.describe(campaign.state.resources.missing(cost)), Color(1.0, 0.8, 0.5))
return
focus = _nearest(p)
if focus is GatherNode and held and not kit.patching:
var why := _blocker(focus)
if why == "":
gathering = focus
gather_elapsed = 0.0
player.start_channel(gathering.clip)
elif not _denied:
_denied = true
message.emit(why, Color(1.0, 0.8, 0.5))
elif focus is Workbench and pressed:
if 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()
elif remote != null:
workbench_requested.emit()
elif focus is ShedCounter and pressed:
if not campaign.is_daytime():
message.emit("The supply shed is for daytime", Color(1.0, 0.8, 0.5))
elif shed_panel != null:
shed_panel.open()
elif remote != null:
shed_requested.emit()
## A shop's menu is open: closes it on interact again, by walking off its counter's reach, at
## nightfall, or entering build mode (workbench: Workbench; shed: ShedCounter; both just need
## global_position and reach).
func _step_menu(panel: UpgradePanel, counter, p: Vector2, pressed: bool) -> void:
var at := Vector2(counter.global_position.x, counter.global_position.z) if counter != null else p
var reach: float = counter.reach if counter != null else 0.0
if pressed or not campaign.is_daytime() or at.distance_to(p) > reach + 0.5 or (build != null and build.active):
panel.close()
## Why a point can't be gathered right now ("" if it can).
func _blocker(n: GatherNode) -> String:
if not campaign.is_daytime():
return "Gathering is for daytime"
if charges(n) <= 0:
return "Nothing left here; it grows back overnight"
if campaign.state.resources.pouch_room() < n.yield_amount:
return "Pouch full: drop it off at the supply shed"
return ""
func cancel() -> void:
if gathering != null or repairing >= 0:
gathering = null
gather_elapsed = 0.0
repairing = -1
repair_elapsed = 0.0
player.stop_channel()
## Overnight: every used gather point regains its regen_per_night charges.
func regen_overnight() -> void:
var used := campaign.state.gather_charges
for n in nodes:
var key := String(n.name)
if used.has(key):
var c := int(used[key]) + n.regen_per_night
if c >= n.max_charges:
used.erase(key)
else:
used[key] = c
func _finish_gather() -> void:
var n := gathering
var s := campaign.state
var added := s.resources.add_to_pouch(n.material, n.yield_amount)
var left := charges(n) - 1
s.gather_charges[String(n.name)] = left
cancel()
gathered.emit(n.material, added, n)
_popup("+%d %s" % [added, n.material], n.global_position + Vector3(0.0, n.top() + 1.4, 0.0), GatherNode.COLORS[n.material])
func _nearest(p: Vector2) -> Node3D:
var best: Node3D = null
var best_d := INF
for n in nodes:
var d := Vector2(n.global_position.x, n.global_position.z).distance_to(p)
var score := d + (100.0 if charges(n) <= 0 else 0.0) # used-up points only as a fallback
if d <= n.reach and score < best_d:
best = n
best_d = score
if workbench != null:
var d := Vector2(workbench.global_position.x, workbench.global_position.z).distance_to(p)
if d <= workbench.reach and d < best_d:
best = workbench
best_d = d
if shed_counter != null:
var d := Vector2(shed_counter.global_position.x, shed_counter.global_position.z).distance_to(p)
if d <= shed_counter.reach and d < best_d:
best = shed_counter
best_d = d
return best
func _popup(text: String, at: Vector3, color: Color) -> void:
var slot := 0
for i in POPUPS:
if _popup_left[i] < _popup_left[slot]:
slot = i
var l := _popups[slot]
l.text = text
l.modulate = color
l.global_position = at
l.visible = true
_popup_left[slot] = POPUP_TIME
func _process(delta: float) -> void:
if player == null:
return
var p := player.global_position
for n in nodes if show_labels else []:
var near := Vector2(n.global_position.x - p.x, n.global_position.z - p.z).length() < LABEL_RANGE
var prompt := ""
if n == gathering:
prompt = "Gathering..."
elif n == focus:
var why := _blocker(n)
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)
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)
var shown := (repairing if repairing >= 0 else repair_target) if show_labels else -1
_repair_label.visible = shown >= 0
if shown >= 0:
var e := campaign.state.structure(shown)
if not e.is_empty():
_repair_label.global_position = Vector3(e["x"], 2.7, e["z"])
var cost := structures.repair_cost(shown)
_repair_label.text = "%s %d/%d\n%s" % [StructureSystem.NAMES[e["type"]], int(e["hp"]), structures.max_hp(e["type"]),
"Repairing..." if repairing >= 0 else "[E / X] Repair (%s)" % ResourceStore.describe(cost)]
for i in POPUPS:
if _popup_left[i] <= 0.0:
continue
_popup_left[i] = maxf(_popup_left[i] - delta, 0.0)
var l := _popups[i]
l.visible = _popup_left[i] > 0.0
l.global_position.y += delta * 1.2
l.modulate.a = clampf(_popup_left[i] / 0.4, 0.0, 1.0)
func _unhandled_input(event: InputEvent) -> void:
if remote == null and event.is_action_pressed("interact", false):
_interact_requested = true