Skip to content

Commit

Permalink
Made Time::time_since_startup return from last tick. (#3264)
Browse files Browse the repository at this point in the history
Also added unit tests for it.

# Objective

- Fixes #3259 

## Solution

- As discussed in #3259

Co-authored-by: Alex Helfet <alex.helfet@gmail.com>
  • Loading branch information
fluffysquirrels and Alex Helfet committed Dec 7, 2021
1 parent a636145 commit 5e516ab
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions crates/bevy_core/src/time/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Time {
delta_seconds_f64: f64,
delta_seconds: f32,
seconds_since_startup: f64,
time_since_startup: Duration,
startup: Instant,
}

Expand All @@ -20,6 +21,7 @@ impl Default for Time {
startup: Instant::now(),
delta_seconds_f64: 0.0,
seconds_since_startup: 0.0,
time_since_startup: Duration::from_secs(0),
delta_seconds: 0.0,
}
}
Expand All @@ -38,8 +40,8 @@ impl Time {
self.delta_seconds = self.delta.as_secs_f32();
}

let duration_since_startup = instant - self.startup;
self.seconds_since_startup = duration_since_startup.as_secs_f64();
self.time_since_startup = instant - self.startup;
self.seconds_since_startup = self.time_since_startup.as_secs_f64();
self.last_update = Some(instant);
}

Expand All @@ -61,7 +63,7 @@ impl Time {
self.delta_seconds_f64
}

/// The time since startup in seconds
/// The time from startup to the last update in seconds
#[inline]
pub fn seconds_since_startup(&self) -> f64 {
self.seconds_since_startup
Expand All @@ -79,8 +81,10 @@ impl Time {
self.last_update
}

/// The ['Duration'] from startup to the last update
#[inline]
pub fn time_since_startup(&self) -> Duration {
Instant::now() - self.startup
self.time_since_startup
}
}

Expand Down Expand Up @@ -110,6 +114,7 @@ mod tests {
assert_eq!(time.startup(), start_instant);
assert_eq!(time.delta_seconds_f64(), 0.0);
assert_eq!(time.seconds_since_startup(), 0.0);
assert_eq!(time.time_since_startup(), Duration::from_secs(0));
assert_eq!(time.delta_seconds(), 0.0);

// Update `time` and check results
Expand All @@ -125,6 +130,10 @@ mod tests {
time.seconds_since_startup(),
(first_update_instant - start_instant).as_secs_f64()
);
assert_eq!(
time.time_since_startup(),
(first_update_instant - start_instant)
);
assert_eq!(time.delta_seconds, 0.0);

// Update `time` again and check results
Expand All @@ -142,6 +151,10 @@ mod tests {
time.seconds_since_startup(),
(second_update_instant - start_instant).as_secs_f64()
);
assert_eq!(
time.time_since_startup(),
(second_update_instant - start_instant)
);
assert_eq!(time.delta_seconds(), time.delta().as_secs_f32());
}
}

0 comments on commit 5e516ab

Please sign in to comment.