From d15b96ef48106611d4a1d9874834f50d710e059a Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Wed, 13 Aug 2025 17:00:29 -0500 Subject: [PATCH] Add "start" and "quit" buttons to title screen I'm stealing the game-over plugin's button handling for use in the main menu. I guess it turns out that my widgets are more generic than intended. --- src/lib.rs | 2 +- src/widgets.rs | 81 +++++++++++++++++++++++--------------------------- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 16e78ab..fd22231 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,7 +81,7 @@ impl Plugin for AsteroidPlugin { .add_event::() .add_event::() .add_event::(); - app.insert_state(GameState::GameOver); + app.insert_state(GameState::TitleScreen); } } diff --git a/src/widgets.rs b/src/widgets.rs index d7482f7..2164edb 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -47,7 +47,7 @@ impl Plugin for PluginGameOver { .add_systems(OnExit(GameState::GameOver), despawn::) .add_systems( Update, - operate_gameover_ui.run_if(in_state(GameState::GameOver)), + operate_gameover_ui //.run_if(in_state(GameState::GameOver)), ); } } @@ -72,6 +72,7 @@ struct MarkerGameOver; #[derive(Component)] enum GameOverMenuAction { ToMainMenu, + StartGame, Quit, } @@ -95,6 +96,31 @@ fn despawn(mut commands: Commands, to_despawn: Query impl Bundle { + ( + Button, + // TODO: Generic action + Node { + width: Val::Px(150.0), + height: Val::Px(65.0), + border: UiRect::all(Val::Px(2.0)), + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + margin: UiRect::all(Val::Px(5.0)), + ..default() + }, + BorderColor(Color::BLACK), + BorderRadius::MAX, + BackgroundColor(UI_BUTTON_NORMAL), + children![( + Text::new(text), + TextColor(Color::srgb(0.9, 0.9, 0.9)), + TextShadow::default(), + )], + ) +} + fn spawn_menu(mut commands: Commands) { commands .spawn(( @@ -121,6 +147,12 @@ fn spawn_menu(mut commands: Commands) { TextColor(Color::srgb(0.7, 0.7, 0.7)), TextShadow::default(), )); + cmds.spawn(( + button_bundle("Start Game"), GameOverMenuAction::StartGame + )); + cmds.spawn(( + button_bundle("Quit"), GameOverMenuAction::Quit + )); }); } @@ -173,48 +205,8 @@ fn spawn_gameover_ui(mut commands: Commands) { ..default() }, children![ - ( - Button, - GameOverMenuAction::ToMainMenu, - Node { - width: Val::Px(150.0), - height: Val::Px(65.0), - border: UiRect::all(Val::Px(2.0)), - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - margin: UiRect::all(Val::Px(5.0)), - ..default() - }, - BorderColor(Color::BLACK), - BorderRadius::MAX, - BackgroundColor(UI_BUTTON_NORMAL), - children![( - Text::new("Main Menu"), - TextColor(Color::srgb(0.9, 0.9, 0.9)), - TextShadow::default(), - )] - ), - ( - Button, - GameOverMenuAction::Quit, - Node { - width: Val::Px(150.0), - height: Val::Px(65.0), - border: UiRect::all(Val::Px(2.0)), - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - margin: UiRect::all(Val::Px(5.0)), - ..default() - }, - BorderColor(Color::BLACK), - BorderRadius::MAX, - BackgroundColor(UI_BUTTON_NORMAL), - children![( - Text::new("Quit"), - TextColor(Color::srgb(0.9, 0.9, 0.9)), - TextShadow::default(), - )] - ) + (button_bundle("Main Menu"), GameOverMenuAction::ToMainMenu,), + (button_bundle("Quit"), GameOverMenuAction::Quit), ], )); } @@ -273,6 +265,9 @@ fn operate_gameover_ui( GameOverMenuAction::ToMainMenu => { game_state.set(GameState::TitleScreen); } + GameOverMenuAction::StartGame => { + game_state.set(GameState::Playing); + } GameOverMenuAction::Quit => { app_exit_events.write(AppExit::Success); }