Skip to content

Commit 839ea06

Browse files
committed
add time played to game over screen
1 parent 0fdff22 commit 839ea06

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/screens/game.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct GameStats {
1515
pub distance_traveled: u32,
1616
pub shots_fired: u32,
1717
pub shots_hit: u32,
18-
// pub time_played: f32,
18+
pub time_played: f32,
1919
// pub successful_trip: bool,
2020
}
2121

@@ -43,7 +43,7 @@ pub(super) fn plugin(app: &mut App) {
4343
distance_traveled: 0,
4444
shots_fired: 0,
4545
shots_hit: 0,
46-
// time_played: 0.0,
46+
time_played: 0.0,
4747
// successful_trip: false,
4848
});
4949
app.insert_resource(HasEntered(false));
@@ -56,6 +56,11 @@ pub(super) fn plugin(app: &mut App) {
5656
app.configure_sets(Startup, GameSystems::Play);
5757
app.configure_sets(Startup, GameSystems::Environment);
5858

59+
app.add_systems(
60+
Update,
61+
stopwatch.run_if(in_state(GameState::Play).and(in_state(PauseState::NotPaused))),
62+
);
63+
5964
app.add_systems(
6065
FixedUpdate,
6166
enter_player.run_if(
@@ -141,6 +146,7 @@ fn enter_player(
141146
game_stats.distance_traveled = 0;
142147
game_stats.shots_fired = 0;
143148
game_stats.shots_hit = 0;
149+
game_stats.time_played = 0.0;
144150
let player = player.single_mut();
145151
if player.is_err() {
146152
return;
@@ -185,6 +191,10 @@ struct Asteroid {
185191
#[derive(Resource)]
186192
struct AsteroidSpawnTimer(Timer);
187193

194+
fn stopwatch(time: Res<Time<Virtual>>, mut game_stats: ResMut<GameStats>) {
195+
game_stats.time_played += time.delta_secs();
196+
}
197+
188198
fn player_input(
189199
mut commands: Commands,
190200
input_query: Query<&ActionState<Action>>,

src/screens/gameover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ fn spawn_game_over(
8484
},
8585
children![
8686
stats_row(
87-
"DISTANCE TRAVELED",
88-
&game_stats.distance_traveled.to_string(),
87+
"TIME PLAYED",
88+
format!("{:.0}", &game_stats.time_played).as_str(),
8989
scale,
9090
font_handle.clone(),
9191
),

0 commit comments

Comments
 (0)