diff --git a/game/src/game.rs b/game/src/game.rs new file mode 100644 index 0000000000000000000000000000000000000000..de6a74140a5e0726b844d06f6dc745ac63340e8c --- /dev/null +++ b/game/src/game.rs @@ -0,0 +1,124 @@ +use bevy::prelude::*; + + use super::{despawn_screen, DisplayQuality, GameState, Volume, TEXT_COLOR}; + + // This plugin will contain the game. In this case, it's just be a screen that will + // display the current settings for 5 seconds before returning to the menu + pub struct GamePlugin; + + impl Plugin for GamePlugin { + fn build(&self, app: &mut App) { + app.add_systems(OnEnter(GameState::Game), game_setup) + .add_systems(Update, game.run_if(in_state(GameState::Game))) + .add_systems(OnExit(GameState::Game), despawn_screen::<OnGameScreen>); + } + } + + // Tag component used to tag entities added on the game screen + #[derive(Component)] + struct OnGameScreen; + + #[derive(Resource, Deref, DerefMut)] + struct GameTimer(Timer); + + fn game_setup( + mut commands: Commands, + display_quality: Res<DisplayQuality>, + volume: Res<Volume>, + ) { + commands + .spawn(( + NodeBundle { + style: Style { + width: Val::Percent(100.0), + height: Val::Percent(100.0), + // center children + align_items: AlignItems::Center, + justify_content: JustifyContent::Center, + ..default() + }, + ..default() + }, + OnGameScreen, + )) + .with_children(|parent| { + // First create a `NodeBundle` for centering what we want to display + parent + .spawn(NodeBundle { + style: Style { + // This will display its children in a column, from top to bottom + flex_direction: FlexDirection::Column, + // `align_items` will align children on the cross axis. Here the main axis is + // vertical (column), so the cross axis is horizontal. This will center the + // children + align_items: AlignItems::Center, + ..default() + }, + background_color: Color::BLACK.into(), + ..default() + }) + .with_children(|parent| { + // Display two lines of text, the second one with the current settings + parent.spawn( + TextBundle::from_section( + "Will be back to the menu shortly...", + TextStyle { + font_size: 80.0, + color: TEXT_COLOR, + ..default() + }, + ) + .with_style(Style { + margin: UiRect::all(Val::Px(50.0)), + ..default() + }), + ); + parent.spawn( + TextBundle::from_sections([ + TextSection::new( + format!("quality: {:?}", *display_quality), + TextStyle { + font_size: 60.0, + color: Color::BLUE, + ..default() + }, + ), + TextSection::new( + " - ", + TextStyle { + font_size: 60.0, + color: TEXT_COLOR, + ..default() + }, + ), + TextSection::new( + format!("volume: {:?}", *volume), + TextStyle { + font_size: 60.0, + color: Color::GREEN, + ..default() + }, + ), + ]) + .with_style(Style { + margin: UiRect::all(Val::Px(50.0)), + ..default() + }), + ); + }); + }); + // Spawn a 5 seconds timer to trigger going back to the menu + commands.insert_resource(GameTimer(Timer::from_seconds(5.0, TimerMode::Once))); + } + + // Tick the timer, and change state when finished + fn game( + time: Res<Time>, + mut game_state: ResMut<NextState<GameState>>, + mut timer: ResMut<GameTimer>, + ) { + if timer.tick(time.delta()).finished() { + game_state.set(GameState::Menu); + } + } + diff --git a/game/src/main.rs b/game/src/main.rs index a9e24892e2f815b2add3b45b0a21d7caf6ec962a..3793ecbad62e7966d55239cc0f3c7bae66499479 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -56,131 +56,7 @@ fn setup(mut commands: Commands) { } -mod game { - use bevy::prelude::*; - - use super::{despawn_screen, DisplayQuality, GameState, Volume, TEXT_COLOR}; - - // This plugin will contain the game. In this case, it's just be a screen that will - // display the current settings for 5 seconds before returning to the menu - pub struct GamePlugin; - - impl Plugin for GamePlugin { - fn build(&self, app: &mut App) { - app.add_systems(OnEnter(GameState::Game), game_setup) - .add_systems(Update, game.run_if(in_state(GameState::Game))) - .add_systems(OnExit(GameState::Game), despawn_screen::<OnGameScreen>); - } - } - - // Tag component used to tag entities added on the game screen - #[derive(Component)] - struct OnGameScreen; - - #[derive(Resource, Deref, DerefMut)] - struct GameTimer(Timer); - - fn game_setup( - mut commands: Commands, - display_quality: Res<DisplayQuality>, - volume: Res<Volume>, - ) { - commands - .spawn(( - NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - // center children - align_items: AlignItems::Center, - justify_content: JustifyContent::Center, - ..default() - }, - ..default() - }, - OnGameScreen, - )) - .with_children(|parent| { - // First create a `NodeBundle` for centering what we want to display - parent - .spawn(NodeBundle { - style: Style { - // This will display its children in a column, from top to bottom - flex_direction: FlexDirection::Column, - // `align_items` will align children on the cross axis. Here the main axis is - // vertical (column), so the cross axis is horizontal. This will center the - // children - align_items: AlignItems::Center, - ..default() - }, - background_color: Color::BLACK.into(), - ..default() - }) - .with_children(|parent| { - // Display two lines of text, the second one with the current settings - parent.spawn( - TextBundle::from_section( - "Will be back to the menu shortly...", - TextStyle { - font_size: 80.0, - color: TEXT_COLOR, - ..default() - }, - ) - .with_style(Style { - margin: UiRect::all(Val::Px(50.0)), - ..default() - }), - ); - parent.spawn( - TextBundle::from_sections([ - TextSection::new( - format!("quality: {:?}", *display_quality), - TextStyle { - font_size: 60.0, - color: Color::BLUE, - ..default() - }, - ), - TextSection::new( - " - ", - TextStyle { - font_size: 60.0, - color: TEXT_COLOR, - ..default() - }, - ), - TextSection::new( - format!("volume: {:?}", *volume), - TextStyle { - font_size: 60.0, - color: Color::GREEN, - ..default() - }, - ), - ]) - .with_style(Style { - margin: UiRect::all(Val::Px(50.0)), - ..default() - }), - ); - }); - }); - // Spawn a 5 seconds timer to trigger going back to the menu - commands.insert_resource(GameTimer(Timer::from_seconds(5.0, TimerMode::Once))); - } - - // Tick the timer, and change state when finished - fn game( - time: Res<Time>, - mut game_state: ResMut<NextState<GameState>>, - mut timer: ResMut<GameTimer>, - ) { - if timer.tick(time.delta()).finished() { - game_state.set(GameState::Menu); - } - } -} +mod game; mod menu { use bevy::{app::AppExit, prelude::*};