1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use bevy::prelude::*;

use crate::state::AppState;

pub(crate) struct CleanupPlugin;

impl Plugin for CleanupPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(OnExit(AppState::InGame), cleanup);
    }
}

/// Mark all entities which should be recursively despawned after the game is
/// exited with this component.
#[derive(Component)]
pub struct DespawnOnGameExit;

fn cleanup(mut commands: Commands, query: Query<Entity, With<DespawnOnGameExit>>) {
    for entity in query.iter() {
        commands.entity(entity).despawn_recursive();
    }
}