diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 10daf75022e312..3e61aeeb72fba6 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -15,7 +15,7 @@ pub use time::*; pub mod prelude { #[doc(hidden)] pub use crate::{ - DefaultTaskPoolOptions, EntityLabels, FixedTimestep, Labels, Name, Time, Timer + DefaultTaskPoolOptions, EntityLabels, FixedTimestep, Labels, Name, Time, Timer, }; } diff --git a/crates/bevy_core/src/time/fixed_timestep.rs b/crates/bevy_core/src/time/fixed_timestep.rs index 48237e638131bd..009172f0b8d6ca 100644 --- a/crates/bevy_core/src/time/fixed_timestep.rs +++ b/crates/bevy_core/src/time/fixed_timestep.rs @@ -1,10 +1,6 @@ -use bevy_ecs::{ - schedule::ShouldRun, - system::{Res, ResMut} -}; -use bevy_utils::Duration; +use bevy_ecs::{schedule::ShouldRun, system::ResMut}; -use crate::Time; +use bevy_utils::Duration; #[derive(Debug, Clone)] pub struct TimeAccumulator { @@ -14,19 +10,16 @@ pub struct TimeAccumulator { impl Default for TimeAccumulator { fn default() -> Self { - Self { - time: Duration::from_secs(0), - steps: 0 + Self { + time: Duration::from_secs(0), + steps: 0, } } } impl TimeAccumulator { pub fn new(time: Duration, steps: usize) -> Self { - Self { - time, - steps - } + Self { time, steps } } /// The number of accrued steps. @@ -43,14 +36,14 @@ impl TimeAccumulator { /// The amount of time accrued toward the next step as [`f32`] % of timestep. pub fn overstep_percentage(&self, timestep: Duration) -> f32 { - self.time.div_duration_f32(timestep) + self.time.as_secs_f32() / timestep.as_secs_f32() } /// The amount of time accrued toward the next step as [`f64`] % of timestep. pub fn overstep_percentage_f64(&self, timestep: Duration) -> f64 { - self.time.div_duration_f64(timestep) + self.time.as_secs_f64() / timestep.as_secs_f64() } - + /// Add to the stored time, then convert into as many steps as possible. pub fn add_time(&mut self, time: Duration, timestep: Duration) { self.time += time; @@ -74,22 +67,19 @@ impl TimeAccumulator { } pub fn reset(&mut self) { - self.time = 0.0; + self.time = Duration::from_secs(0); self.steps = 0; } } - pub struct FixedTimestep; impl FixedTimestep { - pub fn step ( - mut accumulator: ResMut - ) -> ShouldRun { - if let Some(_) = accumulator.sub_step() { + pub fn step(mut accumulator: ResMut) -> ShouldRun { + if accumulator.sub_step().is_some() { ShouldRun::YesAndCheckAgain } else { ShouldRun::No } } -} \ No newline at end of file +} diff --git a/crates/bevy_core/src/time/time.rs b/crates/bevy_core/src/time/time.rs index c6c7b710a41dda..d2186df03a3fd0 100644 --- a/crates/bevy_core/src/time/time.rs +++ b/crates/bevy_core/src/time/time.rs @@ -1,8 +1,7 @@ use bevy_ecs::system::ResMut; use bevy_utils::{Duration, Instant}; -use bevy_ecs::system::ResMut; -use bevy_utils::{Duration, Instant}; +use crate::TimeAccumulator; /// Tracks time elapsed since the previous update and total frame time since the app was started. #[derive(Debug, Clone)] @@ -94,8 +93,8 @@ impl Time { pub fn fixed_delta_seconds_f64(&self) -> f64 { self.fixed_delta_seconds_f64 } - - /// Changes fixed delta. Users are strongly cautioned against modifying + + /// Changes fixed delta. Users are strongly cautioned against modifying /// the step rate this way, except during startup. /// Prefer [`Time::set_speed_multiplier`] to ensure consistent simulation behavior. pub fn set_steps_per_second(&mut self, steps: f32) { @@ -121,6 +120,7 @@ impl Time { /// Set rate app time advances relative to true clock time. #[inline] pub fn set_speed_multiplier(&mut self, speed_multiplier: f32) { + assert!(speed_multiplier > 0.0); self.speed_multiplier = speed_multiplier; } @@ -172,12 +172,12 @@ impl Time { } } -pub(crate) fn time_system( - mut time: ResMut