141 lines
4.3 KiB
GDScript
141 lines
4.3 KiB
GDScript
class_name PumpkinBombs
|
|
extends Node3D
|
|
## Pumpkin bombs in flight, for both scarecrows (ScarecrowCombat throws them): each flies in an
|
|
## arc from the thrower's hand to its target, spinning, at GameConfig.bomb_speed, and bursts on
|
|
## landing: every enemy within its radius (plus the enemy's hit radius) takes its damage, crows
|
|
## included. Walls don't stop the blast; structures, animals, and the scarecrows aren't hurt.
|
|
## A burst flashes an orange ring on the ground (the fear burst's shader) over the blast radius.
|
|
## Co-op: on the partner's machine (replica) bombs are only drawn, from the host's throws; the
|
|
## host's bursts do the damage.
|
|
|
|
signal thrown(from: Vector3, to: Vector3)
|
|
signal burst(at: Vector3, hits: int)
|
|
|
|
const MAX_FLYING := 12
|
|
const BURST_TIME := 0.5
|
|
## Seconds of flight at the shortest and longest.
|
|
const MIN_FLIGHT := 0.35
|
|
const MAX_FLIGHT := 1.1
|
|
|
|
var replica := false
|
|
var bursts: int = 0
|
|
var enemies_hit: int = 0
|
|
|
|
var _cfg: GameConfig
|
|
var _enemies: EnemySystem
|
|
var _flying: Array[Dictionary] = [] # {from, to, t, time, damage, radius, mesh}
|
|
var _idle: Array[MeshInstance3D] = []
|
|
var _rings: Array[Dictionary] = [] # {mesh, t}
|
|
|
|
|
|
func setup(cfg: GameConfig, enemies: EnemySystem) -> void:
|
|
_cfg = cfg
|
|
_enemies = enemies
|
|
|
|
|
|
## Throws one from from (the hand) to to (on the ground). Returns false if too many are flying.
|
|
func throw(from: Vector3, to: Vector3, damage: int, radius: float) -> bool:
|
|
if _flying.size() >= MAX_FLYING:
|
|
return false
|
|
var span := Vector2(to.x - from.x, to.z - from.z).length()
|
|
var mi: MeshInstance3D = _idle.pop_back() if not _idle.is_empty() else _new_mesh()
|
|
mi.visible = true
|
|
mi.global_position = from
|
|
mi.reset_physics_interpolation()
|
|
_flying.append({"from": from, "to": Vector3(to.x, 0.2, to.z), "t": 0.0, "damage": damage, "radius": radius,
|
|
"time": clampf(span / _cfg.bomb_speed, MIN_FLIGHT, MAX_FLIGHT), "peak": 1.0 + span * 0.22, "mesh": mi})
|
|
thrown.emit(from, to)
|
|
return true
|
|
|
|
|
|
func flying() -> int:
|
|
return _flying.size()
|
|
|
|
|
|
## Every bomb in flight lands now, without bursting (a new morning).
|
|
func clear() -> void:
|
|
for f in _flying:
|
|
_park(f["mesh"])
|
|
_flying.clear()
|
|
|
|
|
|
## Physics tick: flights move on; landed ones burst.
|
|
func step(dt: float) -> void:
|
|
var i := 0
|
|
while i < _flying.size():
|
|
var f := _flying[i]
|
|
f["t"] = float(f["t"]) + dt
|
|
var u := minf(float(f["t"]) / float(f["time"]), 1.0)
|
|
var mi := f["mesh"] as MeshInstance3D
|
|
var at := (f["from"] as Vector3).lerp(f["to"], u)
|
|
at.y += float(f["peak"]) * 4.0 * u * (1.0 - u)
|
|
mi.global_position = at
|
|
mi.rotation = Vector3(u * TAU * 1.5, 0.0, u * PI)
|
|
if u < 1.0:
|
|
i += 1
|
|
continue
|
|
_burst(f["to"], int(f["damage"]), float(f["radius"]))
|
|
_park(mi)
|
|
_flying.remove_at(i)
|
|
|
|
|
|
func _burst(at: Vector3, damage: int, radius: float) -> void:
|
|
bursts += 1
|
|
var n := 0
|
|
if not replica:
|
|
var es := _enemies
|
|
for d in es.active_count:
|
|
if es.alive[d] == 0:
|
|
continue
|
|
var reach := radius + es.t_hit_radius[es.type[d]]
|
|
if Vector2(es.pos_x[d] - at.x, es.pos_z[d] - at.z).length_squared() <= reach * reach:
|
|
es.apply_hit(d, damage)
|
|
n += 1
|
|
enemies_hit += n
|
|
_flash(at, radius)
|
|
burst.emit(at, n)
|
|
|
|
|
|
func _new_mesh() -> MeshInstance3D:
|
|
var mi := MeshInstance3D.new()
|
|
mi.mesh = WeaponModels.mesh_of(2)
|
|
mi.scale = Vector3.ONE * WeaponModels.BOMB_SCALE
|
|
mi.top_level = true
|
|
add_child(mi)
|
|
return mi
|
|
|
|
|
|
func _park(mi: MeshInstance3D) -> void:
|
|
mi.visible = false
|
|
_idle.append(mi)
|
|
|
|
|
|
func _flash(at: Vector3, radius: float) -> void:
|
|
var plane := PlaneMesh.new()
|
|
plane.size = Vector2.ONE * radius * 2.0
|
|
var mat := ShaderMaterial.new()
|
|
mat.shader = load("res://game/resources/fear_burst.gdshader") as Shader
|
|
mat.set_shader_parameter("color", Color(1.0, 0.55, 0.12))
|
|
var mi := MeshInstance3D.new()
|
|
mi.mesh = plane
|
|
mi.material_override = mat
|
|
mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
mi.top_level = true
|
|
mi.physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
|
|
add_child(mi)
|
|
mi.global_position = Vector3(at.x, 0.3, at.z)
|
|
_rings.append({"mesh": mi, "t": 0.0})
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
var i := 0
|
|
while i < _rings.size():
|
|
var r := _rings[i]
|
|
r["t"] = float(r["t"]) + delta / BURST_TIME
|
|
var mi := r["mesh"] as MeshInstance3D
|
|
(mi.material_override as ShaderMaterial).set_shader_parameter("t", minf(float(r["t"]), 1.0))
|
|
if float(r["t"]) >= 1.0:
|
|
mi.queue_free()
|
|
_rings.remove_at(i)
|
|
continue
|
|
i += 1
|