From 704a4476f0f323b49fb25a59fde610f02dbfcf48 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Mon, 4 May 2026 22:14:58 +0200 Subject: [PATCH] fix: remove orphaned duplicate code in cleanup_game_entities --- src/game_state.rs | 93 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/src/game_state.rs b/src/game_state.rs index 111a078..8cf8b1c 100644 --- a/src/game_state.rs +++ b/src/game_state.rs @@ -1,5 +1,6 @@ +use crate::components::{Bullet, Enemy, GameOverUI, RestartMessage, StartButton, StartMenuUI}; +use crate::resources::RestartPressed; use bevy::prelude::*; -use crate::components::{Bullet, Enemy, GameOverUI, StartMenuUI, StartButton}; // Import necessary components // --- Game States --- #[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)] @@ -27,19 +28,43 @@ pub fn setup_game_over_ui(mut commands: Commands) { position_type: PositionType::Absolute, align_self: AlignSelf::Center, justify_self: JustifySelf::Center, - top: Val::Percent(40.0), // Center vertically roughly + top: Val::Percent(40.0), ..default() }), - GameOverUI, // Tag the UI element + GameOverUI, + )); + commands.spawn(( + TextBundle::from_section( + "Press R to Restart", + TextStyle { + font_size: 32.0, + color: Color::WHITE, + ..default() + }, + ) + .with_style(Style { + position_type: PositionType::Absolute, + align_self: AlignSelf::Center, + justify_self: JustifySelf::Center, + top: Val::Percent(55.0), + ..default() + }), + RestartMessage, )); - // TODO: Add "Press R to Restart" text later } -pub fn cleanup_game_over_ui(mut commands: Commands, query: Query>) { +pub fn cleanup_game_over_ui( + mut commands: Commands, + query: Query>, + restart_query: Query>, +) { println!("Exiting GameOver state. Cleaning up UI."); for entity in query.iter() { commands.entity(entity).despawn_recursive(); } + for entity in restart_query.iter() { + commands.entity(entity).despawn_recursive(); + } } // --- Cleanup --- @@ -48,8 +73,9 @@ pub fn cleanup_game_over_ui(mut commands: Commands, query: Query>, - enemy_bullet_query: Query>, // Need to specify crate::components + enemy_bullet_query: Query>, enemy_query: Query>, + restart_message_query: Query>, // Optionally despawn player too, or handle separately if needed for restart // player_query: Query>, ) { @@ -57,12 +83,15 @@ pub fn cleanup_game_entities( for entity in bullet_query.iter() { commands.entity(entity).despawn(); } - for entity in enemy_bullet_query.iter() { // Also despawn enemy bullets + for entity in enemy_bullet_query.iter() { commands.entity(entity).despawn(); } for entity in enemy_query.iter() { commands.entity(entity).despawn(); } + for entity in restart_message_query.iter() { + commands.entity(entity).despawn(); + } // for entity in player_query.iter() { // commands.entity(entity).despawn(); // } @@ -72,7 +101,7 @@ pub fn cleanup_game_entities( pub fn setup_start_menu_ui(mut commands: Commands) { println!("Entering StartMenu state. Setting up UI."); - + // Root UI container commands .spawn(( @@ -91,18 +120,21 @@ pub fn setup_start_menu_ui(mut commands: Commands) { )) .with_children(|parent| { // Title - parent.spawn(TextBundle::from_section( - "BGLGA", - TextStyle { - font_size: 120.0, - color: Color::WHITE, + parent.spawn( + TextBundle::from_section( + "BGLGA", + TextStyle { + font_size: 120.0, + color: Color::WHITE, + ..default() + }, + ) + .with_style(Style { + margin: UiRect::bottom(Val::Px(50.0)), ..default() - }, - ).with_style(Style { - margin: UiRect::bottom(Val::Px(50.0)), - ..default() - })); - + }), + ); + // Start Game Button parent .spawn(( @@ -162,4 +194,25 @@ pub fn start_menu_button_system( } } } -} \ No newline at end of file +} + +pub fn handle_restart_input( + mut keyboard_input: ResMut>, + mut restart_resource: ResMut, + mut app_state: ResMut>, +) { + if keyboard_input.just_pressed(KeyCode::R) { + restart_resource.pressed = true; + } +} + +pub fn restart_game_system( + mut app_state: ResMut>, + mut restart_resource: ResMut, +) { + if restart_resource.pressed { + println!("Restart requested. Transitioning to Playing state."); + restart_resource.pressed = false; + app_state.set(AppState::Playing); + } +}