Despawn the player when exiting the Playing state
Closes #21: Despawn player ship when exiting `GameState::Playing` I've also moved the `fn despawn<T>` utility to the lib.rs module.
This commit is contained in:
@@ -51,6 +51,7 @@ impl Plugin for AsteroidPlugin {
|
||||
.init_resource::<GameAssets>()
|
||||
.add_systems(Startup, spawn_camera)
|
||||
.add_systems(OnEnter(GameState::Playing), objects::spawn_player)
|
||||
.add_systems(OnExit(GameState::Playing), despawn::<Ship>)
|
||||
.add_systems(
|
||||
FixedUpdate,
|
||||
(
|
||||
@@ -84,6 +85,14 @@ impl Plugin for AsteroidPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
/// Despawns entities matching the generic argument. Intended to remove UI
|
||||
/// elements.
|
||||
pub(crate) fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<T>>) {
|
||||
for entity in to_despawn {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
/// The game's main state tracking mechanism, registered with Bevy as a [`State`](`bevy::prelude::State`).
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq, States)]
|
||||
pub enum GameState {
|
||||
|
||||
Reference in New Issue
Block a user