bevy_cleanup 0.1.0

Cleanup design pattern tools for Bevy
Documentation
  • Coverage
  • 30.77%
    4 out of 13 items documented2 out of 9 items with examples
  • Size
  • Source code size: 55.24 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.27 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 1m 34s Average build duration of successful builds.
  • all releases: 1m 34s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • aecsocket/bevy_cleanup
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • aecsocket

bevy_cleanup

crates.io docs.rs

Providers helpers for using the cleanup design pattern in Bevy, where entities are automatically removed after a state transition, depending on any Cleanup marker components they have.

use bevy::prelude::*;
use bevy_cleanup::{Cleanup, AddStateCleanup};

// Set up your States enum and keep your Cleanup component types close by

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, States)]
enum AppState {
    #[default]
    Menu,
    Game,
}

#[derive(Component, Cleanup)]
struct CleanupMenu;

#[derive(Component, Cleanup)]
struct CleanupGame;

// Set up your App

fn main() {
    App::new()
        .add_state::<AppState>()
        .add_state_cleanup::<_, CleanupMenu>(AppState::Menu)
        .add_state_cleanup::<_, CleanupGame>(AppState::Game)
        .add_systems(OnEnter(AppState::Menu), setup_menu)
        .run();
}

fn setup_menu(mut commands: Commands) {
    // When spawning an entity, give it one of your `Cleanup` component types
    // Typically, you put the Name and Cleanup types at the top of the component tuple
    commands.spawn((
        Name::new("Menu entity"),
        CleanupMenu,
        // everything else...
    ));
}

// When you want to switch from Menu to Game...
fn switch_to_game(mut next_state: ResMut<NextState<AppState>>) {
    // After this, any entities with the cleanup component of the current state (`CleanupMenu`)
    // will be automatically recursively despawned.
    next_state.set(AppState::Game);
}

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.