#bevy #cleanup #gamedev #derive #component #pattern #design

bevy_cleanup_derive

为 bevy_cleanup 提供派生宏

1 个不稳定版本

0.1.0 2023 年 8 月 29 日

#58#cleanup


用于 bevy_cleanup

MIT/Apache

5KB

bevy_cleanup

crates.io docs.rs

为 Bevy 提供了使用清理设计模式的辅助工具,其中实体在状态转换后自动移除,取决于它们拥有的任何 Cleanup 标记组件。

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);
}

许可证

以下任一许可证下发行:

任选其一。

除非您明确声明,否则根据 Apache-2.0 许可证定义的,您有意提交以包含在工作中的任何贡献,都将如上所述双许可,而不附加任何额外的条款或条件。

依赖项

~270–720KB
~17K SLoC