diff --git a/src/maze/systems/spawn.rs b/src/maze/systems/spawn.rs index 98c889a..26b94b9 100644 --- a/src/maze/systems/spawn.rs +++ b/src/maze/systems/spawn.rs @@ -15,7 +15,6 @@ use crate::{ components::{HexMaze, MazeConfig, Tile, Wall}, resources::GlobalMazeConfig, }, - screens::Screen, theme::palette::rose_pine::RosePineDawn, }; @@ -62,7 +61,6 @@ pub fn spawn_maze( config.clone(), Transform::from_translation(Vec3::ZERO.with_y(y_offset)), Visibility::Visible, - StateScoped(Screen::Gameplay), )) .insert_if(CurrentFloor, || floor == 1) // Only floor 1 gets CurrentFloor .id(); diff --git a/src/player/systems/spawn.rs b/src/player/systems/spawn.rs index 636572e..e37aa6b 100644 --- a/src/player/systems/spawn.rs +++ b/src/player/systems/spawn.rs @@ -5,7 +5,6 @@ use crate::{ assets::{blue_material, generate_pill_mesh}, components::{CurrentPosition, Player}, }, - screens::Screen, }; use bevy::prelude::*; @@ -33,6 +32,5 @@ pub fn spawn_player( Mesh3d(meshes.add(generate_pill_mesh(player_radius, player_height / 2.))), MeshMaterial3d(materials.add(blue_material())), Transform::from_xyz(start_pos.x, y_offset, start_pos.y), - StateScoped(Screen::Gameplay), )); } diff --git a/src/screens/gameplay.rs b/src/screens/gameplay.rs index 42bd319..61582b2 100644 --- a/src/screens/gameplay.rs +++ b/src/screens/gameplay.rs @@ -8,6 +8,7 @@ use crate::{ use bevy::{input::common_conditions::input_just_pressed, prelude::*}; pub(super) fn plugin(app: &mut App) { + app.init_resource::(); app.add_systems( OnEnter(Screen::Gameplay), ( @@ -16,8 +17,14 @@ pub(super) fn plugin(app: &mut App) { spawn_hint_command, spawn_stats_command, ) - .chain(), + .chain() + .run_if(not(resource_exists::)), ); + app.add_systems(OnEnter(Screen::Gameplay), |mut commands: Commands| { + commands.insert_resource(GameplayInitialized(true)); + }); + + app.add_systems(OnEnter(Screen::Title), reset_gameplay_state); app.add_systems( Update, @@ -28,3 +35,11 @@ pub(super) fn plugin(app: &mut App) { fn pause_game(mut next_screen: ResMut>) { next_screen.set(Screen::Pause); } + +fn reset_gameplay_state(mut commands: Commands) { + commands.remove_resource::(); +} + +#[derive(Debug, Default, Reflect, Resource, DerefMut, Deref)] +#[reflect(Resource)] +pub struct GameplayInitialized(bool); diff --git a/src/screens/mod.rs b/src/screens/mod.rs index 7af5a6c..643245b 100644 --- a/src/screens/mod.rs +++ b/src/screens/mod.rs @@ -7,6 +7,7 @@ mod splash; mod title; use bevy::prelude::*; +pub use gameplay::GameplayInitialized; pub(super) fn plugin(app: &mut App) { app.init_state::(); diff --git a/src/screens/pause.rs b/src/screens/pause.rs index c726f5b..fa3caa3 100644 --- a/src/screens/pause.rs +++ b/src/screens/pause.rs @@ -2,24 +2,38 @@ use bevy::{input::common_conditions::input_just_pressed, prelude::*}; use crate::theme::{ events::OnPress, + palette::rose_pine::RosePineDawn, + prelude::ColorScheme, widgets::{Containers, Widgets}, }; use super::Screen; pub(super) fn plugin(app: &mut App) { - app.add_systems(OnEnter(Screen::Pause), spawn_title_screen); + app.add_systems(OnEnter(Screen::Pause), spawn_pause_overlay); app.add_systems( Update, return_to_game.run_if(in_state(Screen::Pause).and(input_just_pressed(KeyCode::Escape))), ); } -fn spawn_title_screen(mut commands: Commands) { +fn spawn_pause_overlay(mut commands: Commands) { commands .ui_root() - .insert(StateScoped(Screen::Pause)) + .insert(( + StateScoped(Screen::Pause), + BackgroundColor(RosePineDawn::Muted.to_color().with_alpha(0.5)), + )) .with_children(|parent| { + parent + .spawn(Node { + bottom: Val::Px(100.), + ..default() + }) + .with_children(|parent| { + parent.header("Paused"); + }); + parent.button("Continue").observe(return_to_game_trigger); parent .button("Exit") diff --git a/src/stats/systems/mod.rs b/src/stats/systems/mod.rs index 0ee4eaa..a5eddf8 100644 --- a/src/stats/systems/mod.rs +++ b/src/stats/systems/mod.rs @@ -13,23 +13,26 @@ use reset::reset_timers; use score::{update_score, update_score_display}; use total_timer::{update_total_timer, update_total_timer_display}; -use crate::screens::Screen; +use crate::screens::{GameplayInitialized, Screen}; pub(super) fn plugin(app: &mut App) { - app.add_systems(OnEnter(Screen::Gameplay), reset_timers) - .add_systems( - Update, + app.add_systems( + OnEnter(Screen::Gameplay), + reset_timers.run_if(not(resource_exists::)), + ); + app.add_systems( + Update, + ( ( - ( - update_score.before(update_floor_timer), - update_score_display, - ) - .chain(), - (update_floor_timer, update_floor_timer_display).chain(), - (update_total_timer, update_total_timer_display).chain(), - update_floor_display, - update_highest_floor_display, + update_score.before(update_floor_timer), + update_score_display, ) - .run_if(in_state(Screen::Gameplay)), - ); + .chain(), + (update_floor_timer, update_floor_timer_display).chain(), + (update_total_timer, update_total_timer_display).chain(), + update_floor_display, + update_highest_floor_display, + ) + .run_if(in_state(Screen::Gameplay)), + ); } diff --git a/src/stats/systems/spawn.rs b/src/stats/systems/spawn.rs index ca4283c..0ec6646 100644 --- a/src/stats/systems/spawn.rs +++ b/src/stats/systems/spawn.rs @@ -1,7 +1,6 @@ use bevy::prelude::*; use crate::{ - screens::Screen, stats::{ components::{ FloorDisplay, FloorTimerDisplay, HighestFloorDisplay, ScoreDisplay, TotalTimerDisplay, @@ -12,14 +11,11 @@ use crate::{ }; pub fn spawn_stats(mut commands: Commands) { - commands - .ui_stats() - .insert(StateScoped(Screen::Gameplay)) - .with_children(|parent| { - parent.stats("Floor: 1", FloorDisplay); - parent.stats("Highest Floor: 1", HighestFloorDisplay); - parent.stats("Score: 0", ScoreDisplay); - parent.stats("Floor Timer", FloorTimerDisplay); - parent.stats("Total Timer", TotalTimerDisplay); - }); + commands.ui_stats().with_children(|parent| { + parent.stats("Floor: 1", FloorDisplay); + parent.stats("Highest Floor: 1", HighestFloorDisplay); + parent.stats("Score: 0", ScoreDisplay); + parent.stats("Floor Timer", FloorTimerDisplay); + parent.stats("Total Timer", TotalTimerDisplay); + }); }