From 0e517de4196fe62bbdcb09588690e12d10e48d3d Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Mon, 11 Aug 2025 23:40:52 -0500 Subject: [PATCH] Move the title screen stuff into widgets.rs --- src/lib.rs | 3 +-- src/title_screen.rs | 54 --------------------------------------------- src/widgets.rs | 51 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 56 deletions(-) delete mode 100644 src/title_screen.rs diff --git a/src/lib.rs b/src/lib.rs index 66ee1a1..006ece5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ mod machinery; mod objects; mod physics; mod resources; -mod title_screen; mod widgets; use crate::config::{ @@ -30,7 +29,7 @@ pub struct AsteroidPlugin; impl Plugin for AsteroidPlugin { fn build(&self, app: &mut App) { app.add_plugins(( - title_screen::GameMenuPlugin, + widgets::GameMenuPlugin, widgets::preparation_widget_plugin, RapierPhysicsPlugin::::pixels_per_meter(10.0), RapierDebugRenderPlugin::default(), diff --git a/src/title_screen.rs b/src/title_screen.rs deleted file mode 100644 index 52e2168..0000000 --- a/src/title_screen.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::GameState; - -use bevy::prelude::*; - -pub struct GameMenuPlugin; - -impl Plugin for GameMenuPlugin { - fn build(&self, app: &mut App) { - app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu) - .add_systems(OnExit(GameState::TitleScreen), despawn_menu) - .add_systems( - Update, - handle_spacebar.run_if(in_state(GameState::TitleScreen)), - ); - } -} - -// Marker component for the title screen UI entity. -// This way, a query for the TitleUI can be used to despawn the title screen -#[derive(Component)] -struct TitleUI; - -fn spawn_menu(mut commands: Commands) { - commands - .spawn(( - TitleUI, - Node { - flex_direction: FlexDirection::Column, - ..Default::default() - }, - )) - .with_children(|cmds| { - cmds.spawn(( - Text::new("Robert's Bad Asteroids Game"), - TextFont::from_font_size(50.0), - )); - cmds.spawn(( - Text::new("Press space to begin"), - TextFont::from_font_size(40.0), - )); - }); -} - -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); - } -} diff --git a/src/widgets.rs b/src/widgets.rs index d1e6e24..11733be 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -97,3 +97,54 @@ fn animate_get_ready_widget( game_state.set(GameState::Playing); } } + +pub struct GameMenuPlugin; + +impl Plugin for GameMenuPlugin { + fn build(&self, app: &mut App) { + app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu) + .add_systems(OnExit(GameState::TitleScreen), despawn_menu) + .add_systems( + Update, + handle_spacebar.run_if(in_state(GameState::TitleScreen)), + ); + } +} + +// Marker component for the title screen UI entity. +// This way, a query for the TitleUI can be used to despawn the title screen +#[derive(Component)] +struct TitleUI; + +fn spawn_menu(mut commands: Commands) { + commands + .spawn(( + TitleUI, + Node { + flex_direction: FlexDirection::Column, + ..Default::default() + }, + )) + .with_children(|cmds| { + cmds.spawn(( + Text::new("Robert's Bad Asteroids Game"), + TextFont::from_font_size(50.0), + )); + cmds.spawn(( + Text::new("Press space to begin"), + TextFont::from_font_size(40.0), + )); + }); +} + +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); + } +}