Skip to content

Commit

Permalink
Add effective_speed and add warning when skipping
Browse files Browse the repository at this point in the history
  • Loading branch information
nakedible committed Jun 28, 2023
1 parent cdeafa8 commit 94bea5c
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions crates/bevy_time/src/virt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_utils::Duration;
use bevy_utils::{tracing::warn, Duration};
use bevy_ecs::system::{Res, ResMut};
use bevy_reflect::{FromReflect, Reflect};

Expand Down Expand Up @@ -62,8 +62,9 @@ use crate::real::Real;
#[derive(Debug, Copy, Clone, Reflect, FromReflect)]
pub struct Virtual {
max_delta: Duration,
relative_speed: f64,
paused: bool,
relative_speed: f64,
effective_speed: f64,
}

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

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

/// Sets the speed the clock advances relative to your system clock, given as an [`f32`].
Expand Down Expand Up @@ -207,8 +208,9 @@ impl Default for Virtual {
fn default() -> Self {
Self {
max_delta: Time::<Virtual>::DEFAULT_MAX_DELTA,
relative_speed: 1.0,
paused: false,
relative_speed: 1.0,
effective_speed: 1.0,
}
}
}
Expand All @@ -218,17 +220,26 @@ pub fn virtual_time_system(
mut virt: ResMut<Time<Virtual>>,
real: Res<Time<Real>>,
) {
let context = virt.context();
let raw_delta = real.delta();
let clamped_delta = std::cmp::min(raw_delta, context.max_delta);
let delta = if context.paused {
Duration::ZERO
} else if context.relative_speed != 1.0 {
clamped_delta.mul_f64(context.relative_speed)
let max_delta = virt.context().max_delta;
let clamped_delta = if raw_delta > max_delta {
warn!("delta time larger than maximum delta, clamping delta to {:?} and skipping {:?}", max_delta, raw_delta - max_delta);
max_delta
} else {
raw_delta
};
let effective_speed = if virt.context().paused {
0.0
} else {
virt.context().relative_speed
};
let delta = if effective_speed != 1.0 {
clamped_delta.mul_f64(effective_speed)
} else {
// avoid rounding when at normal speed
clamped_delta
};
virt.context_mut().effective_speed = effective_speed;
virt.advance_by(delta);
virt.as_generic().clone_into(current.as_mut());
}

0 comments on commit 94bea5c

Please sign in to comment.