harvest/game/scripts/torch_flame.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

138 lines
5 KiB
GDScript

class_name TorchFlame
extends Node3D
## The Propane Torch's flame (#17): a jet of fire particles from the gun's muzzle, sized to the
## band ScarecrowCombat burns (length m past the muzzle, width m across), and a flickering orange
## light. A WeaponModels held effect: it sits at the muzzle facing the way the scarecrow's model
## does (-Z), so it follows the model's yaw, which is all the partner's machine knows of the
## other scarecrow's aim. set_lit() starts and stops the jet; the fire already out keeps flying
## for LIFETIME s after, so the effect fades rather than vanishing.
## Seconds a puff of fire flies: it covers the band's length in that time.
const LIFETIME := 0.35
const PUFFS := 72
## The jet dips this far below level over its length (radians), so the fire washes over the
## enemies in the band rather than flying over their heads from the gun's height.
const DIP := 0.17
## Fire is brighter than white (HDR, which the filmic tonemap rolls off), so it still burns
## through the night look's dimmed, desaturated picture (DayNight's adjustments).
const BRIGHTNESS := 1.4
const LIGHT_ENERGY := 2.4
const LIGHT_COLOR := Color(1.0, 0.55, 0.2)
## The band it was built for (m).
var length: float
var width: float
var lit := false
var _jet: CPUParticles3D
var _light: OmniLight3D
## The light's flicker draws from its own generator, leaving the global one's sequence alone.
var _flicker := RandomNumberGenerator.new()
func _init(p_length: float = 4.5, p_width: float = 1.6) -> void:
length = p_length
width = p_width
name = "TorchFlame"
_jet = _build_jet()
add_child(_jet)
_light = OmniLight3D.new()
_light.name = "Glow"
_light.light_color = LIGHT_COLOR
_light.light_energy = LIGHT_ENERGY
_light.omni_range = length
_light.shadow_enabled = false
_light.position = Vector3(0.0, -0.2, -length * 0.35)
add_child(_light)
set_lit(false)
## Starts the jet (and its light), or stops it: the puffs in the air fly on and fade.
func set_lit(on: bool) -> void:
lit = on
_jet.emitting = on
_light.visible = on
## The jet's direction (unit, world space): forward from the muzzle, dipping.
func direction() -> Vector3:
return (global_basis * _jet.direction).normalized()
func _process(_delta: float) -> void:
if lit:
_light.light_energy = LIGHT_ENERGY * _flicker.randf_range(0.75, 1.15)
func _build_jet() -> CPUParticles3D:
var p := CPUParticles3D.new()
p.name = "Jet"
p.amount = PUFFS
p.lifetime = LIFETIME
p.local_coords = false # puffs already out don't swing with the scarecrow
p.draw_order = CPUParticles3D.DRAW_ORDER_VIEW_DEPTH # blended back to front
p.emission_shape = CPUParticles3D.EMISSION_SHAPE_SPHERE
p.emission_sphere_radius = 0.08
p.direction = Vector3(0.0, -sin(DIP), -cos(DIP))
# Wide enough to fill the band's width at its far end; flattened into a horizontal fan.
p.spread = rad_to_deg(atan2(width * 0.5, length)) * 0.8
p.flatness = 0.7
var speed := length / LIFETIME
p.initial_velocity_min = speed * 0.85
p.initial_velocity_max = speed * 1.08
p.gravity = Vector3(0.0, 1.5, 0.0) # flames rise a little
p.angle_min = -180.0
p.angle_max = 180.0
p.angular_velocity_min = -120.0
p.angular_velocity_max = 120.0
p.scale_amount_min = 0.8
p.scale_amount_max = 1.1
var grow := Curve.new()
grow.add_point(Vector2(0.0, 0.22))
grow.add_point(Vector2(0.45, 0.75))
grow.add_point(Vector2(1.0, 1.0))
p.scale_amount_curve = grow
var ramp := Gradient.new()
ramp.set_offset(0, 0.0)
ramp.set_color(0, Color(1.0, 0.8, 0.3, 0.95)) # yellow-hot at the nozzle
ramp.set_offset(1, 1.0)
ramp.set_color(1, Color(0.3, 0.08, 0.04, 0.0)) # smoke, gone
ramp.add_point(0.18, Color(1.0, 0.48, 0.06, 0.95))
ramp.add_point(0.55, Color(0.95, 0.22, 0.02, 0.85))
ramp.add_point(0.8, Color(0.55, 0.08, 0.02, 0.5))
p.color_ramp = ramp
p.hue_variation_min = -0.02
p.hue_variation_max = 0.03
var quad := QuadMesh.new()
quad.size = Vector2.ONE * width * 0.75
quad.material = _puff_material()
p.mesh = quad
p.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
return p
## A soft round puff, colored by the particle (vertex color) and always facing the camera. Plain
## alpha blending, unlit and unfogged, keeps it fire-colored on bright grass by day and through
## the night's fog (additive blending washed it out to white on the grass).
static func _puff_material() -> StandardMaterial3D:
var dot := Gradient.new()
dot.set_color(0, Color(1.0, 1.0, 1.0, 1.0))
dot.set_color(1, Color(1.0, 1.0, 1.0, 0.0))
dot.add_point(0.45, Color(1.0, 1.0, 1.0, 0.6))
var tex := GradientTexture2D.new()
tex.gradient = dot
tex.fill = GradientTexture2D.FILL_RADIAL
tex.fill_from = Vector2(0.5, 0.5)
tex.fill_to = Vector2(1.0, 0.5)
tex.width = 64
tex.height = 64
var mat := StandardMaterial3D.new()
mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
mat.disable_fog = true
mat.billboard_mode = BaseMaterial3D.BILLBOARD_PARTICLES
mat.billboard_keep_scale = true
mat.vertex_color_use_as_albedo = true
mat.albedo_color = Color(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS)
mat.albedo_texture = tex
mat.cull_mode = BaseMaterial3D.CULL_DISABLED
return mat