An event-based plugin for the Bevy game engine that provides a simple way to add a despawn effect for 2D sprites.
Contains a basic physics implementation or a feature for bevy_rapier integration.
use bevy::prelude::*;
use bevy_despawn_particles::prelude::*;
#[derive(Component, Default)]
pub struct Marker;
fn main() {
App::new()
.add_plugins((DefaultPlugins, DespawnParticlesPlugin))
.add_systems(Startup, setup)
.add_systems(Update, despawn)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands
.spawn(SpriteBundle {
texture: asset_server.load("asteroid_round.png"),
..default()
})
.insert(Marker);
}
fn despawn(
mut despawn_particles_event_writer: EventWriter<DespawnParticlesEvent>,
entities: Query<Entity, Added<Marker>>,
) {
if let Ok(entity) = entities.get_single() {
despawn_particles_event_writer.send(
DespawnParticlesEvent::builder()
.with_fade(true) // The particles will fade as they get closer to expiration
.with_shrink(true) // The particles will shrink as they get closer to expiration
.with_linvel(150.0..300.0) // Random velocity between 150.0 and 300.0
.with_angvel([-5.0, -2.5, 2.5, 5.0]) // Random angular velocity from the given list
.with_mass(1.0) // Always 1.0
.with_lifetime(0.3..1.0) // Random lifetime between 0.3 and 1.0
.with_angular_damping(1.0) // Always 1.0, angular 'friction' that decelerates the particle
.with_linear_damping(1.0) // Always 1.0, linear 'friction' that decelerates the particle
.build(entity),
);
}
}
Examples
All the following examples can be found in the examples directory of this repository.
cargo run --release --example the_works |
This example utilizes most of the parameters available. The particles fade and shrink, have a mass affected by gravity, shoot outwards and have some amount of angular velocity, and dampening. |
|
cargo run --release --example fade |
In this example the particles are stationary and just fade, giving the visual effect of the entire sprite just fading away |
|
cargo run --release --example shrink |
In this example the particles are stationary and just shrink in place |
|
cargo run --release --example velocity |
In this example the particles shoot outwards from the sprite |
|
cargo run --release --example mesh |
Can be utilized on a Mesh. Also includes usage of the faux circle mesh to replace the arguably unappealing triangles that typically make up a Cirlcle mesh |
|