mirror of
https://github.com/kristoferssolo/maze-ascension.git
synced 2025-10-21 19:20:34 +00:00
refactor(stats): make score a resource
This commit is contained in:
parent
58276ea8f7
commit
5e1e4a546a
@ -1,9 +1,5 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Component, Deref, DerefMut)]
|
|
||||||
#[reflect(Component)]
|
|
||||||
pub struct Score(pub usize);
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Reflect, Component)]
|
#[derive(Debug, Clone, Reflect, Component)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct FloorDisplay;
|
pub struct FloorDisplay;
|
||||||
|
|||||||
@ -4,16 +4,15 @@ pub mod resources;
|
|||||||
mod systems;
|
mod systems;
|
||||||
|
|
||||||
use bevy::{ecs::system::RunSystemOnce, prelude::*};
|
use bevy::{ecs::system::RunSystemOnce, prelude::*};
|
||||||
use components::Score;
|
use resources::{FloorTimer, Score, TotalTimer};
|
||||||
use resources::{FloorTimer, TotalTimer};
|
|
||||||
|
|
||||||
pub(super) fn plugin(app: &mut App) {
|
pub(super) fn plugin(app: &mut App) {
|
||||||
app.register_type::<Score>()
|
app.init_resource::<Score>()
|
||||||
.init_resource::<TotalTimer>()
|
.init_resource::<TotalTimer>()
|
||||||
.init_resource::<FloorTimer>()
|
.init_resource::<FloorTimer>()
|
||||||
.add_plugins(systems::plugin);
|
.add_plugins(systems::plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawn_stats_command(world: &mut World) {
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,10 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Reflect, Resource, Deref, DerefMut)]
|
||||||
|
#[reflect(Resource)]
|
||||||
|
pub struct Score(pub usize);
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Resource, Deref, DerefMut)]
|
#[derive(Debug, Reflect, Resource, Deref, DerefMut)]
|
||||||
#[reflect(Resource)]
|
#[reflect(Resource)]
|
||||||
pub struct TotalTimer(pub Timer);
|
pub struct TotalTimer(pub Timer);
|
||||||
|
|||||||
@ -1,28 +1,31 @@
|
|||||||
mod common;
|
mod common;
|
||||||
mod floor;
|
mod floor;
|
||||||
mod floor_timer;
|
mod floor_timer;
|
||||||
|
mod reset;
|
||||||
mod score;
|
mod score;
|
||||||
pub mod setup;
|
pub mod spawn;
|
||||||
mod total_timer;
|
mod total_timer;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use floor::{update_floor_display, update_highest_floor_display};
|
use floor::{update_floor_display, update_highest_floor_display};
|
||||||
use floor_timer::{update_floor_timer, update_floor_timer_display};
|
use floor_timer::{update_floor_timer, update_floor_timer_display};
|
||||||
|
use reset::reset_timers;
|
||||||
use score::{update_score, update_score_display};
|
use score::{update_score, update_score_display};
|
||||||
use total_timer::{update_total_timer, update_total_timer_display};
|
use total_timer::{update_total_timer, update_total_timer_display};
|
||||||
|
|
||||||
use crate::screens::Screen;
|
use crate::screens::Screen;
|
||||||
|
|
||||||
pub(super) fn plugin(app: &mut App) {
|
pub(super) fn plugin(app: &mut App) {
|
||||||
app.add_systems(
|
app.add_systems(OnEnter(Screen::Gameplay), reset_timers)
|
||||||
Update,
|
.add_systems(
|
||||||
(
|
Update,
|
||||||
(update_score, update_score_display).chain(),
|
(
|
||||||
(update_floor_timer, update_floor_timer_display).chain(),
|
(update_score, update_score_display).chain(),
|
||||||
(update_total_timer, update_total_timer_display).chain(),
|
(update_floor_timer, update_floor_timer_display).chain(),
|
||||||
update_floor_display,
|
(update_total_timer, update_total_timer_display).chain(),
|
||||||
update_highest_floor_display,
|
update_floor_display,
|
||||||
)
|
update_highest_floor_display,
|
||||||
.run_if(in_state(Screen::Gameplay)),
|
)
|
||||||
);
|
.run_if(in_state(Screen::Gameplay)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/stats/systems/reset.rs
Normal file
13
src/stats/systems/reset.rs
Normal 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;
|
||||||
|
}
|
||||||
@ -6,13 +6,13 @@ use crate::{
|
|||||||
},
|
},
|
||||||
floor::resources::HighestFloor,
|
floor::resources::HighestFloor,
|
||||||
stats::{
|
stats::{
|
||||||
components::{Score, ScoreDisplay},
|
components::ScoreDisplay,
|
||||||
resources::FloorTimer,
|
resources::{FloorTimer, Score},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn update_score(
|
pub fn update_score(
|
||||||
mut score_query: Query<&mut Score>,
|
mut score: ResMut<Score>,
|
||||||
hightes_floor: Res<HighestFloor>,
|
hightes_floor: Res<HighestFloor>,
|
||||||
floor_timer: Res<FloorTimer>,
|
floor_timer: Res<FloorTimer>,
|
||||||
) {
|
) {
|
||||||
@ -20,21 +20,13 @@ pub fn update_score(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Ok(mut score) = score_query.get_single_mut() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
score.0 = calculate_score(hightes_floor.0, floor_timer.elapsed_secs());
|
score.0 = calculate_score(hightes_floor.0, floor_timer.elapsed_secs());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_score_display(
|
pub fn update_score_display(
|
||||||
score_query: Query<&Score>,
|
|
||||||
mut text_query: Query<&mut Text, With<ScoreDisplay>>,
|
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 {
|
let Ok(mut text) = text_query.get_single_mut() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
25
src/stats/systems/spawn.rs
Normal file
25
src/stats/systems/spawn.rs
Normal 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -120,7 +120,7 @@ impl<T: SpawnUi> Widgets for T {
|
|||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
bundle,
|
bundle,
|
||||||
TextColor(RosePineDawn::Foam.to_color()),
|
TextColor(RosePineDawn::Text.to_color()),
|
||||||
));
|
));
|
||||||
entity
|
entity
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user