Skip to content

Commit 600c3fe

Browse files
committed
keep internal copy of step size to simplify API
1 parent b6e327a commit 600c3fe

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

crates/bevy_time/src/fixed_timestep.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use bevy_app::FixedUpdate;
1414
use bevy_ecs::{system::Resource, world::World};
15-
use bevy_utils::{default, Duration, Instant};
15+
use bevy_utils::{default, Duration};
1616

1717
use crate::{Time, TimeContext};
1818

@@ -28,7 +28,7 @@ pub struct FixedTimestep {
2828
impl Default for FixedTimestep {
2929
fn default() -> Self {
3030
Self {
31-
size: Duration::from_micros(15625),
31+
size: Self::DEFAULT_STEP_SIZE,
3232
steps: 0,
3333
overstep: Duration::ZERO,
3434
max_steps_per_update: u32::MAX,
@@ -37,6 +37,9 @@ impl Default for FixedTimestep {
3737
}
3838

3939
impl FixedTimestep {
40+
/// The default step size.
41+
pub const DEFAULT_STEP_SIZE: Duration = Duration::from_micros(15625);
42+
4043
/// Constructs a new `FixedTimestep` from a [`Duration`].
4144
pub fn new(size: Duration) -> Self {
4245
assert!(!size.is_zero(), "timestep is zero");
@@ -139,11 +142,10 @@ pub fn run_fixed_update_schedule(world: &mut World) {
139142
}
140143

141144
// run schedule however many times
142-
let dt = timestep.size();
143145
for _ in 0..steps {
144146
let mut time = world.resource_mut::<Time>();
145147
assert!(matches!(time.context(), TimeContext::FixedUpdate));
146-
time.tick(dt, Instant::now());
148+
time.update();
147149
schedule.run(world);
148150
}
149151

crates/bevy_time/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ fn time_system(
139139
// update virtual time clock
140140
time.update_with_instant(frame_start);
141141

142+
// apply any step size changes
143+
time.fixed_timestep_size = fixed_timestep.size();
144+
142145
// accumulate
143146
fixed_timestep.accumulate(time.delta());
144147
}

crates/bevy_time/src/time.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bevy_utils::tracing::warn;
44
use bevy_utils::{default, Duration, Instant};
55

66
use crate::clock::Clock;
7+
use crate::fixed_timestep::FixedTimestep;
78

89
/// A clock that tracks how much time has advanced since the last update and since startup.
910
///
@@ -15,6 +16,7 @@ pub struct Time {
1516
context: TimeContext,
1617
update: Clock,
1718
fixed_update: Clock,
19+
pub(crate) fixed_timestep_size: Duration,
1820
paused: bool,
1921
next_paused: Option<bool>,
2022
relative_speed: f64, // using `f64` instead of `f32` to minimize drift from rounding errors
@@ -37,6 +39,7 @@ impl Default for Time {
3739
context: TimeContext::Update,
3840
update: default(),
3941
fixed_update: default(),
42+
fixed_timestep_size: FixedTimestep::DEFAULT_STEP_SIZE,
4043
paused: false,
4144
next_paused: None,
4245
relative_speed: 1.0,
@@ -179,15 +182,11 @@ impl Time {
179182
self.update.update(dt, instant);
180183
}
181184
TimeContext::FixedUpdate => {
182-
warn!("In the `FixedUpdate` context, `Time` can only be advanced via `tick`.");
185+
self.fixed_update.update(self.fixed_timestep_size, instant);
183186
}
184187
}
185188
}
186189

187-
pub(crate) fn tick(&mut self, dt: Duration, instant: Instant) {
188-
self.current_clock_mut().update(dt, instant);
189-
}
190-
191190
/// Applies pending pause or relative speed changes.
192191
///
193192
/// This method is provided for use in tests. Calling this method as part of your app will most

0 commit comments

Comments
 (0)