harvest/game/scripts/scarecrow.gd
Forrest fa7f67fe04 Task #16: per-player pouch in co-op
Each scarecrow carries its own pouch (ResourceStore.pouches by avatar
slot; slot 0 is the solo player / host, `pouch` stays slot 0's), with one
shared pouch_capacity so a Bigger pouch grows both. The shed stays shared.

- Gathering, the shed drop-off, repair, upgrade-in-place, patching,
  building and buying all act on the acting avatar's slot. The host pays
  the partner's "place" and "buy" requests from P2's pouch and the shed.
- The partner's build mode, build hotbar, shop menus and materials HUD
  check affordability against P2's pouch (FarmSim.set_local).
- Alien Goop goes to the killer's pouch: FarmSim sets
  EnemySystem.attacker around each avatar's combat step and shots carry
  their shooter's slot; bomb/turret kills go to the nearest scarecrow.
  The replica no longer rolls goop locally.
- When the partner leaves, their pouch goes into the shed (and is named
  in the toast); a co-op save continued alone banks P2's pouch at morning.
- FarmState.VERSION 7 (resources.partner_pouch) with a v6 migration;
  CoopSession.PROTOCOL 4.
- Tests: per-slot store rules, v6 save migration, and test_coop cases for
  separate gathering, no cross-banking in the shed, own-pouch paying,
  shared capacity, goop credit and leaving. test_coop's crowd case now
  resets the loopback's drop counter so earlier traffic can't change
  which messages it loses.
- Docs: COOP.md and RULES.md no longer say the pouch is shared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 13:06:25 -04:00

302 lines
9.6 KiB
GDScript

class_name Scarecrow
extends Node3D
## The scarecrow's defensive kit apart from the gun (ScarecrowCombat):
## * Health lives in FarmState and carries over between nights (D7). try_damage() is the only
## way to hurt the scarecrow: after an accepted hit it is invulnerable for
## invulnerability_time, so several hits on one tick cost one hit's damage. Hits blink the
## model red, tint the screen (ScarecrowHud), and interrupt patching.
## * Fear (fear action): every thief within fear_radius flees for fear_duration; carriers
## drop their crop (D5) and thieves mid-steal lose their progress. Then fear_cooldown
## seconds until the next one. A ring under the scarecrow shows that fear is ready.
## * Patch up (hold patch): stand still, unable to shoot, for patch_time, then +patch_heal HP
## for costs["patch"] (straw, paid when the patch completes). Releasing early, taking a hit,
## or using fear cancels it. Not available at full health, while gathering, or without the
## straw (patch_denied says what's missing).
## * Defeat: at 0 HP the scarecrow falls (death clip) and stops acting; the campaign turns it
## into Game Over on the same tick (in co-op only once both have fallen: until then it's out
## until the morning). Every new morning, also after a retry, revives it with fear ready.
## In co-op there are two kits: slot 0 (player 1) uses FarmState.scarecrow_hp, slot 1 (player 2)
## partner_hp, and each patches with straw from its own pouch (and the shed). A kit whose remote
## is set (the partner's, on the host) reads RemoteControls instead of the local input.
signal damaged(hp: int)
signal fear_used(position: Vector3, scared: int)
signal fear_ready
signal patched(hp: int)
signal patch_cancelled
signal patch_denied(missing: Dictionary)
const BURST_TIME := 0.45
var config: GameConfig
var campaign: CampaignController
var player: PlayerCharacter
var enemies: EnemySystem
var behavior: ThiefBehavior
## Set by FarmSim: patching doesn't start while a gather channel runs.
var interactions: Interactions
## 0 = player 1, 1 = player 2 (whose health in FarmState), and the partner's input (co-op host).
var slot: int = 0
var remote: RemoteControls
var alive := true
var invulnerable_left: float = 0.0
## Seconds until fear can be used again (0 = ready).
var fear_left: float = 0.0
var patching := false
var patch_elapsed: float = 0.0
var accepted_hits: int = 0
var rejected_hits: int = 0
var _burst: MeshInstance3D
var _burst_mat: ShaderMaterial
var _burst_t: float = -1.0
var _ready_ring: MeshInstance3D
var _ready_mat: StandardMaterial3D
var _patch_ring: MeshInstance3D
var _patch_mat: ShaderMaterial
var _time: float = 0.0
var _fear_requested := false
var _patch_denied := false
var _fear_was_held := false
func setup(cfg: GameConfig, p_campaign: CampaignController, p_player: PlayerCharacter,
p_enemies: EnemySystem, p_behavior: ThiefBehavior) -> void:
config = cfg
campaign = p_campaign
player = p_player
enemies = p_enemies
behavior = p_behavior
campaign.campaign_ended.connect(_on_campaign_ended)
_build_effects()
func hp() -> int:
return campaign.state.scarecrow_hp if slot == 0 else campaign.state.partner_hp
func max_hp() -> int:
return campaign.state.scarecrow_max_hp if slot == 0 else campaign.state.partner_max_hp
func _set_hp(value: int) -> void:
if slot == 0:
campaign.state.scarecrow_hp = clampi(value, 0, campaign.state.scarecrow_max_hp)
else:
campaign.state.partner_hp = clampi(value, 0, campaign.state.partner_max_hp)
func can_shoot() -> bool:
return alive and not patching
## Share of the fear cooldown that has passed: 1 when fear is ready.
func fear_charge() -> float:
var cd := fear_cooldown()
return 1.0 - fear_left / cd if cd > 0.0 else 1.0
## Fear's reach and recharge time, with upgrades.
func fear_radius() -> float:
return Upgrades.value(config, campaign.state, "fear_radius")
func fear_cooldown() -> float:
return Upgrades.value(config, campaign.state, "fear_cooldown")
## One physics tick: timers, then the fear and patch inputs.
func step(dt: float) -> void:
invulnerable_left = maxf(invulnerable_left - dt, 0.0)
if fear_left > 0.0:
fear_left = maxf(fear_left - dt, 0.0)
if fear_left == 0.0:
fear_ready.emit()
# A fear press counts from its input event (so even a tap between two ticks registers) or
# from the action turning pressed since the last tick (simulated input).
var held := remote.held(&"fear") if remote != null else Input.is_action_pressed("fear")
var pressed := _fear_requested or (held and not _fear_was_held)
if remote != null:
pressed = remote.take(&"fear")
_fear_requested = false
_fear_was_held = held
if not alive:
return
if pressed:
use_fear()
step_patch(dt, remote.held(&"patch") if remote != null else Input.is_action_pressed("patch"))
func _unhandled_input(event: InputEvent) -> void:
if remote == null and event.is_action_pressed("fear", false):
_fear_requested = true
## Advances the patch channel while held (keeps channeling, one patch after another, for as
## long as it is held and the scarecrow isn't at full health); releasing cancels.
func step_patch(dt: float, held: bool) -> void:
if not held or not alive:
cancel_patch()
_patch_denied = false
return
var s := campaign.state
var cost: Dictionary = config.costs.get("patch", {})
if not patching:
if hp() >= max_hp() or (interactions != null and interactions.channeling()):
return
if not s.resources.can_afford(cost, slot):
if not _patch_denied: # once per press
_patch_denied = true
patch_denied.emit(s.resources.missing(cost, slot))
return
patching = true
patch_elapsed = 0.0
player.start_channel(player.channel_animation)
patch_elapsed += dt
if patch_elapsed >= config.patch_time:
_end_patch()
if s.pay(cost, slot):
_set_hp(hp() + config.patch_heal)
s.record_event("patches")
patched.emit(hp())
func cancel_patch() -> void:
if patching:
_end_patch()
patch_cancelled.emit()
## The only way to hurt the scarecrow. Returns true if the hit was accepted.
func try_damage(amount: int = 1) -> bool:
if not alive:
return false
if invulnerable_left > 0.0:
rejected_hits += 1
return false
invulnerable_left = config.invulnerability_time
accepted_hits += 1
var s := campaign.state
s.record_event("scarecrow_damage", mini(amount, hp()))
_set_hp(hp() - amount)
cancel_patch()
damaged.emit(hp())
if hp() <= 0:
_die()
return true
## Fear burst at the scarecrow. Returns how many thieves fled, or -1 if it isn't ready.
func use_fear() -> int:
if not alive or fear_left > 0.0:
return -1
cancel_patch()
fear_left = fear_cooldown()
var p := player.global_position
var n := behavior.scare(enemies, Vector2(p.x, p.z), fear_radius())
play_burst()
fear_used.emit(p, n)
return n
## The fear ring spreading from the scarecrow (also on the co-op partner's machine).
func play_burst() -> void:
var p := player.global_position
_burst.scale = Vector3.ONE * fear_radius() / config.fear_radius
_burst_t = 0.0
_burst.global_position = Vector3(p.x, p.y + 0.3, p.z)
func reset_for_morning() -> void:
alive = true
invulnerable_left = 0.0
fear_left = 0.0
_end_patch()
player.revive()
player.set_flash(0.0)
func _die() -> void:
if not alive:
return
alive = false
_end_patch()
player.die()
func _end_patch() -> void:
patching = false
patch_elapsed = 0.0
if player != null:
player.stop_channel()
func _on_campaign_ended(outcome: CampaignController.Outcome) -> void:
if outcome == CampaignController.Outcome.GAME_OVER_SCARECROW:
_die()
func _process(delta: float) -> void:
if player == null:
return
_time += delta
var flash := 0.0
if alive and invulnerable_left > 0.0:
flash = 0.65 if fmod(invulnerable_left, 0.2) > 0.1 else 0.15
player.set_flash(flash)
var at := player.get_global_transform_interpolated().origin
if _burst_t >= 0.0:
_burst_t += delta / BURST_TIME
_burst.visible = _burst_t < 1.0
_burst_mat.set_shader_parameter("t", clampf(_burst_t, 0.0, 1.0))
if _burst_t >= 1.0:
_burst_t = -1.0
_ready_ring.visible = alive and fear_left == 0.0
if _ready_ring.visible:
_ready_ring.global_position = at + Vector3(0.0, 0.12, 0.0)
_ready_mat.albedo_color.a = 0.45 + 0.25 * sin(_time * 4.0)
_patch_ring.visible = patching
if patching:
_patch_ring.global_position = at + Vector3(0.0, 2.75, 0.0)
_patch_mat.set_shader_parameter("progress", patch_elapsed / config.patch_time)
func _build_effects() -> void:
var plane := PlaneMesh.new()
plane.size = Vector2.ONE * config.fear_radius * 2.0
_burst_mat = ShaderMaterial.new()
_burst_mat.shader = load("res://game/resources/fear_burst.gdshader") as Shader
_burst = _effect(plane, _burst_mat)
var torus := TorusMesh.new()
torus.inner_radius = 0.55
torus.outer_radius = 0.68
torus.rings = 32
torus.ring_segments = 4
_ready_mat = StandardMaterial3D.new()
_ready_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
_ready_mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
_ready_mat.albedo_color = Color(0.6, 0.9, 1.0, 0.6)
_ready_mat.no_depth_test = true
_ready_ring = _effect(torus, _ready_mat)
_ready_ring.scale = Vector3(1.0, 0.12, 1.0)
var quad := QuadMesh.new()
quad.size = Vector2(0.9, 0.9)
_patch_mat = ShaderMaterial.new()
_patch_mat.shader = load("res://game/resources/steal_ring.gdshader") as Shader
_patch_mat.set_shader_parameter("fill_color", Color(0.45, 1.0, 0.4))
_patch_ring = _effect(quad, _patch_mat)
func _effect(mesh: Mesh, mat: Material) -> MeshInstance3D:
var mi := MeshInstance3D.new()
mi.mesh = mesh
mi.material_override = mat
mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
mi.top_level = true
mi.visible = false
mi.physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
add_child(mi)
return mi