1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! A simple particle system for bursts and impacts — one straight-line
//! velocity per particle, no gravity or collision.
use crate::buffer::{Buffer, Cell};
use crossterm::style::Color;
use std::time::Duration;
/// One glyph moving in a straight line with a finite lifetime.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Particle {
/// Current x position (cells).
pub x: f32,
/// Current y position (cells).
pub y: f32,
/// x velocity (cells/second).
pub vx: f32,
/// y velocity (cells/second).
pub vy: f32,
/// Glyph to render.
pub symbol: char,
/// Foreground color.
pub color: Color,
/// Total time the particle lives before despawning.
pub lifetime: Duration,
/// Time elapsed since spawn.
pub age: Duration,
}
impl Particle {
/// Whether this particle is still within its `lifetime`.
pub fn is_alive(&self) -> bool {
self.age < self.lifetime
}
}
/// A collection of `Particle`s: spawn, `update` each tick, `render`
/// each frame; dead particles are dropped automatically.
#[derive(Clone, Debug, Default)]
pub struct ParticleSystem {
particles: Vec<Particle>,
}
impl ParticleSystem {
/// Creates an empty particle system.
pub fn new() -> Self {
ParticleSystem::default()
}
/// Adds a particle to the system.
pub fn spawn(&mut self, p: Particle) {
self.particles.push(p);
}
/// Advances every particle's position and age by `elapsed`,
/// dropping any that have expired.
pub fn update(&mut self, elapsed: Duration) {
let dt = elapsed.as_secs_f32();
for p in &mut self.particles {
p.x += p.vx * dt;
p.y += p.vy * dt;
p.age += elapsed;
}
self.particles.retain(|p| p.is_alive());
}
/// Draws every particle's glyph at its rounded position, skipping
/// any that have moved out of bounds.
pub fn render(&self, buf: &mut Buffer) {
for p in &self.particles {
let x = p.x.round();
let y = p.y.round();
if x >= 0.0 && y >= 0.0 && (x as u16) < buf.width && (y as u16) < buf.height {
buf.set(
x as u16,
y as u16,
Cell {
symbol: p.symbol,
fg: p.color,
bg: Color::Reset,
alpha: 1.0,
..Default::default()
},
);
}
}
}
/// Number of currently-alive particles.
pub fn len(&self) -> usize {
self.particles.len()
}
/// Whether there are no currently-alive particles.
pub fn is_empty(&self) -> bool {
self.particles.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn particle_system_new_is_empty() {
let ps = ParticleSystem::new();
assert_eq!(ps.len(), 0);
assert!(ps.is_empty());
}
#[test]
fn spawn_increases_len_by_one() {
let mut ps = ParticleSystem::new();
let p = Particle {
x: 0.0,
y: 0.0,
vx: 0.0,
vy: 0.0,
symbol: '*',
color: Color::Red,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p);
assert_eq!(ps.len(), 1);
}
#[test]
fn update_moves_particle_by_velocity_times_elapsed() {
let mut ps = ParticleSystem::new();
let p = Particle {
x: 0.0,
y: 0.0,
vx: 2.0,
vy: 0.0,
symbol: '*',
color: Color::Red,
lifetime: Duration::from_millis(1000),
age: Duration::ZERO,
};
ps.spawn(p);
ps.update(Duration::from_millis(500));
// With vx=2.0 and elapsed=500ms=0.5s, x should advance by 2.0 * 0.5 = 1.0
// We need to check through render or by accessing particles directly
// Since particles is private, we'll check via rendering behavior
let mut buf = Buffer::new(10, 10);
ps.render(&mut buf);
// The particle should be at x=1.0 (rounded to 1), y=0.0
assert_eq!(
*buf.get(1, 0),
Cell {
symbol: '*',
fg: Color::Red,
bg: Color::Reset,
alpha: 1.0,
..Default::default()
}
);
}
#[test]
fn update_accumulates_position_across_multiple_calls() {
let mut ps = ParticleSystem::new();
let p = Particle {
x: 0.0,
y: 0.0,
vx: 2.0,
vy: 0.0,
symbol: '*',
color: Color::Red,
lifetime: Duration::from_secs(10),
age: Duration::ZERO,
};
ps.spawn(p);
ps.update(Duration::from_millis(500));
ps.update(Duration::from_millis(500));
// Two 500ms updates at vx=2.0 should accumulate to x = 2.0*0.5 + 2.0*0.5
// = 2.0, not stay at 1.0 (which a buggy `p.x = p.vx * dt` assignment
// instead of `p.x += p.vx * dt` accumulation would produce).
let mut buf = Buffer::new(10, 10);
ps.render(&mut buf);
assert_eq!(
*buf.get(2, 0),
Cell {
symbol: '*',
fg: Color::Red,
bg: Color::Reset,
alpha: 1.0,
..Default::default()
}
);
assert_eq!(ps.len(), 1);
}
#[test]
fn update_accumulates_age_across_multiple_calls_until_expiry() {
let mut ps = ParticleSystem::new();
let p = Particle {
x: 0.0,
y: 0.0,
vx: 0.0,
vy: 0.0,
symbol: '*',
color: Color::Red,
lifetime: Duration::from_millis(150),
age: Duration::ZERO,
};
ps.spawn(p);
ps.update(Duration::from_millis(100));
assert_eq!(ps.len(), 1); // 100ms < 150ms lifetime, still alive
ps.update(Duration::from_millis(100));
// Total age = 200ms > 150ms lifetime — only correct if age accumulates
// across calls (100+100=200) rather than resetting each call.
assert_eq!(ps.len(), 0);
}
#[test]
fn update_ages_particle_and_removes_when_age_exceeds_lifetime() {
let mut ps = ParticleSystem::new();
let p = Particle {
x: 0.0,
y: 0.0,
vx: 0.0,
vy: 0.0,
symbol: '*',
color: Color::Red,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p);
assert_eq!(ps.len(), 1);
ps.update(Duration::from_millis(150));
assert_eq!(ps.len(), 0);
}
#[test]
fn update_retains_particles_with_age_less_than_lifetime() {
let mut ps = ParticleSystem::new();
let p = Particle {
x: 0.0,
y: 0.0,
vx: 0.0,
vy: 0.0,
symbol: '*',
color: Color::Red,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p);
assert_eq!(ps.len(), 1);
ps.update(Duration::from_millis(50));
assert_eq!(ps.len(), 1);
}
#[test]
fn render_writes_particle_to_buffer_at_rounded_position() {
let mut ps = ParticleSystem::new();
let p = Particle {
x: 3.4,
y: 2.6,
vx: 0.0,
vy: 0.0,
symbol: 'X',
color: Color::Blue,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p);
let mut buf = Buffer::new(10, 10);
ps.render(&mut buf);
// x=3.4 rounds to 3, y=2.6 rounds to 3
assert_eq!(
*buf.get(3, 3),
Cell {
symbol: 'X',
fg: Color::Blue,
bg: Color::Reset,
alpha: 1.0,
..Default::default()
}
);
}
#[test]
fn render_skips_particle_outside_buffer_bounds() {
let mut ps = ParticleSystem::new();
// Particle at negative x
let p1 = Particle {
x: -1.0,
y: 0.0,
vx: 0.0,
vy: 0.0,
symbol: 'A',
color: Color::Red,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p1);
// Particle at x >= width
let p2 = Particle {
x: 10.0,
y: 0.0,
vx: 0.0,
vy: 0.0,
symbol: 'B',
color: Color::Red,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p2);
// Particle at negative y
let p3 = Particle {
x: 0.0,
y: -1.0,
vx: 0.0,
vy: 0.0,
symbol: 'C',
color: Color::Red,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p3);
// Particle at y >= height
let p4 = Particle {
x: 0.0,
y: 10.0,
vx: 0.0,
vy: 0.0,
symbol: 'D',
color: Color::Red,
lifetime: Duration::from_millis(100),
age: Duration::ZERO,
};
ps.spawn(p4);
let mut buf = Buffer::new(10, 10);
ps.render(&mut buf);
// Buffer should still be all default cells
for y in 0..10 {
for x in 0..10 {
assert_eq!(*buf.get(x, y), Cell::default());
}
}
}
}