From 1f49b74cdd8c1666144796c8213a868b12809cb2 Mon Sep 17 00:00:00 2001 From: Sergi Date: Tue, 13 Sep 2022 01:42:08 +0200 Subject: [PATCH 1/4] Remaining fn in Timer --- crates/bevy_time/src/timer.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 6f5d240c3fc8a..4e65a5bf54cec 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -343,6 +343,21 @@ impl Timer { 1.0 - self.percent() } + /// Returns the remainig time in seconds + /// + /// # 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(), 1.5); + /// ``` + #[inline] + pub fn remaining(&self) -> f32 { + self.duration().as_secs_f32() -self.elapsed().as_secs_f32() + } + /// Returns the number of times a repeating timer /// finished during the last [`tick`](Timer::tick) call. /// From e71e756d3a87e8d01cbd007d1474c2ae2f3b6992 Mon Sep 17 00:00:00 2001 From: Sergi-Ferrez <61662926+Sergi-Ferrez@users.noreply.github.com> Date: Tue, 13 Sep 2022 14:24:07 +0200 Subject: [PATCH 2/4] Fix remaining and remaining_secs --- crates/bevy_time/src/timer.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 4e65a5bf54cec..c3ce8fdb3d164 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -343,7 +343,7 @@ impl Timer { 1.0 - self.percent() } - /// Returns the remainig time in seconds + /// Returns the remaining time in seconds /// /// # Examples /// ``` @@ -351,11 +351,26 @@ impl Timer { /// 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(), 1.5); + /// assert_eq!(timer.remaining_secs(), 1.5); /// ``` #[inline] - pub fn remaining(&self) -> f32 { - self.duration().as_secs_f32() -self.elapsed().as_secs_f32() + pub fn remaining_secs(&self) -> f32 { + self.duration().as_secs_f32() - self.elapsed().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 From a264e50e4d965c6f3c327d15de320298401b103f Mon Sep 17 00:00:00 2001 From: Sergi-Ferrez <61662926+Sergi-Ferrez@users.noreply.github.com> Date: Tue, 13 Sep 2022 20:15:18 +0200 Subject: [PATCH 3/4] Fix float equality assert --- crates/bevy_time/src/timer.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index c3ce8fdb3d164..024a8c8b4b97a 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -348,10 +348,12 @@ impl Timer { /// # 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)); - /// assert_eq!(timer.remaining_secs(), 1.5); + /// let result = timer.remaining_secs().total_cmp(&1.5); + /// assert_eq!(Ordering::Equal, result); /// ``` #[inline] pub fn remaining_secs(&self) -> f32 { From be5e9139c88a49a3f407884d3f67a2c4cf6117ca Mon Sep 17 00:00:00 2001 From: Sergi-Ferrez <61662926+Sergi-Ferrez@users.noreply.github.com> Date: Wed, 14 Sep 2022 09:34:24 +0200 Subject: [PATCH 4/4] `remaining_secs` Simplified Logic --- crates/bevy_time/src/timer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 024a8c8b4b97a..54cd51e3a3658 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -357,7 +357,7 @@ impl Timer { /// ``` #[inline] pub fn remaining_secs(&self) -> f32 { - self.duration().as_secs_f32() - self.elapsed().as_secs_f32() + self.remaining().as_secs_f32() } /// Returns the remaining time using Duration