Skip to main content

ssruntime/eval/effect/
state.rs

1use super::types::*;
2use alloc::vec::Vec;
3
4#[derive(Clone, Debug, Default)]
5pub struct ParticleStateSoA {
6    pub id: Vec<i32>,
7    pub pid: Vec<i32>,
8    pub cycle: Vec<i32>,
9    pub exist: Vec<bool>,
10    pub born: Vec<bool>,
11    pub stime: Vec<i32>,
12    pub endtime: Vec<i32>,
13}
14
15impl ParticleStateSoA {
16    pub fn resize(&mut self, size: usize) {
17        self.id.resize(size, 0);
18        self.pid.resize(size, 0);
19        self.cycle.resize(size, 0);
20        self.exist.resize(size, false);
21        self.born.resize(size, false);
22        self.stime.resize(size, 0);
23        self.endtime.resize(size, 0);
24    }
25
26    pub fn reset(&mut self) {
27        self.id.fill(0);
28        self.pid.fill(0);
29        self.cycle.fill(0);
30        self.exist.fill(false);
31        self.born.fill(false);
32        self.stime.fill(0);
33        self.endtime.fill(0);
34    }
35
36    pub fn cleanup(&mut self, index: usize) {
37        self.id[index] = 0;
38        self.pid[index] = 0;
39        self.cycle[index] = 0;
40        self.exist[index] = false;
41        self.born[index] = false;
42        self.stime[index] = 0;
43        self.endtime[index] = 0;
44    }
45}
46
47#[derive(Clone, Debug, Default)]
48pub struct EmitterState {
49    pub seed_offset: i32,
50    pub position: Vector2,
51    pub particle_state: ParticleStateSoA,
52}
53
54impl EmitterState {
55    pub fn new(emitmax: i32) -> Self {
56        let mut state = Self::default();
57        state.particle_state.resize(emitmax as usize);
58        state
59    }
60
61    pub fn reset(&mut self) {
62        self.seed_offset = 0;
63        self.position = Vector2::default();
64        self.particle_state.reset();
65    }
66}
67
68#[derive(Clone, Debug, Default)]
69pub struct RenderState {
70    pub current_frame: f32,
71    pub target_frame: f32,
72    pub seed_offset: i32,
73    pub is_playing: bool,
74    pub is_pausing: bool,
75    pub is_loop: bool,
76    pub emitter_states: Vec<EmitterState>,
77}
78
79impl RenderState {
80    pub fn reset(&mut self) {
81        self.current_frame = 0.0;
82        self.target_frame = 0.0;
83        self.seed_offset = 0;
84        self.is_playing = true;
85        self.is_pausing = false;
86        self.is_loop = false;
87        for s in &mut self.emitter_states {
88            s.reset();
89        }
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_particle_state_soa_resize() {
99        let mut state = ParticleStateSoA::default();
100        state.resize(5);
101        assert_eq!(state.id.len(), 5);
102        assert_eq!(state.cycle.len(), 5);
103        assert_eq!(state.exist.len(), 5);
104        assert_eq!(state.born.len(), 5);
105        assert_eq!(state.stime.len(), 5);
106        assert_eq!(state.endtime.len(), 5);
107    }
108
109    #[test]
110    fn test_particle_state_soa_cleanup() {
111        let mut state = ParticleStateSoA::default();
112        state.resize(2);
113
114        state.id[1] = 42;
115        state.cycle[1] = 1;
116        state.exist[1] = true;
117        state.born[1] = true;
118        state.stime[1] = 10;
119        state.endtime[1] = 20;
120
121        state.cleanup(1);
122
123        assert_eq!(state.id[1], 0);
124        assert_eq!(state.cycle[1], 0);
125        assert!(!state.exist[1]);
126        assert!(!state.born[1]);
127        assert_eq!(state.stime[1], 0);
128        assert_eq!(state.endtime[1], 0);
129
130        // Ensure index 0 is unaffected
131        assert_eq!(state.id[0], 0);
132        assert!(!state.exist[0]);
133    }
134
135    #[test]
136    fn test_emitter_state_new() {
137        let state = EmitterState::new(10);
138        assert_eq!(state.particle_state.id.len(), 10);
139        assert_eq!(state.seed_offset, 0);
140    }
141}