diff --git a/Makefile b/Makefile index ae5fb45..3e14879 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index cda8eb6..1b05130 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/comfy/examples/particle_systems.rs b/comfy/examples/particle_systems.rs index c399438..3bc9cb8 100644 --- a/comfy/examples/particle_systems.rs +++ b/comfy/examples/particle_systems.rs @@ -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)), )); } diff --git a/comfy/src/particles.rs b/comfy/src/particles.rs index c9b8e39..9fef69b 100644 --- a/comfy/src/particles.rs +++ b/comfy/src/particles.rs @@ -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,