From 0bece21a2f3450bc7f08838b9bf95ad8b5efd78a Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Sat, 10 Feb 2024 14:33:59 +0100 Subject: [PATCH] Add max particle distance --- CHANGELOG.md | 2 ++ comfy/src/particles.rs | 47 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8ec465..3fd9d87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ - Allow changing `game_config_mut().target_framerate` during gameplay. Previously this was only possible at initialization, but Comfy will now update its frame timer at the end of each frame, allowing this to be configurable at will. +- Added `max_distance` to `Particle` allowing particles to only travel a set maximum distance. +- Added `spawn_particle_fan_ex` with more flexible parameters for max particle distance. # v0.3.0 diff --git a/comfy/src/particles.rs b/comfy/src/particles.rs index 85a3f8f..9cc98f4 100644 --- a/comfy/src/particles.rs +++ b/comfy/src/particles.rs @@ -219,6 +219,9 @@ pub struct Particle { pub z_index: i32, pub size: Vec2, + pub distance_traveled: f32, + pub max_distance: Option, + pub angular_velocity: f32, pub start_time: f32, @@ -260,7 +263,17 @@ impl Particle { } pub fn update(&mut self, delta: f32) { - self.position += self.current_velocity() * delta; + let move_vec = self.current_velocity() * delta; + + if let Some(max_distance) = self.max_distance { + self.distance_traveled += move_vec.length(); + + if self.distance_traveled > max_distance { + self.lifetime_current = 0.0; + } + } + + self.position += move_vec; self.rotation += self.angular_velocity * delta; self.lifetime_current -= delta; @@ -511,6 +524,9 @@ impl Default for Particle { lifetime_current: 0.0, lifetime_max: 1.0, + max_distance: None, + distance_traveled: 0.0, + texture: texture_id("error"), source_rect: None, spritesheet: None, @@ -560,6 +576,35 @@ pub fn spawn_particle_fan( } } +pub fn spawn_particle_fan_ex( + num: i32, + dir: Vec2, + wiggle_radians: f32, + max_distance: Option, + max_distance_spread: Option, + velocity_range: Range, + map: impl Fn(Particle) -> Particle, +) { + for _ in 0..num { + let direction = dir.normalize_or_right().wiggle(wiggle_radians); + + let particle = map(Particle { + direction, + max_distance: max_distance.map(|d| { + if let Some(spread) = max_distance_spread { + gen_range(d - spread, d + spread) + } else { + d + } + }), + velocity: gen_range(velocity_range.start, velocity_range.end), + ..Default::default() + }); + + spawn_particle(particle); + } +} + // fn map_circle_point_to_rectangle( // point_in_circle: Vec2, // circle_radius: f32,