Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Add max particle distance
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Feb 10, 2024
1 parent 33ca543 commit 0bece21
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 46 additions & 1 deletion comfy/src/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ pub struct Particle {
pub z_index: i32,
pub size: Vec2,

pub distance_traveled: f32,
pub max_distance: Option<f32>,

pub angular_velocity: f32,

pub start_time: f32,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<f32>,
max_distance_spread: Option<f32>,
velocity_range: Range<f32>,
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,
Expand Down

0 comments on commit 0bece21

Please sign in to comment.