From 58c2a09d87b49e158af385831ac4ee6b3910a1c9 Mon Sep 17 00:00:00 2001 From: Connor McMillin <22306123+Connor-McMillin@users.noreply.github.com> Date: Wed, 10 May 2023 21:37:25 -0700 Subject: [PATCH] Revert "Fix timer with zero duration (#8467)" This reverts commit e2531b227345e4eb3771428a3d94263c0f5f29e4. --- crates/bevy_time/src/timer.rs | 41 +++++------------------------------ 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 9d586f7d59172..369036eeed6df 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -224,17 +224,10 @@ impl Timer { if self.finished() { if self.mode == TimerMode::Repeating { - self.times_finished_this_tick = self - .elapsed() - .as_nanos() - .checked_div(self.duration().as_nanos()) - .map_or(u32::MAX, |x| x as u32); - self.set_elapsed( - self.elapsed() - .as_nanos() - .checked_rem(self.duration().as_nanos()) - .map_or(Duration::ZERO, |x| Duration::from_nanos(x as u64)), - ); + self.times_finished_this_tick = + (self.elapsed().as_nanos() / self.duration().as_nanos()) as u32; + // Duration does not have a modulo + self.set_elapsed(self.elapsed() - self.duration() * self.times_finished_this_tick); } else { self.times_finished_this_tick = 1; self.set_elapsed(self.duration()); @@ -336,11 +329,7 @@ impl Timer { /// ``` #[inline] pub fn percent(&self) -> f32 { - if self.duration == Duration::ZERO { - 1.0 - } else { - self.elapsed().as_secs_f32() / self.duration().as_secs_f32() - } + self.elapsed().as_secs_f32() / self.duration().as_secs_f32() } /// Returns the percentage of the timer remaining time (goes from 1.0 to 0.0). @@ -528,26 +517,6 @@ mod tests { assert_eq!(t.times_finished_this_tick(), 0); } - #[test] - fn times_finished_this_tick_repeating_zero_duration() { - let mut t = Timer::from_seconds(0.0, TimerMode::Repeating); - assert_eq!(t.times_finished_this_tick(), 0); - assert_eq!(t.elapsed(), Duration::ZERO); - assert_eq!(t.percent(), 1.0); - t.tick(Duration::from_secs(1)); - assert_eq!(t.times_finished_this_tick(), u32::MAX); - assert_eq!(t.elapsed(), Duration::ZERO); - assert_eq!(t.percent(), 1.0); - t.tick(Duration::from_secs(2)); - assert_eq!(t.times_finished_this_tick(), u32::MAX); - assert_eq!(t.elapsed(), Duration::ZERO); - assert_eq!(t.percent(), 1.0); - t.reset(); - assert_eq!(t.times_finished_this_tick(), 0); - assert_eq!(t.elapsed(), Duration::ZERO); - assert_eq!(t.percent(), 1.0); - } - #[test] fn times_finished_this_tick_precise() { let mut t = Timer::from_seconds(0.01, TimerMode::Repeating);