58 lines
1.5 KiB
GDScript3
58 lines
1.5 KiB
GDScript3
|
|
class_name ExplosionVFXBase
|
||
|
|
extends Node2D
|
||
|
|
|
||
|
|
# Shared particle motion/fade/lifetime logic for all explosion VFX types.
|
||
|
|
# Subclasses fix their own visual style and color as facts about
|
||
|
|
# themselves (see LightBurstVFX/AsteroidExplosionVFX), not as parameters
|
||
|
|
# passed in by whatever destroyed the object - so "ships always explode
|
||
|
|
# blue" is guaranteed by the class hierarchy, not by remembering to pass
|
||
|
|
# the right Color() at each call site.
|
||
|
|
|
||
|
|
@export var particle_count: int = 12
|
||
|
|
@export var lifetime: float = 0.5
|
||
|
|
@export var min_speed: float = 60.0
|
||
|
|
@export var max_speed: float = 220.0
|
||
|
|
|
||
|
|
var age: float = 0.0
|
||
|
|
var particles: Array = []
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
randomize()
|
||
|
|
for i in range(particle_count):
|
||
|
|
var angle: float = randf() * TAU
|
||
|
|
var speed: float = randf_range(min_speed, max_speed)
|
||
|
|
particles.append({
|
||
|
|
"pos": Vector2.ZERO,
|
||
|
|
"vel": Vector2(cos(angle), sin(angle)) * speed,
|
||
|
|
"rot": randf() * TAU,
|
||
|
|
"rot_speed": randf_range(-6.0, 6.0),
|
||
|
|
"size": _random_particle_size(),
|
||
|
|
})
|
||
|
|
queue_redraw()
|
||
|
|
|
||
|
|
|
||
|
|
func _process(delta: float) -> void:
|
||
|
|
age += delta
|
||
|
|
if age >= lifetime:
|
||
|
|
queue_free()
|
||
|
|
return
|
||
|
|
for p in particles:
|
||
|
|
p["pos"] += p["vel"] * delta
|
||
|
|
p["vel"] *= 0.9
|
||
|
|
p["rot"] += p["rot_speed"] * delta
|
||
|
|
queue_redraw()
|
||
|
|
|
||
|
|
|
||
|
|
func _draw() -> void:
|
||
|
|
var alpha: float = clamp(1.0 - (age / lifetime), 0.0, 1.0)
|
||
|
|
for p in particles:
|
||
|
|
_draw_particle(p, alpha)
|
||
|
|
|
||
|
|
|
||
|
|
func _random_particle_size() -> float:
|
||
|
|
return randf_range(10.0, 20.0)
|
||
|
|
|
||
|
|
|
||
|
|
func _draw_particle(_p: Dictionary, _alpha: float) -> void:
|
||
|
|
pass
|