diff --git a/CHANGELOG.md b/CHANGELOG.md index 05e7dbaaeb..9783fb107e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,18 @@ current changes on git with [previous release tags][git_tag_comparison]. ### Changed - [Breaking changes to timer API][914] + - Created getters and setters rather than exposing struct members. - [Removed timer auto-ticking system][931] + - Added an example of how to tick timers manually. +- [Breaking changes to Time API][934] + - Created getters to get `Time` state and made members private. + - Modifying `Time`'s values directly is no longer possible outside of bevy. ### Fixed +[914]: https://github.com/bevyengine/bevy/pull/914 +[931]: https://github.com/bevyengine/bevy/pull/931 +[934]: https://github.com/bevyengine/bevy/pull/934 + ## Version 0.3.0 (2020-11-03) diff --git a/crates/bevy_core/src/time/time.rs b/crates/bevy_core/src/time/time.rs index 0baee828e2..d2f32411bc 100644 --- a/crates/bevy_core/src/time/time.rs +++ b/crates/bevy_core/src/time/time.rs @@ -4,19 +4,19 @@ use bevy_utils::{Duration, Instant}; /// Tracks elapsed time since the last update and since the App has started #[derive(Debug)] pub struct Time { - pub delta: Duration, - pub instant: Option, - pub delta_seconds_f64: f64, - pub delta_seconds: f32, - pub seconds_since_startup: f64, - pub startup: Instant, + delta: Duration, + last_update: Option, + delta_seconds_f64: f64, + delta_seconds: f32, + seconds_since_startup: f64, + startup: Instant, } impl Default for Time { fn default() -> Time { Time { delta: Duration::from_secs(0), - instant: None, + last_update: None, startup: Instant::now(), delta_seconds_f64: 0.0, seconds_since_startup: 0.0, @@ -28,15 +28,55 @@ impl Default for Time { impl Time { pub fn update(&mut self) { let now = Instant::now(); - if let Some(instant) = self.instant { - self.delta = now - instant; + self.update_with_instant(now); + } + + pub(crate) fn update_with_instant(&mut self, instant: Instant) { + if let Some(last_update) = self.last_update { + self.delta = instant - last_update; self.delta_seconds_f64 = self.delta.as_secs_f64(); self.delta_seconds = self.delta.as_secs_f32(); } - let duration_since_startup = now - self.startup; + let duration_since_startup = instant - self.startup; self.seconds_since_startup = duration_since_startup.as_secs_f64(); - self.instant = Some(now); + self.last_update = Some(instant); + } + + /// The delta between the current tick and last tick as a [`Duration`] + #[inline] + pub fn delta(&self) -> Duration { + self.delta + } + + /// The delta between the current and last tick as [`f32`] seconds + #[inline] + pub fn delta_seconds(&self) -> f32 { + self.delta_seconds + } + + /// The delta between the current and last tick as [`f64`] seconds + #[inline] + pub fn delta_seconds_f64(&self) -> f64 { + self.delta_seconds_f64 + } + + /// The time since startup in seconds + #[inline] + pub fn seconds_since_startup(&self) -> f64 { + self.seconds_since_startup + } + + /// The [`Instant`] the app was started + #[inline] + pub fn startup(&self) -> Instant { + self.startup + } + + /// The ['Instant'] when [`Time::update`] was last called, if it exists + #[inline] + pub fn last_update(&self) -> Option { + self.last_update } pub fn time_since_startup(&self) -> Duration { @@ -47,3 +87,60 @@ impl Time { pub(crate) fn time_system(mut time: ResMut