-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Time into three separate clocks
- Loading branch information
Showing
7 changed files
with
266 additions
and
846 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use bevy_utils::Duration; | ||
use bevy_ecs::world::World; | ||
use bevy_ecs::{reflect::ReflectResource, system::Resource}; | ||
use bevy_reflect::{FromReflect, Reflect}; | ||
|
||
use crate::FixedUpdate; | ||
use crate::time::Time; | ||
use crate::virt::Virtual; | ||
|
||
#[derive(Resource, Debug, Copy, Clone, Reflect, FromReflect)] | ||
#[reflect(Resource)] | ||
pub struct Fixed { | ||
period: Duration, | ||
accumulated: Duration, | ||
} | ||
|
||
impl Time<Fixed> { | ||
pub fn period(&self) -> Duration { | ||
self.context().period | ||
} | ||
|
||
pub fn set_period(&mut self, period: Duration) { | ||
assert_ne!(period, Duration::ZERO, "attempted to set fixed time period to zero"); | ||
self.context_mut().period = period; | ||
} | ||
|
||
fn expend(&mut self) -> bool { | ||
let period = self.period(); | ||
if let Some(new_value) = self.context_mut().accumulated.checked_sub(period) { | ||
// reduce accumulated and increase elapsed by period | ||
self.context_mut().accumulated = new_value; | ||
self.advance_by(period); | ||
true | ||
} else { | ||
// no more periods left in accumulated | ||
false | ||
} | ||
} | ||
} | ||
|
||
impl Default for Fixed { | ||
fn default() -> Self { | ||
Self { | ||
period: Duration::from_secs_f64(1. / 60.), | ||
accumulated: Duration::ZERO, | ||
} | ||
} | ||
} | ||
|
||
pub fn run_fixed_update_schedule( | ||
world: &mut World | ||
) { | ||
let delta = world.resource::<Time<Virtual>>().delta(); | ||
world.resource_mut::<Time<Fixed>>().context_mut().accumulated += delta; | ||
|
||
// Run the schedule until we run out of accumulated time | ||
let _ = world.try_schedule_scope(FixedUpdate, |world, schedule| { | ||
while world.resource_mut::<Time<Fixed>>().expend() { | ||
world.resource::<Time<Fixed>>().as_current().clone_into(world.resource_mut::<Time>().as_mut()); | ||
schedule.run(world); | ||
} | ||
}); | ||
world.resource::<Time<Virtual>>().as_current().clone_into(world.resource_mut::<Time>().as_mut()); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use bevy_utils::{Duration, Instant}; | ||
use bevy_ecs::{reflect::ReflectResource, system::Resource}; | ||
use bevy_reflect::{FromReflect, Reflect}; | ||
|
||
use crate::time::Time; | ||
|
||
#[derive(Resource, Debug, Copy, Clone, Reflect, FromReflect)] | ||
#[reflect(Resource)] | ||
pub struct Real { | ||
startup: Instant, | ||
first_update: Option<Instant>, | ||
last_update: Option<Instant>, | ||
} | ||
|
||
impl Default for Real { | ||
fn default() -> Self { | ||
Self { | ||
startup: Instant::now(), | ||
first_update: None, | ||
last_update: None, | ||
} | ||
} | ||
} | ||
|
||
impl Time<Real> { | ||
pub fn update(&mut self) { | ||
let instant = Instant::now(); | ||
self.update_with_instant(instant); | ||
} | ||
|
||
pub fn update_with_duration(&mut self, duration: Duration) { | ||
let last_update = self.context().last_update.unwrap_or(self.context().startup); | ||
self.update_with_instant(last_update + duration); | ||
} | ||
|
||
pub fn update_with_instant(&mut self, instant: Instant) { | ||
let Some(last_update) = self.context().last_update else { | ||
let mut context = self.context_mut(); | ||
context.first_update = Some(instant); | ||
context.last_update = Some(instant); | ||
return; | ||
}; | ||
let delta = instant - last_update; | ||
self.advance_by(delta); | ||
} | ||
} |
Oops, something went wrong.