From 097a55948cf85aae1c9cd35503155a41e032803e Mon Sep 17 00:00:00 2001 From: Amber Kowalski Date: Sat, 28 Nov 2020 16:08:31 -0500 Subject: [PATCH] Refactor Time API and internals (#934) Refactor Time API and internals --- CHANGELOG.md | 9 ++ crates/bevy_core/src/time/time.rs | 119 ++++++++++++++++-- .../src/frame_time_diagnostics_plugin.rs | 4 +- .../src/print_diagnostics_plugin.rs | 4 +- examples/2d/contributors.rs | 4 +- examples/2d/sprite_sheet.rs | 2 +- examples/3d/parenting.rs | 2 +- examples/3d/spawner.rs | 4 +- examples/3d/z_sort_debug.rs | 2 +- examples/app/plugin.rs | 2 +- examples/ecs/event.rs | 2 +- examples/ecs/hierarchy.rs | 8 +- examples/ecs/timers.rs | 6 +- examples/game/breakout.rs | 4 +- examples/tools/bevymark.rs | 8 +- examples/ui/font_atlas_debug.rs | 2 +- examples/wasm/winit_wasm.rs | 2 +- examples/window/window_settings.rs | 2 +- 18 files changed, 146 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05e7dbaaebd89..9783fb107eb6b 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 0baee828e2bf0..d2f32411bceef 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