diff --git a/src/stats/components.rs b/src/stats/components.rs index d714fbd..f37e09b 100644 --- a/src/stats/components.rs +++ b/src/stats/components.rs @@ -1,9 +1,5 @@ use bevy::prelude::*; -#[derive(Debug, Reflect, Component, Deref, DerefMut)] -#[reflect(Component)] -pub struct Score(pub usize); - #[derive(Debug, Clone, Reflect, Component)] #[reflect(Component)] pub struct FloorDisplay; diff --git a/src/stats/mod.rs b/src/stats/mod.rs index 083f6d6..7693d14 100644 --- a/src/stats/mod.rs +++ b/src/stats/mod.rs @@ -4,16 +4,15 @@ pub mod resources; mod systems; use bevy::{ecs::system::RunSystemOnce, prelude::*}; -use components::Score; -use resources::{FloorTimer, TotalTimer}; +use resources::{FloorTimer, Score, TotalTimer}; pub(super) fn plugin(app: &mut App) { - app.register_type::() + app.init_resource::() .init_resource::() .init_resource::() .add_plugins(systems::plugin); } pub fn spawn_stats_command(world: &mut World) { - let _ = world.run_system_once(systems::setup::setup); + let _ = world.run_system_once(systems::spawn::spawn_stats); } diff --git a/src/stats/resources.rs b/src/stats/resources.rs index b4e2be8..2da7f8b 100644 --- a/src/stats/resources.rs +++ b/src/stats/resources.rs @@ -2,6 +2,10 @@ use std::time::Duration; use bevy::prelude::*; +#[derive(Debug, Default, Reflect, Resource, Deref, DerefMut)] +#[reflect(Resource)] +pub struct Score(pub usize); + #[derive(Debug, Reflect, Resource, Deref, DerefMut)] #[reflect(Resource)] pub struct TotalTimer(pub Timer); diff --git a/src/stats/systems/mod.rs b/src/stats/systems/mod.rs index 2c067f8..62e0e8e 100644 --- a/src/stats/systems/mod.rs +++ b/src/stats/systems/mod.rs @@ -1,28 +1,31 @@ mod common; mod floor; mod floor_timer; +mod reset; mod score; -pub mod setup; +pub mod spawn; mod total_timer; use bevy::prelude::*; use floor::{update_floor_display, update_highest_floor_display}; use floor_timer::{update_floor_timer, update_floor_timer_display}; +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; pub(super) fn plugin(app: &mut App) { - app.add_systems( - Update, - ( - (update_score, 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, - ) - .run_if(in_state(Screen::Gameplay)), - ); + app.add_systems(OnEnter(Screen::Gameplay), reset_timers) + .add_systems( + Update, + ( + (update_score, 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, + ) + .run_if(in_state(Screen::Gameplay)), + ); } diff --git a/src/stats/systems/reset.rs b/src/stats/systems/reset.rs new file mode 100644 index 0000000..ab795a2 --- /dev/null +++ b/src/stats/systems/reset.rs @@ -0,0 +1,13 @@ +use bevy::prelude::*; + +use crate::stats::resources::{FloorTimer, Score, TotalTimer}; + +pub fn reset_timers( + mut floor_timer: ResMut, + mut total_timer: ResMut, + mut score: ResMut, +) { + floor_timer.reset(); + total_timer.reset(); + score.0 = 0; +} diff --git a/src/stats/systems/score.rs b/src/stats/systems/score.rs index f2eb7be..57b04e4 100644 --- a/src/stats/systems/score.rs +++ b/src/stats/systems/score.rs @@ -6,13 +6,13 @@ use crate::{ }, floor::resources::HighestFloor, stats::{ - components::{Score, ScoreDisplay}, - resources::FloorTimer, + components::ScoreDisplay, + resources::{FloorTimer, Score}, }, }; pub fn update_score( - mut score_query: Query<&mut Score>, + mut score: ResMut, hightes_floor: Res, floor_timer: Res, ) { @@ -20,21 +20,13 @@ pub fn update_score( return; } - let Ok(mut score) = score_query.get_single_mut() else { - return; - }; - score.0 = calculate_score(hightes_floor.0, floor_timer.elapsed_secs()); } pub fn update_score_display( - score_query: Query<&Score>, mut text_query: Query<&mut Text, With>, + score: Res, ) { - let Ok(score) = score_query.get_single() else { - return; - }; - let Ok(mut text) = text_query.get_single_mut() else { return; }; diff --git a/src/stats/systems/setup.rs b/src/stats/systems/setup.rs deleted file mode 100644 index 0fa9380..0000000 --- a/src/stats/systems/setup.rs +++ /dev/null @@ -1,23 +0,0 @@ -use bevy::prelude::*; - -use crate::{ - stats::{ - components::{ - FloorDisplay, FloorTimerDisplay, HighestFloorDisplay, Score, ScoreDisplay, - TotalTimerDisplay, - }, - container::StatsContainer, - }, - theme::widgets::Widgets, -}; - -pub fn setup(mut commands: Commands) { - commands.spawn((Name::new("Score"), Score(0))); - 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); - }); -} diff --git a/src/stats/systems/spawn.rs b/src/stats/systems/spawn.rs new file mode 100644 index 0000000..ca4283c --- /dev/null +++ b/src/stats/systems/spawn.rs @@ -0,0 +1,25 @@ +use bevy::prelude::*; + +use crate::{ + screens::Screen, + stats::{ + components::{ + FloorDisplay, FloorTimerDisplay, HighestFloorDisplay, ScoreDisplay, TotalTimerDisplay, + }, + container::StatsContainer, + }, + theme::widgets::Widgets, +}; + +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); + }); +} diff --git a/src/theme/widgets.rs b/src/theme/widgets.rs index e185efa..3f30e85 100644 --- a/src/theme/widgets.rs +++ b/src/theme/widgets.rs @@ -120,7 +120,7 @@ impl Widgets for T { ..default() }, bundle, - TextColor(RosePineDawn::Foam.to_color()), + TextColor(RosePineDawn::Text.to_color()), )); entity }