Skip to content

Commit cb52992

Browse files
committed
Add effective_speed and add warning when skipping
1 parent 52a298c commit cb52992

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

crates/bevy_time/src/virt.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bevy_utils::Duration;
1+
use bevy_utils::{tracing::warn, Duration};
22
use bevy_ecs::system::{Res, ResMut};
33
use bevy_reflect::{FromReflect, Reflect};
44

@@ -62,8 +62,9 @@ use crate::real::Real;
6262
#[derive(Debug, Copy, Clone, Reflect, FromReflect)]
6363
pub struct Virtual {
6464
max_delta: Duration,
65-
relative_speed: f64,
6665
paused: bool,
66+
relative_speed: f64,
67+
effective_speed: f64,
6768
}
6869

6970
impl Time<Virtual> {
@@ -138,8 +139,8 @@ impl Time<Virtual> {
138139
/// Returns `0.0` if the game was paused or what the `relative_speed` value
139140
/// was at the start of this update.
140141
#[inline]
141-
pub fn effective_relative_speed() -> f32 {
142-
todo!()
142+
pub fn effective_speed(&self) -> f32 {
143+
self.context().effective_speed as f32
143144
}
144145

145146
/// Returns the speed the clock advanced relative to your system clock in
@@ -148,8 +149,8 @@ impl Time<Virtual> {
148149
/// Returns `0.0` if the game was paused or what the `relative_speed` value
149150
/// was at the start of this update.
150151
#[inline]
151-
pub fn effective_relative_speed_f64() -> f64 {
152-
todo!()
152+
pub fn effective_speed_f64(&self) -> f64 {
153+
self.context().effective_speed
153154
}
154155

155156
/// Sets the speed the clock advances relative to your system clock, given as an [`f32`].
@@ -207,8 +208,9 @@ impl Default for Virtual {
207208
fn default() -> Self {
208209
Self {
209210
max_delta: Time::<Virtual>::DEFAULT_MAX_DELTA,
210-
relative_speed: 1.0,
211211
paused: false,
212+
relative_speed: 1.0,
213+
effective_speed: 1.0,
212214
}
213215
}
214216
}
@@ -218,17 +220,26 @@ pub fn virtual_time_system(
218220
mut virt: ResMut<Time<Virtual>>,
219221
real: Res<Time<Real>>,
220222
) {
221-
let context = virt.context();
222223
let raw_delta = real.delta();
223-
let clamped_delta = std::cmp::min(raw_delta, context.max_delta);
224-
let delta = if context.paused {
225-
Duration::ZERO
226-
} else if context.relative_speed != 1.0 {
227-
clamped_delta.mul_f64(context.relative_speed)
224+
let max_delta = virt.context().max_delta;
225+
let clamped_delta = if raw_delta > max_delta {
226+
warn!("delta time larger than maximum delta, clamping delta to {:?} and skipping {:?}", max_delta, raw_delta - max_delta);
227+
max_delta
228+
} else {
229+
raw_delta
230+
};
231+
let effective_speed = if virt.context().paused {
232+
0.0
233+
} else {
234+
virt.context().relative_speed
235+
};
236+
let delta = if effective_speed != 1.0 {
237+
clamped_delta.mul_f64(effective_speed)
228238
} else {
229239
// avoid rounding when at normal speed
230240
clamped_delta
231241
};
242+
virt.context_mut().effective_speed = effective_speed;
232243
virt.advance_by(delta);
233244
virt.as_generic().clone_into(current.as_mut());
234245
}

0 commit comments

Comments
 (0)