Skip to content

Commit

Permalink
keep internal copy of step size to simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Jun 24, 2023
1 parent b6e327a commit 600c3fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions crates/bevy_time/src/fixed_timestep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use bevy_app::FixedUpdate;
use bevy_ecs::{system::Resource, world::World};
use bevy_utils::{default, Duration, Instant};
use bevy_utils::{default, Duration};

use crate::{Time, TimeContext};

Expand All @@ -28,7 +28,7 @@ pub struct FixedTimestep {
impl Default for FixedTimestep {
fn default() -> Self {
Self {
size: Duration::from_micros(15625),
size: Self::DEFAULT_STEP_SIZE,
steps: 0,
overstep: Duration::ZERO,
max_steps_per_update: u32::MAX,
Expand All @@ -37,6 +37,9 @@ impl Default for FixedTimestep {
}

impl FixedTimestep {
/// The default step size.
pub const DEFAULT_STEP_SIZE: Duration = Duration::from_micros(15625);

/// Constructs a new `FixedTimestep` from a [`Duration`].
pub fn new(size: Duration) -> Self {
assert!(!size.is_zero(), "timestep is zero");
Expand Down Expand Up @@ -139,11 +142,10 @@ pub fn run_fixed_update_schedule(world: &mut World) {
}

// run schedule however many times
let dt = timestep.size();
for _ in 0..steps {
let mut time = world.resource_mut::<Time>();
assert!(matches!(time.context(), TimeContext::FixedUpdate));
time.tick(dt, Instant::now());
time.update();
schedule.run(world);
}

Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ fn time_system(
// update virtual time clock
time.update_with_instant(frame_start);

// apply any step size changes
time.fixed_timestep_size = fixed_timestep.size();

// accumulate
fixed_timestep.accumulate(time.delta());
}
9 changes: 4 additions & 5 deletions crates/bevy_time/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_utils::tracing::warn;
use bevy_utils::{default, Duration, Instant};

use crate::clock::Clock;
use crate::fixed_timestep::FixedTimestep;

/// A clock that tracks how much time has advanced since the last update and since startup.
///
Expand All @@ -15,6 +16,7 @@ pub struct Time {
context: TimeContext,
update: Clock,
fixed_update: Clock,
pub(crate) fixed_timestep_size: Duration,
paused: bool,
next_paused: Option<bool>,
relative_speed: f64, // using `f64` instead of `f32` to minimize drift from rounding errors
Expand All @@ -37,6 +39,7 @@ impl Default for Time {
context: TimeContext::Update,
update: default(),
fixed_update: default(),
fixed_timestep_size: FixedTimestep::DEFAULT_STEP_SIZE,
paused: false,
next_paused: None,
relative_speed: 1.0,
Expand Down Expand Up @@ -179,15 +182,11 @@ impl Time {
self.update.update(dt, instant);
}
TimeContext::FixedUpdate => {
warn!("In the `FixedUpdate` context, `Time` can only be advanced via `tick`.");
self.fixed_update.update(self.fixed_timestep_size, instant);
}
}
}

pub(crate) fn tick(&mut self, dt: Duration, instant: Instant) {
self.current_clock_mut().update(dt, instant);
}

/// Applies pending pause or relative speed changes.
///
/// This method is provided for use in tests. Calling this method as part of your app will most
Expand Down

0 comments on commit 600c3fe

Please sign in to comment.