refactor(stats): make score a resource

This commit is contained in:
Kristofers Solo 2025-01-17 00:56:26 +02:00
parent 58276ea8f7
commit 5e1e4a546a
9 changed files with 65 additions and 56 deletions

View File

@ -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;

View File

@ -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::<Score>()
app.init_resource::<Score>()
.init_resource::<TotalTimer>()
.init_resource::<FloorTimer>()
.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);
}

View File

@ -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);

View File

@ -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)),
);
}

View File

@ -0,0 +1,13 @@
use bevy::prelude::*;
use crate::stats::resources::{FloorTimer, Score, TotalTimer};
pub fn reset_timers(
mut floor_timer: ResMut<FloorTimer>,
mut total_timer: ResMut<TotalTimer>,
mut score: ResMut<Score>,
) {
floor_timer.reset();
total_timer.reset();
score.0 = 0;
}

View File

@ -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<Score>,
hightes_floor: Res<HighestFloor>,
floor_timer: Res<FloorTimer>,
) {
@ -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<ScoreDisplay>>,
score: Res<Score>,
) {
let Ok(score) = score_query.get_single() else {
return;
};
let Ok(mut text) = text_query.get_single_mut() else {
return;
};

View File

@ -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);
});
}

View File

@ -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);
});
}

View File

@ -120,7 +120,7 @@ impl<T: SpawnUi> Widgets for T {
..default()
},
bundle,
TextColor(RosePineDawn::Foam.to_color()),
TextColor(RosePineDawn::Text.to_color()),
));
entity
}