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

bevy_despawn_particles

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

10 releases

0.1.0 Mar 17, 2024
0.1.0-pre.9 Feb 19, 2024
0.1.0-pre.5 Nov 9, 2023
0.1.0-pre.3 Oct 22, 2023
0.1.0-pre.1 Jul 31, 2023

#161 in Game dev

Download history 6/week @ 2024-02-05 24/week @ 2024-02-12 403/week @ 2024-02-19 49/week @ 2024-02-26 100/week @ 2024-03-11 41/week @ 2024-03-18 260/week @ 2024-04-01

401 downloads per month

Unlicense

77KB
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)
        .add_plugin(DespawnParticlesPlugin)
        .add_system(setup.on_startup())
        .add_system(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

~39–83MB
~1.5M SLoC