Test if our time trackers get updated

This commit is contained in:
Brezak 2024-07-13 18:42:30 +02:00
parent eb4ad65836
commit 62f9cac6f9
No known key found for this signature in database
GPG key ID: CB3891E2B1279D7C
2 changed files with 107 additions and 6 deletions

View file

@ -60,17 +60,17 @@ impl Plugin for TimePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Time>()
.init_resource::<Time<Real>>()
.init_resource::<Time<Virtual>>()
.init_resource::<Time<Fixed>>()
.init_resource::<Time<Virtual>>()
.init_resource::<TimeUpdateStrategy>();
#[cfg(feature = "bevy_reflect")]
{
app.register_type::<Time>()
.register_type::<Timer>()
.register_type::<Time<Real>>()
.register_type::<Time<Virtual>>()
.register_type::<Time<Fixed>>()
.register_type::<Timer>();
.register_type::<Time<Virtual>>();
}
app.add_systems(First, time_system.in_set(TimeSystem))
@ -79,8 +79,10 @@ impl Plugin for TimePlugin {
app.configure_sets(First, UpdateTimeTrackers.after(TimeSystem));
app.register_time_context::<Real>()
.register_time_context::<Fixed>()
.register_time_context::<Virtual>()
.register_time_context::<Fixed>();
.register_time_tracker::<MixedTimer>()
.register_time_tracker::<MixedStopwatch>();
// ensure the events are not dropped until `FixedMain` systems can observe them
app.add_systems(FixedPostUpdate, signal_event_update_system);
@ -161,7 +163,10 @@ pub fn time_system(
#[cfg(test)]
mod tests {
use crate::{Fixed, Time, TimePlugin, TimeUpdateStrategy, Virtual};
use crate::{
Fixed, MixedStopwatch, MixedTimer, Real, Time, TimePlugin, TimeUpdateStrategy, Timer,
UpdatingStopwatch, UpdatingTimer, Virtual,
};
use bevy_app::{App, FixedUpdate, Startup, Update};
use bevy_ecs::{
event::{Event, EventReader, EventRegistry, EventWriter, Events, ShouldUpdateEvents},
@ -379,4 +384,100 @@ mod tests {
}
}
}
#[test]
fn time_trackers_track_time() {
// Setup the timesteps so that all the times (fixed, virtual and real) have different values after one update.
let fixed_timestep = Time::<Fixed>::default().timestep();
let virtual_timestep = fixed_timestep + fixed_timestep / 2;
let real_timestep = virtual_timestep * 2;
let mut app = App::new();
app.add_plugins(TimePlugin)
.insert_resource(TimeUpdateStrategy::ManualDuration(real_timestep));
app.world_mut()
.resource_mut::<Time<Virtual>>()
.set_relative_speed(0.5);
let one_second_timer = Timer::new(Duration::from_secs(1), crate::TimerMode::Once);
let tracking_entity = app
.world_mut()
.spawn((
UpdatingStopwatch::<Virtual>::default(),
UpdatingStopwatch::<Fixed>::default(),
UpdatingStopwatch::<Real>::default(),
MixedStopwatch::default(),
UpdatingTimer::<Virtual>::new(one_second_timer.clone()),
UpdatingTimer::<Fixed>::new(one_second_timer.clone()),
UpdatingTimer::<Real>::new(one_second_timer.clone()),
MixedTimer::new(one_second_timer.clone()),
))
.id();
// Time does not increment on the first update.
app.update();
// Time will increment now.
app.update();
let tracking_entity = app.world().entity(tracking_entity);
assert_eq!(
tracking_entity
.get::<UpdatingStopwatch::<Virtual>>()
.unwrap()
.elapsed(),
virtual_timestep
);
assert_eq!(
tracking_entity
.get::<UpdatingStopwatch::<Fixed>>()
.unwrap()
.elapsed(),
fixed_timestep
);
assert_eq!(
tracking_entity
.get::<UpdatingStopwatch::<Real>>()
.unwrap()
.elapsed(),
real_timestep
);
assert_eq!(
tracking_entity
.get::<UpdatingTimer::<Virtual>>()
.unwrap()
.elapsed(),
virtual_timestep
);
assert_eq!(
tracking_entity
.get::<UpdatingTimer::<Fixed>>()
.unwrap()
.elapsed(),
fixed_timestep
);
assert_eq!(
tracking_entity
.get::<UpdatingTimer::<Real>>()
.unwrap()
.elapsed(),
real_timestep
);
let timer = tracking_entity.get::<MixedTimer>().unwrap();
// Mixed timer should be tracking virtual time right now.
assert_eq!(timer.elapsed(), virtual_timestep);
let (virt, fixed) = timer.timers();
assert_eq!(virt.elapsed(), virtual_timestep);
assert_eq!(fixed.elapsed(), fixed_timestep);
let watch = tracking_entity.get::<MixedStopwatch>().unwrap();
// Mixed stopwatch should be tracking virtual time right now.
assert_eq!(watch.elapsed(), virtual_timestep);
let (virt, fixed) = timer.timers();
assert_eq!(virt.elapsed(), virtual_timestep);
assert_eq!(fixed.elapsed(), fixed_timestep);
}
}

View file

@ -227,7 +227,7 @@ impl Time<Virtual> {
}
/// Updates the elapsed duration of `self` by `raw_delta`, up to the `max_delta`.
fn advance_with_raw_delta(&mut self, raw_delta: Duration) {
pub fn advance_with_raw_delta(&mut self, raw_delta: Duration) {
let max_delta = self.context().max_delta;
let clamped_delta = if raw_delta > max_delta {
debug!(