From 619c44f482a92ed257a24454f84797f18a15e559 Mon Sep 17 00:00:00 2001 From: Sergi-Ferrez Date: Wed, 14 Sep 2022 18:57:13 +0000 Subject: [PATCH] Remaining fn in Timer (#5971) # Objective Fixes #5963 ## Solution Add remaining fn in Timer class, this function only minus total duration with elapsed time. Co-authored-by: Sergi-Ferrez <61662926+Sergi-Ferrez@users.noreply.github.com> --- crates/bevy_time/src/timer.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 6f5d240c3f..54cd51e3a3 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -343,6 +343,38 @@ impl Timer { 1.0 - self.percent() } + /// Returns the remaining time in seconds + /// + /// # Examples + /// ``` + /// # use bevy_time::*; + /// use std::cmp::Ordering; + /// use std::time::Duration; + /// let mut timer = Timer::from_seconds(2.0, false); + /// timer.tick(Duration::from_secs_f32(0.5)); + /// let result = timer.remaining_secs().total_cmp(&1.5); + /// assert_eq!(Ordering::Equal, result); + /// ``` + #[inline] + pub fn remaining_secs(&self) -> f32 { + self.remaining().as_secs_f32() + } + + /// Returns the remaining time using Duration + /// + /// # Examples + /// ``` + /// # use bevy_time::*; + /// use std::time::Duration; + /// let mut timer = Timer::from_seconds(2.0, false); + /// timer.tick(Duration::from_secs_f32(0.5)); + /// assert_eq!(timer.remaining(), Duration::from_secs_f32(1.5)); + /// ``` + #[inline] + pub fn remaining(&self) -> Duration { + self.duration() - self.elapsed() + } + /// Returns the number of times a repeating timer /// finished during the last [`tick`](Timer::tick) call. ///