harvest/game/scripts/interactions.gd
Forrest 9f4a1895bd #36: prompts in touch words
Prompts (game/scripts/prompts.gd) holds every key-naming prompt: on a phone
(MobilePort.active()) it says each in touch words, elsewhere exactly the old
string. World labels drop the key (Gather, Repair, Upgrade to, Upgrades, Shop),
the build ghost says Place / Dismantle, the build bar's line is
"Place · Rotate · Dismantle · Done" and its slots drop the number keys, the
cutscenes say "Hold to skip" / "Tap to skip", the tip footer says
"× hides tips", and the tutorial's tips and the first morning's sign name the
thumbs (sides from TouchSticks), Use, Build, Place, Fear, Bomb, Patch, the
hotbar, and the backpack. MobilePort gains the `forced` test override.
game/tests/mobile/prompts_test.gd checks every phone string and that the
desktop strings are byte for byte as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:30:26 -04:00

456 lines
18 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. 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.
## * Upgrade (#10): a levelable structure (turret, tar patch) within REPAIR_REACH, undamaged
## (repair takes priority while it isn't), comes next. Hold interact by day for
## structure_upgrade_time to level it up one, for StructureSystem.upgrade_cost (paid at the
## end); refused, once per press, without the materials, room (a bigger tar patch may not fit),
## or at Max level. Night-gated like the workbench, unlike repair, since it's a build-mode-style
## decision, not an emergency fix.
## * 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). Each one gathers into, deposits, and pays from its own scarecrow's pouch
## (kit.slot, ResourceStore): a partner standing in the shed banks only their own pouch. The
## host's copy of the partner trails the partner's own machine by a few ticks, so a press of
## interact made on arrival at a shop's counter can reach the host while the copy is still out of
## reach: it stays pending for PRESS_WINDOW seconds, until a counter in reach takes it (#22).
signal gathered(material: String, amount: int, node: GatherNode)
signal deposited(moved: Dictionary)
## What happened, or why not ("Pouch full: ..."). Nothing shows it on screen since #23; tests
## listen to it.
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
## Seconds a partner's press of interact waits (on the host) for its copy to reach a counter.
const PRESS_WINDOW := 0.3
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
## Levelable structure in reach, undamaged (-1 = none), and the one being upgraded (#10).
var upgrade_target: int = -1
var upgrading: int = -1
var upgrade_elapsed: float = 0.0
var _shed: FarmZone
var _interact_requested := false
var _interact_was_held := false
## Co-op: what's left of the partner's pending press (seconds; 0 = none).
var _press_left: float = 0.0
var _denied := false
var _popups: Array[Label3D] = []
var _repair_label: Label3D
var _upgrade_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)
_upgrade_label = Label3D.new()
_upgrade_label.billboard = BaseMaterial3D.BILLBOARD_ENABLED
_upgrade_label.no_depth_test = true
_upgrade_label.shaded = false
_upgrade_label.font_size = 40
_upgrade_label.outline_size = 10
_upgrade_label.pixel_size = 0.0075
_upgrade_label.modulate = Color(0.75, 0.95, 1.0)
_upgrade_label.top_level = true
_upgrade_label.visible = false
add_child(_upgrade_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 or upgrading >= 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_press(dt)
_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
upgrade_target = -1
focus = null
_press_left = 0.0
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(kit.slot) > 0:
var moved := campaign.state.resources.deposit(kit.slot)
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, kit.slot):
message.emit("Repaired (%s)" % ResourceStore.describe(cost), Color(0.6, 1.0, 0.55))
return
if upgrading >= 0:
if not held or not campaign.is_daytime() or structures.upgrade_blocker(upgrading, kit.slot) != "":
cancel()
return
upgrade_elapsed += dt
if upgrade_elapsed >= config.structure_upgrade_time:
var id := upgrading
var cost := structures.upgrade_cost(id)
cancel()
if structures.upgrade(id, kit.slot):
message.emit("Upgraded to L%d (%s)" % [structures.level_of(id), 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, kit.slot):
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, kit.slot)), Color(1.0, 0.8, 0.5))
return
upgrade_target = structures.upgradeable_near(p, REPAIR_REACH) if structures != null else -1
if upgrade_target >= 0:
focus = null
if held and not kit.patching:
if not campaign.is_daytime():
if not _denied:
_denied = true
message.emit("Upgrading is for daytime", Color(1.0, 0.8, 0.5))
else:
var why := structures.upgrade_blocker(upgrade_target, kit.slot)
if why == "":
upgrading = upgrade_target
upgrade_elapsed = 0.0
player.start_channel(&"Hammering")
elif not _denied:
_denied = true
message.emit(why, 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:
_press_left = 0.0
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()
elif remote != null:
workbench_requested.emit()
elif focus is ShedCounter and pressed:
_press_left = 0.0
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()
_press_left = 0.0
## Co-op, the partner's hands on the host: whether a press of interact is pending. A new one
## (RemoteControls.take) waits up to PRESS_WINDOW seconds for a counter in reach to take it
## (which clears _press_left), instead of being dropped on the tick it arrives.
func _remote_press(dt: float) -> bool:
if remote.take(&"interact"):
_press_left = PRESS_WINDOW
else:
_press_left = maxf(_press_left - dt, 0.0)
return _press_left > 0.0
## 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(kit.slot) < n.yield_amount:
return "Pouch full: drop it off at the supply shed"
return ""
## The upgrade prompt for a levelable structure in reach: what it costs, "Max level", why it
## can't right now, or that upgrading (unlike repair) is for daytime.
func _upgrade_prompt(id: int) -> String:
if not campaign.is_daytime():
return "Upgrading is for daytime"
var why := structures.upgrade_blocker(id, kit.slot)
if why == "":
return Prompts.upgrade_to(structures.level_of(id) + 1, ResourceStore.describe(structures.upgrade_cost(id)))
return why
func cancel() -> void:
if gathering != null or repairing >= 0 or upgrading >= 0:
gathering = null
gather_elapsed = 0.0
repairing = -1
repair_elapsed = 0.0
upgrading = -1
upgrade_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, kit.slot)
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 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
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 = Prompts.gather() if why == "" else why
n.show_state(charges(n), near and kit.alive, prompt)
if workbench != null and show_labels:
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)
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 Prompts.repair(ResourceStore.describe(cost))]
var ushown := (upgrading if upgrading >= 0 else upgrade_target) if show_labels else -1
_upgrade_label.visible = ushown >= 0
if ushown >= 0:
var e := campaign.state.structure(ushown)
if not e.is_empty():
_upgrade_label.global_position = Vector3(e["x"], 3.3, e["z"])
_upgrade_label.text = "%s L%d\n%s" % [StructureSystem.NAMES[e["type"]], int(e.get("level", 1)),
"Upgrading..." if upgrading >= 0 else _upgrade_prompt(ushown)]
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