2 Commits

Author SHA1 Message Date
c0cf6d7f30 Fix: reset "get-ready" timer upon showing widget
All checks were successful
Basic checks / Basic build-and-test supertask (push) Successful in 7m21s
Closes #25: Reset "get ready" timer upon entering the get-ready state.
2025-08-14 18:20:50 -05:00
cc23e9e08e 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.
2025-08-14 14:50:12 -05:00
2 changed files with 12 additions and 9 deletions

View File

@@ -51,6 +51,7 @@ impl Plugin for AsteroidPlugin {
.init_resource::<GameAssets>() .init_resource::<GameAssets>()
.add_systems(Startup, spawn_camera) .add_systems(Startup, spawn_camera)
.add_systems(OnEnter(GameState::Playing), objects::spawn_player) .add_systems(OnEnter(GameState::Playing), objects::spawn_player)
.add_systems(OnExit(GameState::Playing), despawn::<Ship>)
.add_systems( .add_systems(
FixedUpdate, 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`). /// The game's main state tracking mechanism, registered with Bevy as a [`State`](`bevy::prelude::State`).
#[derive(Clone, Debug, Eq, Hash, PartialEq, States)] #[derive(Clone, Debug, Eq, Hash, PartialEq, States)]
pub enum GameState { pub enum GameState {

View File

@@ -3,6 +3,7 @@ use std::ops::DerefMut;
use crate::{ use crate::{
GameState, GameState,
config::{UI_BUTTON_HOVERED, UI_BUTTON_NORMAL, UI_BUTTON_PRESSED}, config::{UI_BUTTON_HOVERED, UI_BUTTON_NORMAL, UI_BUTTON_PRESSED},
despawn,
resources::{Lives, Score}, resources::{Lives, Score},
}; };
@@ -105,14 +106,6 @@ struct CountdownText;
#[derive(Component)] #[derive(Component)]
struct CountdownBar; struct CountdownBar;
/// Despawns entities matching the generic argument. Intended to remove UI
/// elements.
fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<T>>) {
for entity in to_despawn {
commands.entity(entity).despawn();
}
}
/// Utility function for creating a standard button. /// Utility function for creating a standard button.
fn button_bundle(text: &str) -> impl Bundle { fn button_bundle(text: &str) -> impl Bundle {
( (
@@ -169,7 +162,8 @@ fn spawn_menu(mut commands: Commands) {
}); });
} }
fn spawn_get_ready(mut commands: Commands) { fn spawn_get_ready(mut commands: Commands, mut timer: ResMut<ReadySetGoTimer>) {
timer.reset();
commands.spawn(( commands.spawn((
OnReadySetGo, // marker, so this can be de-spawned properly OnReadySetGo, // marker, so this can be de-spawned properly
Node { Node {