From 6eb69f476fc4babfa223597fac136c8e30c91191 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Sun, 27 Jul 2025 09:31:09 -0500 Subject: [PATCH] Split title screen into it's own mod & Plugin --- src/lib.rs | 34 ++++------------------------------ src/title_screen.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 src/title_screen.rs diff --git a/src/lib.rs b/src/lib.rs index 014f989..9b66690 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod config; +mod title_screen; use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE}; @@ -9,7 +10,8 @@ pub struct AsteroidPlugin; impl Plugin for AsteroidPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, (spawn_camera, spawn_player, spawn_ui)) + app.add_plugins(title_screen::GameMenuPlugin) + .add_systems(Startup, (spawn_camera, spawn_player, spawn_ui)) .insert_resource(ClearColor(BACKGROUND_COLOR)) .insert_resource(WorldSize { width: WINDOW_SIZE.x, @@ -26,9 +28,7 @@ impl Plugin for AsteroidPlugin { FixedPostUpdate, (integrate_velocity, update_positions, apply_rotation_to_mesh) .run_if(in_state(GameState::Playing)), - ) - .add_systems(OnEnter(GameState::TitleScreen), start_screen); - + ); app.insert_state(GameState::TitleScreen); } } @@ -83,11 +83,6 @@ struct WorldSize { height: f32, } -// 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_camera(mut commands: Commands) { commands.spawn(Camera2d); } @@ -232,24 +227,3 @@ fn spawn_ui(mut commands: Commands, score: Res, lives: Res) { TextFont::from_font_size(25.0), )); } - -fn start_screen(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), - )); - }); -} diff --git a/src/title_screen.rs b/src/title_screen.rs new file mode 100644 index 0000000..2b83e40 --- /dev/null +++ b/src/title_screen.rs @@ -0,0 +1,44 @@ +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); + } +} + +// 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(Camera2d); + 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() { + todo!(); +} \ No newline at end of file