From f9da443880a61d45754f5f0370d320d96451a8e5 Mon Sep 17 00:00:00 2001 From: Joy <51241057+maniwani@users.noreply.github.com> Date: Sat, 6 Nov 2021 14:48:11 -0700 Subject: [PATCH] Removed live_real_ fns, changed real_ to raw_, added Time.fixedTime equiv. with tests When comparing dilated and raw delta/elapsed time measurements, be aware that `relative_speed` != 1.0 may have rounding errors, so the ratios might not be perfect. --- crates/bevy_core/src/time/fixed_timestep.rs | 54 +-- crates/bevy_core/src/time/time.rs | 369 +++++++++++++------- examples/scene/scene.rs | 4 +- examples/wasm/winit_wasm.rs | 2 +- 4 files changed, 270 insertions(+), 159 deletions(-) diff --git a/crates/bevy_core/src/time/fixed_timestep.rs b/crates/bevy_core/src/time/fixed_timestep.rs index 09f77b684af223..b98cb392b88ac3 100644 --- a/crates/bevy_core/src/time/fixed_timestep.rs +++ b/crates/bevy_core/src/time/fixed_timestep.rs @@ -1,39 +1,48 @@ use bevy_ecs::{schedule::ShouldRun, system::ResMut}; - use bevy_utils::Duration; +use crate::Time; + #[derive(Debug, Clone)] pub struct FixedTimestepState { time: Duration, - steps: usize, + steps: u32, } impl Default for FixedTimestepState { fn default() -> Self { Self { - time: Duration::from_secs(0), + time: Duration::ZERO, steps: 0, } } } impl FixedTimestepState { - pub fn new(time: Duration, steps: usize) -> Self { + pub fn new(time: Duration, steps: u32) -> Self { Self { time, steps } } /// The number of accrued steps. #[inline] - pub fn steps(&self) -> usize { + pub fn steps(&self) -> u32 { self.steps } - /// The amount of time accrued toward the next step as [`Duration`]. + /// The amount of time accrued toward new steps as [`Duration`]. #[inline] - pub fn overstep(&self) -> Duration { + pub fn time(&self) -> Duration { self.time } + pub fn set_steps(&mut self, steps: u32) { + self.steps = steps; + } + + pub fn set_time(&mut self, time: Duration) { + self.time = time; + } + /// The amount of time accrued toward the next step as [`f32`] % of timestep. pub fn overstep_percentage(&self, timestep: Duration) -> f32 { self.time.as_secs_f32() / timestep.as_secs_f32() @@ -44,7 +53,7 @@ impl FixedTimestepState { self.time.as_secs_f64() / timestep.as_secs_f64() } - /// Add to the stored time, then convert into as many steps as possible. + /// Add to the accrued time, then convert into as many steps as possible. pub fn add_time(&mut self, time: Duration, timestep: Duration) { self.time += time; while self.time >= timestep { @@ -54,22 +63,14 @@ impl FixedTimestepState { } /// Consume a stored step (if any). - pub fn sub_step(&mut self) -> Option { + pub fn sub_step(&mut self) -> Option { let remaining = self.steps.checked_sub(1); self.steps = self.steps.saturating_sub(1); remaining } - pub fn set_time(&mut self, time: Duration) { - self.time = time; - } - - pub fn set_steps(&mut self, steps: usize) { - self.steps = steps; - } - pub fn reset(&mut self) { - self.time = Duration::from_secs(0); + self.time = Duration::ZERO; self.steps = 0; } } @@ -77,8 +78,9 @@ impl FixedTimestepState { pub struct FixedTimestep; impl FixedTimestep { - pub fn step(mut accumulator: ResMut) -> ShouldRun { + pub fn step(mut time: ResMut