harvest/game/scripts/weapon_models.gd
Forrest ca8601029e Task #17: Propane Torch sprays a band of fire
Hit test: _band_hit replaces the torch's cone. It hits the enemies in a
torch_width (1.6 m) wide band along the gun's line, from the scarecrow's
center out to torch_range (4.5 m) past the muzzle, each enemy's hit radius
added. The nearest torch_targets with a clear line are hit; _cone_hit and
_band_hit share _hit_nearest. torch_arc is gone, and the torch joins
MUZZLE_WEAPONS so scripted aim matches the mouse.

Flame: TorchFlame (CPUParticles3D jet plus a flickering light) is sized to
the band. WeaponModels gains a reusable held-effect pattern for #18:
- add_held_effect / lit / douse / advance.
- attack() lights the effect for HOLD_TICKS (1.5) tick intervals instead
  of replaying the kick.
- douse() puts it out the moment fire is let go; it then fades for the
  particle lifetime.
- Timers run on the sim clock (ScarecrowCombat.tick), or on physics frames
  when nothing advances them. That covers the other scarecrow on the
  partner's machine, which the existing "attack" events light, so there is
  no protocol change.

Sound: a new burned signal and a "burn" cue (synth torch_roar.wav,
generated by synth_audio.gd with its own seed) replace the pitchfork's
stab sound.

Tests (test_weapons): band in, beside, behind and beyond; a melee hugger
is burned; held fire over 5 intervals ticks 5 times and stops on release;
the flame is lit at every step while held and hidden after release; the
replica and attack-event paths; the flame follows the model's yaw.

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

239 lines
9.6 KiB
GDScript

