From b8e2276c2cca164c8d94dd67c0108adecc9440ec Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 22 Nov 2020 08:54:01 -0600 Subject: [PATCH 1/8] Add the ability to pause timers --- crates/bevy_core/src/time/timer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_core/src/time/timer.rs b/crates/bevy_core/src/time/timer.rs index 33ec613336e66..6165240db6d9d 100644 --- a/crates/bevy_core/src/time/timer.rs +++ b/crates/bevy_core/src/time/timer.rs @@ -7,6 +7,8 @@ use bevy_utils::Duration; /// /// Non repeating timers will stop tracking and stay in the finished state until reset. /// Repeating timers will only be in the finished state on each tick `duration` is reached or exceeded, and can still be reset at any given point. +/// +/// Paused timers will not have elapsed time increased. #[derive(Clone, Debug, Default, Properties)] pub struct Timer { pub elapsed: f32, From e1d758d9f3e15e613862a2dae6b2b73f43c2e131 Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sun, 22 Nov 2020 08:53:43 -0600 Subject: [PATCH 2/8] Add the ability to pause timers --- crates/bevy_core/src/time/timer.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/bevy_core/src/time/timer.rs b/crates/bevy_core/src/time/timer.rs index 6165240db6d9d..642c4ae7472a5 100644 --- a/crates/bevy_core/src/time/timer.rs +++ b/crates/bevy_core/src/time/timer.rs @@ -16,6 +16,7 @@ pub struct Timer { pub finished: bool, /// Will only be true on the tick `duration` is reached or exceeded. pub just_finished: bool, + pub paused: bool, pub repeating: bool, } @@ -36,8 +37,16 @@ impl Timer { } } + pub fn pause(&mut self) { + self.paused = true + } + /// Advances the timer by `delta` seconds. pub fn tick(&mut self, delta: f32) -> &Self { + if self.paused { + return self; + } + let prev_finished = self.elapsed >= self.duration; if !prev_finished { self.elapsed += delta; From 7407d95c3a0e0767f299e868316ede8da945ae1b Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Mon, 23 Nov 2020 00:05:55 -0600 Subject: [PATCH 3/8] Make `Timer`'s members private --- crates/bevy_core/src/time/timer.rs | 40 ++++++++++++++++--- .../src/print_diagnostics_plugin.rs | 4 +- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/crates/bevy_core/src/time/timer.rs b/crates/bevy_core/src/time/timer.rs index 642c4ae7472a5..bb1ba96696844 100644 --- a/crates/bevy_core/src/time/timer.rs +++ b/crates/bevy_core/src/time/timer.rs @@ -11,13 +11,13 @@ use bevy_utils::Duration; /// Paused timers will not have elapsed time increased. #[derive(Clone, Debug, Default, Properties)] pub struct Timer { - pub elapsed: f32, - pub duration: f32, - pub finished: bool, + elapsed: f32, + duration: f32, + finished: bool, /// Will only be true on the tick `duration` is reached or exceeded. - pub just_finished: bool, - pub paused: bool, - pub repeating: bool, + just_finished: bool, + paused: bool, + repeating: bool, } impl Timer { @@ -41,6 +41,34 @@ impl Timer { self.paused = true } + pub fn resume(&mut self) { + self.paused = false + } + + pub fn is_paused(&self) -> bool { + self.paused + } + + pub fn elapsed(&self) -> f32 { + self.elapsed + } + + pub fn duration(&self) -> f32 { + self.duration + } + + pub fn is_finished(&self) -> bool { + self.finished + } + + pub fn just_finished(&self) -> bool { + self.just_finished + } + + pub fn is_repeating(&self) -> bool { + self.repeating + } + /// Advances the timer by `delta` seconds. pub fn tick(&mut self, delta: f32) -> &Self { if self.paused { diff --git a/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs index 73e814fb332c5..0354bf581a0a8 100644 --- a/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs @@ -66,7 +66,7 @@ impl PrintDiagnosticsPlugin { time: Res