Skip to content

Commit

Permalink
Revert "Fix timer with zero duration (bevyengine#8467)"
Browse files Browse the repository at this point in the history
This reverts commit e2531b2.
  • Loading branch information
Connor-McMillin committed May 11, 2023
1 parent f786ad4 commit 58c2a09
Showing 1 changed file with 5 additions and 36 deletions.
41 changes: 5 additions & 36 deletions crates/bevy_time/src/timer.rs
Expand Up @@ -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());
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 58c2a09

Please sign in to comment.