class_name WeaponModels
extends Node3D
## The scarecrow's weapons as it holds them (ScarecrowCombat), from the models in
## assets/weapons/, on the scarecrow's model (which faces -Z): the main weapon in use in the
## right hand, the off-hand one in the left. The Blender exports sit far from the origin, so each
## mesh is re-centered (mesh_of) and posed here:
## pitchfork held low like a spear at the right side, tines forward; a stab thrusts it out
## corn shooter at the gun's spot, its corn-cob muzzle at the gameplay muzzle point; each shot
## kicks it back
## pumpkin bomb in the left hand (the off-hand); after a throw the next one grows back in the
## hand over the recharge (set_bomb_charge)
## attack() plays the weapon's motion (the pumpkin just leaves the hand: the thrown one is
## PumpkinBombs'), or lights a held weapon's effect (below).
## Task #7's Potato Cannon, Shurikens, Propane Torch, and Alien Ray have no models of their own
## yet (visuals stay out of scope): each is a placeholder pivot (_add_placeholder) at the corn
## shooter's gun spot, the corn shooter's mesh tinted a different color so they're still tellable
## apart in play; it kicks back like a gun on each attack, unless it's a held weapon.
## Held weapons (add_held_effect; the Propane Torch's TorchFlame, #17) attack every short tick
## while fire is held, so each attack lights their effect instead of replaying a motion. The
## effect sits at the muzzle facing the model's way and stays lit for its hold s after each
## attack: lit while fire is held, out hold s after the last attack (or at once with douse(),
## when fire is let go). Then it fades for fade s (shown, no longer lit) before it's hidden. The
## timers run on the simulation's clock, advance() from ScarecrowCombat.tick() (hand-stepped tests
## drive it too), or on the physics frames when nothing advances them: in co-op, the other
## scarecrow on the partner's farm is lit by its attack events and stepped by nothing there (and
## after a Game Over nothing ticks).
const PATHS: Array[String] = ["res://assets/weapons/PitchFork.glb", "res://assets/weapons/CornShooter.glb",
"res://assets/weapons/PumpkinBomb.glb"]
## Model scale: the pitchfork is 3 m long as modeled, the pumpkin 0.75 m across.
const SCALES: Array[float] = [0.6, 0.55, 0.45]
## Pumpkin bombs in flight use the same scale.
const BOMB_SCALE := 0.45
## Seconds of each weapon's attack motion, and how far it goes.
const STAB_TIME := 0.24
const STAB_REACH := 0.75
const KICK_TIME := 0.09
const KICK := 0.09
static var _meshes := {}
var weapon: int = 0
var offhand: int = -1
var _pivots: Array[Node3D] = []
var _rest: Array[Transform3D] = []
var _attack_t := -1.0
var _bomb_charge := 1.0
## The gameplay muzzle point on the model (setup), where held effects sit.
var _muzzle := Vector3.ZERO
## Held weapons' effects, by Weapon: {"node": a Node3D with set_lit(bool), "hold": s, "fade": s,
## "left": s still lit (> 0), fading (-fade .. 0), or out and hidden (<= -fade)}.
var _held := {}
## Physics frames since advance() was last called (above 1: nothing steps this scarecrow's
## weapons, so its held effects time out on the physics frames instead).
var _undriven: int = 0
## A model's mesh, re-centered (the scene's root node sits where it was in Blender).
static func mesh_of(index: int) -> Mesh:
if not _meshes.has(index):
var scene := (load(PATHS[index]) as PackedScene).instantiate()
var mi := scene.find_children("*", "MeshInstance3D", true, false)[0] as MeshInstance3D
_meshes[index] = mi.mesh
scene.free()
return _meshes[index]
## Builds the three under model, the pitchfork in hand and the off-hand empty. muzzle: the
## gameplay muzzle point on the model (the corn shooter's tip goes there).
func setup(model: Node3D, muzzle: Vector3) -> void:
name = "Weapons"
_muzzle = muzzle
model.add_child(self)
# Pitchfork: tines (the mesh's +Y) forward and a little down, the shaft past the right hip.
_add(0, Transform3D(Basis.from_euler(Vector3(-1.75, 0.0, 0.0)), Vector3(0.32, 1.15, -0.95)))
# Corn shooter: its tip (the mesh's +Z end, 0.59 m out) turned forward onto the muzzle point.
var tip := 0.589 * SCALES[1]
_add(1, Transform3D(Basis.from_euler(Vector3(0.0, PI, 0.0)), muzzle + Vector3(0.0, 0.0, tip)))
# Pumpkin: in the left hand, in front.
_add(2, Transform3D(Basis.IDENTITY, Vector3(-0.42, 1.2, -0.5)))
# Potato cannon, shurikens, propane torch, and the alien ray (Task #7): no models yet, so each
# is the corn shooter's mesh at the same gun spot, tinted so they're still tellable apart.
var gun_rest := Transform3D(Basis.from_euler(Vector3(0.0, PI, 0.0)), muzzle + Vector3(0.0, 0.0, tip))
_add_placeholder(3, gun_rest, Color(0.55, 0.38, 0.22)) # potato cannon: earthy brown
_add_placeholder(4, gun_rest, Color(0.75, 0.78, 0.82)) # shurikens: steel grey
_add_placeholder(5, gun_rest, Color(0.95, 0.45, 0.12)) # propane torch: flame orange
_add_placeholder(6, gun_rest, Color(0.35, 0.95, 0.4)) # alien ray: alien green
show_weapon(0)
show_offhand(-1)
func _add(index: int, rest: Transform3D) -> void:
var pivot := Node3D.new()
pivot.name = ["Pitchfork", "CornShooter", "PumpkinBomb"][index]
add_child(pivot)
var mi := MeshInstance3D.new()
mi.mesh = mesh_of(index)
mi.scale = Vector3.ONE * SCALES[index]
pivot.add_child(mi)
pivot.transform = rest
_pivots.append(pivot)
_rest.append(rest)
## A placeholder main weapon (Task #7, no model of its own): the corn shooter's mesh, tinted, at
## rest. index must follow the order _add was called in (ScarecrowCombat.Weapon).
func _add_placeholder(index: int, rest: Transform3D, tint: Color) -> void:
var pivot := Node3D.new()
pivot.name = ScarecrowCombat.WEAPON_IDS[index]
add_child(pivot)
var mi := MeshInstance3D.new()
mi.mesh = mesh_of(1) # the corn shooter's, reused (no new art for Task #7)
mi.scale = Vector3.ONE * SCALES[1]
var mat := StandardMaterial3D.new()
mat.albedo_color = tint
mi.material_override = mat
pivot.add_child(mi)
pivot.transform = rest
_pivots.append(pivot)
_rest.append(rest)
## Shows this main weapon (ScarecrowCombat.Weapon) in the right hand, and no other (any other
## weapon's held effect goes out and is hidden at once).
func show_weapon(index: int) -> void:
weapon = index
_attack_t = -1.0
for i in ScarecrowCombat.MAIN_WEAPONS:
_pivots[i].visible = i == index
_pivots[i].transform = _rest[i]
for i: int in _held:
if i != index:
_set_held(i, -INF)
## Shows this off-hand weapon in the left hand (-1: none).
func show_offhand(index: int) -> void:
offhand = index
for i in ScarecrowCombat.OFFHAND_WEAPONS:
_pivots[i].visible = i == index
## The main weapon's attack motion: a stab or a kick. A held weapon lights its effect instead.
func attack(index: int) -> void:
if index != weapon:
return
if _held.has(index):
_set_held(index, float(_held[index]["hold"]))
else:
_attack_t = 0.0
## Adds a held weapon's effect (index: its ScarecrowCombat.Weapon): a Node3D with set_lit(on),
## placed at the muzzle facing the model's way (-Z), out until an attack lights it. hold: s it
## stays lit after each attack (longer than the weapon's tick, so it stays lit while fire is
## held). fade: s it stays shown once out (fire already in the air dying away).
func add_held_effect(index: int, effect: Node3D, hold: float, fade: float = 0.0) -> void:
effect.position = _muzzle
add_child(effect)
_held[index] = {"node": effect, "hold": hold, "fade": fade, "left": 0.0}
_set_held(index, -INF)
## A held weapon's effect (null: none).
func held_effect(index: int) -> Node3D:
return _held[index]["node"] if _held.has(index) else null
## Whether a held weapon's effect is lit: it attacked within its hold, and fire wasn't let go.
func lit(index: int) -> bool:
return _held.has(index) and float(_held[index]["left"]) > 0.0
## Fire let go: the held weapons' effects go out now (then fade as usual), not hold s later.
func douse() -> void:
for i: int in _held:
if float(_held[i]["left"]) > 0.0:
_set_held(i, 0.0)
## Runs the held effects' timers on the simulation's clock (ScarecrowCombat.tick()).
func advance(dt: float) -> void:
_undriven = 0
_advance_held(dt)
## Share of the pumpkin bomb's recharge done (1 = a pumpkin in the hand).
func set_bomb_charge(charge: float) -> void:
_bomb_charge = clampf(charge, 0.0, 1.0)
_apply_charge()
func _apply_charge() -> void:
if _pivots.size() > 2:
var s := _bomb_charge * _bomb_charge
_pivots[2].scale = Vector3.ONE * maxf(s, 0.001)
_pivots[2].get_child(0).visible = s > 0.05
func _advance_held(dt: float) -> void:
for i: int in _held:
var left := float(_held[i]["left"])
if left > -float(_held[i]["fade"]):
_set_held(i, left - dt)
## Sets a held effect's timer (s still lit; at or below 0 out, at or below -fade hidden) and shows
## it so.
func _set_held(index: int, left: float) -> void:
var h: Dictionary = _held[index]
var was_lit := float(h["left"]) > 0.0
h["left"] = left
var node := h["node"] as Node3D
if (left > 0.0) != was_lit or left == -INF:
node.call(&"set_lit", left > 0.0)
node.visible = left > -float(h["fade"])
func _physics_process(delta: float) -> void:
_undriven += 1
if _undriven > 1 and not _held.is_empty():
_advance_held(delta)
func _process(delta: float) -> void:
if _attack_t < 0.0 or _pivots.is_empty():
return
_attack_t += delta / (STAB_TIME if weapon == 0 else KICK_TIME)
var u := clampf(_attack_t, 0.0, 1.0)
var at := _rest[weapon].origin
if weapon == 0: # out fast, back slower
var push := sin(PI * pow(u, 0.6)) * STAB_REACH
at += Vector3(0.0, 0.08 * push, -push)
else:
at += Vector3(0.0, 0.02, KICK) * (1.0 - u)
if _attack_t >= 1.0:
_attack_t = -1.0
at = _rest[weapon].origin
_pivots[weapon].position = at