Skip to content

Commit

Permalink
fixed crash when setting particle spawn rate too high
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jan 27, 2024
1 parent daec700 commit 319940a
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/scene/particle_system/emitter/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,20 @@ impl BaseEmitter {
pub fn tick(&mut self, dt: f32) {
self.time += dt;
let time_amount_per_particle = 1.0 / self.particle_spawn_rate as f32;
let mut particle_count = (self.time / time_amount_per_particle) as u32;
self.time -= time_amount_per_particle * particle_count as f32;
self.particles_to_spawn = (self.time / time_amount_per_particle) as u32;
self.time -= time_amount_per_particle * self.particles_to_spawn as f32;
if let Some(max_particles) = self.max_particles {
let alive_particles = self.alive_particles;
if alive_particles < max_particles && alive_particles + particle_count > max_particles {
particle_count = max_particles - particle_count;
if alive_particles < max_particles
&& alive_particles + self.particles_to_spawn > max_particles
{
self.particles_to_spawn = max_particles.saturating_sub(alive_particles);
}
if !self.resurrect_particles && self.spawned_particles > u64::from(max_particles) {
self.particles_to_spawn = 0;
return;
}
}
self.particles_to_spawn = particle_count;
self.spawned_particles += self.particles_to_spawn as u64;
}

Expand Down

0 comments on commit 319940a

Please sign in to comment.