-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ParticleSystem::oneshot and example (#57)
Co-authored-by: Abnormal Brain Studios <[email protected]>
- Loading branch information
1 parent
bb6cc3a
commit 7298b1f
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! This example shows how particle systems can be spawned dynamically and | ||
//! automatically despawned when finished. | ||
use bevy::{input::common_conditions::input_just_pressed, prelude::*, window::PrimaryWindow}; | ||
use bevy_particle_systems::{ | ||
ParticleBurst, ParticleSystem, ParticleSystemBundle, ParticleSystemPlugin, Playing, | ||
VelocityModifier, | ||
}; | ||
|
||
fn main() { | ||
App::new() | ||
.insert_resource(ClearColor(Color::BLACK)) | ||
.add_plugins((DefaultPlugins, ParticleSystemPlugin)) | ||
.add_systems(Startup, setup) | ||
.add_systems( | ||
Update, | ||
spawn_particle_systems.run_if(input_just_pressed(MouseButton::Left)), | ||
) | ||
.run(); | ||
} | ||
|
||
fn setup(mut commands: Commands) { | ||
commands.spawn(Camera2dBundle::default()); | ||
} | ||
|
||
fn spawn_particle_systems( | ||
mut commands: Commands, | ||
window: Query<&Window, With<PrimaryWindow>>, | ||
camera: Query<(&Camera, &GlobalTransform)>, | ||
) { | ||
let (camera, camera_transform) = camera.single(); | ||
|
||
if let Some(world_position) = window | ||
.single() | ||
.cursor_position() | ||
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor)) | ||
.map(|ray| ray.origin.truncate()) | ||
{ | ||
commands.spawn(( | ||
ParticleSystemBundle { | ||
transform: Transform::from_translation(world_position.extend(0.)), | ||
particle_system: ParticleSystem { | ||
spawn_rate_per_second: 0.0.into(), | ||
max_particles: 1_000, | ||
initial_speed: (0.0..300.0).into(), | ||
scale: 2.0.into(), | ||
velocity_modifiers: vec![ | ||
VelocityModifier::Drag(0.001.into()), | ||
VelocityModifier::Vector(Vec3::new(0.0, -400.0, 0.0).into()), | ||
], | ||
color: (Color::BLUE..Color::rgba(1.0, 0.0, 0.0, 0.0)).into(), | ||
bursts: vec![ParticleBurst { | ||
time: 0.0, | ||
count: 1000, | ||
}], | ||
..ParticleSystem::oneshot() | ||
}, | ||
..default() | ||
}, | ||
Playing, | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters