From 30c7679af5883f97f6d99fa2b0045283ea11db7e Mon Sep 17 00:00:00 2001 From: Tobias Wennberg Date: Thu, 13 Mar 2025 19:22:53 +0100 Subject: [PATCH] fix stopwatch --- src/main.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4f0dfad..149a2fc 100755 --- a/src/main.rs +++ b/src/main.rs @@ -92,7 +92,7 @@ fn main() { mark_box, markflip_box, )) - .add_systems(OnEnter(GameState::Start), (despawn_boxes.before(draw_boxes), draw_boxes)) + .add_systems(OnEnter(GameState::Start), (despawn_boxes, draw_boxes).chain()) .add_systems(OnEnter(GameState::Fail), draw_fail) .add_systems(OnEnter(GameState::Win), draw_win) .run(); @@ -103,7 +103,7 @@ fn despawn_boxes( boxes: Query>, ) { for e in boxes.iter() { - commands.entity(e).despawn(); + commands.entity(e).despawn_recursive(); } } @@ -111,6 +111,8 @@ fn despawn_boxes( fn load_initial_entities( mut commands: Commands, ) { + let mut stopwatch = Stopwatch::new(); + stopwatch.pause(); commands.spawn(( // Accepts a `String` or any type that converts into a `String`, such as `&str` Text::new("0"), @@ -125,7 +127,7 @@ fn load_initial_entities( right: Val::Percent(50.), ..default() }, - GameStopwatch(Stopwatch::new()), + GameStopwatch(stopwatch), )); } @@ -153,6 +155,7 @@ fn switch_to_win( fn draw_win( mut commands: Commands, mut game_stopwatch: Query<&mut GameStopwatch>, + mut next_state: ResMut>, ) { let mut stopwatch = game_stopwatch.single_mut(); stopwatch.0.pause(); @@ -171,6 +174,7 @@ fn draw_win( ..default() }, )); + next_state.set(GameState::Start); } fn draw_fail( mut commands: Commands, @@ -326,6 +330,7 @@ fn init_mines( } } game_stopwatch.single_mut().0.reset(); + game_stopwatch.single_mut().0.unpause(); next_state.set(GameState::Playing); }