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

Commit

Permalink
Better particle systems example
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Sep 21, 2023
1 parent 7c60f14 commit 83dec5f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# EXAMPLE=post_processing
# EXAMPLE=shapes
# EXAMPLE=ecs_sprite
EXAMPLE=ecs_topdown_game
# EXAMPLE=particle_systems
# EXAMPLE=ecs_topdown_game
EXAMPLE=particle_systems

# default: build-examples
# default: wasm-build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ comfy is free and open source and dual licensed under MIT and Apache 2.0 license
- [ ] lighting example
- [ ] combat text example
- [ ] font loading
- [ ] particle systems
- [x] particle systems
- [x] text
- [ ] blood canvas
- [ ] raytracing
Expand Down
43 changes: 41 additions & 2 deletions comfy/examples/particle_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,54 @@ use comfy::*;
simple_game!("Particle Systems Example", setup, update);

fn setup(c: &mut EngineContext) {
c.load_texture_from_bytes(
// Every texture gets a string name later used to reference it.
"comfy",
include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../assets/comfy.png"
)),
wgpu::AddressMode::ClampToEdge,
);

// ParticleSystem's are based off the same principle as singular Particle's, except the
// automatically handle the spawning.

c.commands().spawn((
ParticleSystem::with_spawn_rate(100, 0.1, || {
ParticleSystem::with_spawn_rate(500, 0.01, || {
Particle {
texture: texture_id("comfy"),
position: random_circle(5.0),
size: splat(1.0),
// Other than the builtin easing curves, any f32 -> f32 curve
// will work.
size_curve: quad_in_out,

direction: vec2(0.0, 1.0),
velocity: 10.0,
velocity_end: 5.0,
..Default::default()
}
})
.with_size(vec2(3.0, 8.0)),
Transform::position(vec2(-5.0, 0.0)),
));

c.commands().spawn((
ParticleSystem::with_spawn_on_death(300, || {
Particle {
texture: texture_id("comfy"),
position: random_circle(5.0),
size: splat(0.5),
size_curve: expo_out,
// Both size and color can be faded.
fade_type: FadeType::Both,
color_start: RED,
color_end: RED,
..Default::default()
}
}),
Transform::position(Vec2::ZERO),
Transform::position(vec2(5.0, 0.0)),
));
}

Expand Down
3 changes: 3 additions & 0 deletions comfy/src/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ impl ParticleSystem {
}
}

/// Creates a particle system that always maintains a `max_particles` number of live particles.
/// Every time a particle's lifetime ends, a new particle is immediately spawned to maintain a
/// constant number.
pub fn with_spawn_on_death(
max_particles: usize,
particle_builder: impl Fn() -> Particle + Send + Sync + 'static,
Expand Down

0 comments on commit 83dec5f

Please sign in to comment.