From 54ef257ab457da933e4f3e6aba99c653932e2853 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Wed, 13 Aug 2025 11:21:49 -0500 Subject: [PATCH] Make generic UI despawning function --- src/widgets.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/widgets.rs b/src/widgets.rs index ecbae9b..6408ecc 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -14,7 +14,7 @@ pub struct PluginGameMenu; impl Plugin for PluginGameMenu { fn build(&self, app: &mut App) { app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu) - .add_systems(OnExit(GameState::TitleScreen), despawn_menu) + .add_systems(OnExit(GameState::TitleScreen), despawn::) .add_systems( Update, handle_spacebar.run_if(in_state(GameState::TitleScreen)), @@ -28,7 +28,7 @@ pub struct PluginGetReady; impl Plugin for PluginGetReady { fn build(&self, app: &mut App) { app.add_systems(OnEnter(GameState::GetReady), spawn_get_ready) - .add_systems(OnExit(GameState::GetReady), despawn_get_ready) + .add_systems(OnExit(GameState::GetReady), despawn::) .add_systems( Update, (animate_get_ready_widget).run_if(in_state(GameState::GetReady)), @@ -62,6 +62,14 @@ struct CountdownText; #[derive(Component)] struct CountdownBar; +/// Despawns entities matching the generic argument. Intended to remove UI +/// elements. +fn despawn(mut commands: Commands, to_despawn: Query>) { + for entity in to_despawn { + commands.entity(entity).despawn(); + } +} + fn spawn_get_ready(mut commands: Commands) { commands.spawn(( OnReadySetGo, // marker, so this can be de-spawned properly @@ -96,14 +104,6 @@ fn spawn_get_ready(mut commands: Commands) { )); } -// TODO: Replace this with a generic somewhere else in the crate -// want: `despawn_screen::>()` -fn despawn_get_ready(mut commands: Commands, to_despawn: Query>) { - for entity in to_despawn { - commands.entity(entity).despawn(); - } -} - fn animate_get_ready_widget( mut text_segment: Single<&mut Text, With>, mut bar_segment: Single<&mut Node, With>, @@ -155,12 +155,6 @@ fn spawn_menu(mut commands: Commands) { }); } -fn despawn_menu(mut commands: Commands, to_despawn: Query>) { - for entity in &to_despawn { - commands.entity(entity).despawn(); - } -} - fn handle_spacebar(input: Res>, mut game_state: ResMut>) { if input.just_pressed(KeyCode::Space) { game_state.set(GameState::GetReady);