Skip to content

Commit

Permalink
relative speed should be 0 when paused
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Aug 20, 2022
1 parent a7ae4f3 commit f987c17
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions crates/bevy_time/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Time {
first_update: Option<Instant>,
last_update: Option<Instant>,
relative_speed: f64, // using `f64` instead of `f32` to minimize drift from rounding errors
stored_relative_speed: Option<f64>, // stores relative speed value when time is paused
paused: bool,
delta: Duration,
delta_seconds: f32,
Expand All @@ -33,6 +34,7 @@ impl Default for Time {
first_update: None,
last_update: None,
relative_speed: 1.0,
stored_relative_speed: None,
paused: false,
delta: Duration::ZERO,
delta_seconds: 0.0,
Expand Down Expand Up @@ -190,7 +192,11 @@ impl Time {
pub fn set_relative_speed(&mut self, ratio: f32) {
assert!(ratio.is_finite(), "tried to go infinitely fast");
assert!(ratio.is_sign_positive(), "tried to go back in time");
self.relative_speed = ratio as f64;
if self.paused {
self.stored_relative_speed = Some(ratio as f64);
} else {
self.relative_speed = ratio as f64;
}
}

/// Sets the rate that time advances relative to raw CPU time, given as [`f64`].
Expand All @@ -201,17 +207,30 @@ impl Time {
pub fn set_relative_speed_f64(&mut self, ratio: f64) {
assert!(ratio.is_finite(), "tried to go infinitely fast");
assert!(ratio.is_sign_positive(), "tried to go back in time");
self.relative_speed = ratio;
if self.paused {
self.stored_relative_speed = Some(ratio);
} else {
self.relative_speed = ratio;
}
}

/// Stops time, preventing it from advancing until resumed. Does not affect raw measurements.
pub fn pause(&mut self) {
self.paused = true;
if !self.paused {
self.stored_relative_speed = Some(self.relative_speed);
self.relative_speed = 0.0;
self.paused = true;
}
}

/// Resumes time if paused.
pub fn unpause(&mut self) {
self.paused = false;
if self.paused {
if let Some(s) = self.stored_relative_speed.take() {
self.relative_speed = s;
}
self.paused = false;
}
}

/// Returns `true` if time has been paused and cannot advance.
Expand Down Expand Up @@ -534,17 +553,18 @@ mod tests {
time.update_with_instant(first_update_instant);

assert!(!time.paused());
assert_eq!(time.relative_speed(), 1.0);

time.pause();

assert!(time.paused());
assert_eq!(time.relative_speed(), 0.0);

let second_update_instant = Instant::now();
time.update_with_instant(second_update_instant);
assert_eq!(time.startup(), start_instant);
assert_eq!(time.first_update(), Some(first_update_instant));
assert_eq!(time.last_update(), Some(second_update_instant));
assert_eq!(time.relative_speed(), 1.0);
assert_eq!(time.delta(), Duration::ZERO);
assert_eq!(
time.raw_delta(),
Expand All @@ -562,13 +582,13 @@ mod tests {
time.unpause();

assert!(!time.paused());
assert_eq!(time.relative_speed(), 1.0);

let third_update_instant = Instant::now();
time.update_with_instant(third_update_instant);
assert_eq!(time.startup(), start_instant);
assert_eq!(time.first_update(), Some(first_update_instant));
assert_eq!(time.last_update(), Some(third_update_instant));
assert_eq!(time.relative_speed(), 1.0);
assert_eq!(time.delta(), third_update_instant - second_update_instant);
assert_eq!(
time.raw_delta(),
Expand Down

0 comments on commit f987c17

Please sign in to comment.