Skip to main content

euv_core/game/particles/
impl.rs

1use super::*;
2
3/// Default radial spark burst emitter configuration.
4impl Default for ParticleEmitterConfig {
5    /// Creates a radial neon spark burst configuration.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Default particle emitter config.
10    fn default() -> Self {
11        Self {
12            base_angle: 0.0,
13            spread_rad: TAU,
14            min_speed: 60.0,
15            max_speed: 220.0,
16            min_life: 0.25,
17            max_life: 0.65,
18            start_size: 4.5,
19            end_size: 0.8,
20            start_color: Color::rgba(56, 189, 248, 255),
21            end_color: Color::rgba(99, 102, 241, 0),
22            drag: 2.2,
23        }
24    }
25}
26
27/// Default 1,024-slot particle system pool.
28impl Default for ParticleSystem {
29    /// Allocates a `ParticleSystem` with 1,024 pooled slots.
30    ///
31    /// # Returns
32    ///
33    /// - `Self` - Ready particle system.
34    fn default() -> Self {
35        Self::with_capacity(1024)
36    }
37}
38
39/// Particle emission, deterministic PRNG, and zero-alloc update methods for `ParticleSystem`.
40impl ParticleSystem {
41    /// Allocates a fixed-capacity `ParticleSystem` with `capacity` pre-allocated slots.
42    ///
43    /// # Arguments
44    ///
45    /// - `usize` - Maximum concurrent particles.
46    ///
47    /// # Returns
48    ///
49    /// - `Self` - Pre-allocated particle system.
50    #[must_use]
51    pub fn with_capacity(capacity: usize) -> Self {
52        let cap: usize = capacity.max(16);
53        Self {
54            pool: vec![Particle::default(); cap],
55            cursor: 0,
56            rng_state: 0x9E37_79B9_7F4A_7C15,
57            gravity: Point::new(0.0, 0.0),
58            active_count: 0,
59        }
60    }
61
62    /// Generates a deterministic pseudo-random `f32` in `[0.0, 1.0)`.
63    ///
64    /// # Arguments
65    ///
66    /// - `&Self` - Mutable particle system.
67    ///
68    /// # Returns
69    ///
70    /// - `f32` - Normalized random scalar.
71    fn next_unit_f32(&mut self) -> f32 {
72        let mut s: u64 = self.get_rng_state();
73        s = s
74            .wrapping_mul(6_364_136_223_846_793_005)
75            .wrapping_add(1_442_695_040_888_963_407);
76        self.set_rng_state(s);
77        ((s >> 40) as f32) / ((1_u32 << 24) as f32)
78    }
79
80    /// Emits `count` particles at `origin` using `config` without allocating heap memory.
81    ///
82    /// # Arguments
83    ///
84    /// - `&Self` - Mutable particle system.
85    /// - `Point` - World-space emission origin.
86    /// - `usize` - Number of particles to emit.
87    /// - `ParticleEmitterConfig` - Emission parameters.
88    pub fn emit_burst(&mut self, origin: Point, count: usize, config: ParticleEmitterConfig) {
89        let cap: usize = self.get_pool().len();
90        if cap == 0 {
91            return;
92        }
93        for _ in 0..count {
94            let r_ang: f32 = self.next_unit_f32();
95            let r_spd: f32 = self.next_unit_f32();
96            let r_life: f32 = self.next_unit_f32();
97            let angle: f32 = config.get_base_angle() + (r_ang - 0.5) * config.get_spread_rad();
98            let speed: f32 =
99                config.get_min_speed() + (config.get_max_speed() - config.get_min_speed()) * r_spd;
100            let life: f32 = (config.get_min_life()
101                + (config.get_max_life() - config.get_min_life()) * r_life)
102                .max(0.02);
103            let slot_idx: usize = self.get_cursor() % cap;
104            self.set_cursor((slot_idx + 1) % cap);
105            let p: &mut Particle = &mut self.get_mut_pool()[slot_idx];
106            p.set_active(true);
107            p.set_position(origin);
108            p.set_velocity(Point::new(angle.cos() * speed, angle.sin() * speed));
109            p.set_age(0.0);
110            p.set_lifetime(life);
111            p.set_start_size(config.get_start_size());
112            p.set_end_size(config.get_end_size());
113            p.set_start_color(config.get_start_color());
114            p.set_end_color(config.get_end_color());
115            p.set_drag(config.get_drag());
116        }
117    }
118
119    /// Advances all active particles by `dt_secs` and updates `active_count`.
120    ///
121    /// # Arguments
122    ///
123    /// - `&Self` - Mutable particle system.
124    /// - `f32` - Elapsed timestep in seconds.
125    ///
126    /// # Returns
127    ///
128    /// - `usize` - Number of particles still active after the update.
129    pub fn update(&mut self, dt_secs: f32) -> usize {
130        let gx: f32 = self.get_gravity().get_x();
131        let gy: f32 = self.get_gravity().get_y();
132        let mut live: usize = 0;
133        for p in self.get_mut_pool().iter_mut() {
134            if !p.get_active() {
135                continue;
136            }
137            let next_age: f32 = p.get_age() + dt_secs;
138            if next_age >= p.get_lifetime() {
139                p.set_active(false);
140                continue;
141            }
142            let damp: f32 = (1.0 - p.get_drag() * dt_secs).clamp(0.0, 1.0);
143            let vx: f32 = (p.get_velocity().get_x() + gx * dt_secs) * damp;
144            let vy: f32 = (p.get_velocity().get_y() + gy * dt_secs) * damp;
145            let pos: Point = p.get_position();
146            p.set_age(next_age);
147            p.set_velocity(Point::new(vx, vy));
148            p.set_position(Point::new(
149                pos.get_x() + vx * dt_secs,
150                pos.get_y() + vy * dt_secs,
151            ));
152            live += 1;
153        }
154        self.set_active_count(live);
155        live
156    }
157
158    /// Clears all active particles immediately.
159    ///
160    /// # Arguments
161    ///
162    /// - `&Self` - Mutable particle system.
163    pub fn clear(&mut self) {
164        for p in self.get_mut_pool().iter_mut() {
165            p.set_active(false);
166        }
167        self.set_active_count(0);
168    }
169}