#bevy #bevy-plugin #graphics #gamedev #game-engine #2d-graphics #entity-component

bevy_despawn_particles

An event-based plugin for Bevy to spawn particles on despawn that are built from the original texture

13 releases

new 0.3.1 Feb 9, 2025
0.3.0 Feb 8, 2025
0.2.0 Jul 17, 2024
0.1.0 Mar 17, 2024
0.1.0-pre.1 Jul 31, 2023

#140 in Game dev

Download history 2/week @ 2024-10-29 3/week @ 2024-11-05 1/week @ 2024-12-03 5/week @ 2024-12-10 171/week @ 2025-02-04

171 downloads per month

Unlicense

78KB
1K SLoC

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.
works

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
fade

cargo run --release --example shrink
In this example the particles are stationary and just shrink in place
shrink
cargo run --release --example velocity
In this example the particles shoot outwards from the sprite
velocity
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
Screencast from 2023-07-19 20-55-22

Dependencies

~42–75MB
~1.5M SLoC