From f69cc6f94c9df675457b56297d582c16b5d37cef Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Thu, 26 Nov 2020 13:25:36 -0600 Subject: [PATCH] Allow timers to be paused and encapsulate fields (#914) Allow timers to be paused and encapsulate fields --- crates/bevy_core/src/lib.rs | 1 - crates/bevy_core/src/time/timer.rs | 86 +++++++++++++++---- .../src/print_diagnostics_plugin.rs | 4 +- examples/2d/contributors.rs | 2 +- examples/2d/sprite_sheet.rs | 2 +- examples/app/plugin.rs | 2 +- examples/ecs/event.rs | 2 +- examples/ui/font_atlas_debug.rs | 2 +- 8 files changed, 77 insertions(+), 24 deletions(-) diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 3a5cb4f3c2..03edeef476 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -40,7 +40,6 @@ impl Plugin for CorePlugin { .register_property::() .register_property::>() .add_system_to_stage(stage::FIRST, time_system) - .add_system_to_stage(stage::FIRST, timer_system) .add_system_to_stage(stage::PRE_UPDATE, entity_labels_system); } } diff --git a/crates/bevy_core/src/time/timer.rs b/crates/bevy_core/src/time/timer.rs index a9e9a52894..efa2c3e41a 100644 --- a/crates/bevy_core/src/time/timer.rs +++ b/crates/bevy_core/src/time/timer.rs @@ -1,5 +1,3 @@ -use crate::time::Time; -use bevy_ecs::prelude::*; use bevy_property::Properties; use bevy_utils::Duration; @@ -7,17 +5,17 @@ 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 { - /// Time elapsed on the timer. Guaranteed to be between 0.0 and `duration`, inclusive. - pub elapsed: f32, - pub duration: f32, - /// 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. - 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 repeating: bool, + just_finished: bool, + paused: bool, + repeating: bool, } impl Timer { @@ -37,6 +35,67 @@ impl Timer { } } + #[inline] + pub fn pause(&mut self) { + self.paused = true + } + + #[inline] + pub fn resume(&mut self) { + self.paused = false + } + + #[inline] + pub fn is_paused(&self) -> bool { + self.paused + } + + /// Returns the time elapsed on the timer. Guaranteed to be between 0.0 and `duration`, inclusive. + #[inline] + pub fn elapsed(&self) -> f32 { + self.elapsed + } + + #[inline] + pub fn set_elapsed(&mut self, elapsed: f32) { + self.elapsed = elapsed + } + + #[inline] + pub fn duration(&self) -> f32 { + self.duration + } + + #[inline] + pub fn set_duration(&mut self, duration: f32) { + self.duration = duration + } + + /// Returns the finished state of the timer. + /// + /// 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. + #[inline] + pub fn is_finished(&self) -> bool { + self.finished + } + + /// Will only be true on the tick the timer's duration is reached or exceeded. + #[inline] + pub fn just_finished(&self) -> bool { + self.just_finished + } + + #[inline] + pub fn is_repeating(&self) -> bool { + self.repeating + } + + #[inline] + pub fn set_repeating(&mut self, repeating: bool) { + self.repeating = repeating + } + /// Advances the timer by `delta` seconds. pub fn tick(&mut self, delta: f32) -> &Self { let prev_finished = self.finished; @@ -56,6 +115,7 @@ impl Timer { self } + #[inline] pub fn reset(&mut self) { self.finished = false; self.just_finished = false; @@ -73,12 +133,6 @@ impl Timer { } } -pub(crate) fn timer_system(time: Res