Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify FixedTime and Time (proof-of-concept) #8930

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions crates/bevy_time/src/clock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use bevy_reflect::{FromReflect, Reflect};
use bevy_utils::Duration;

#[derive(Debug, Copy, Clone, Reflect, FromReflect)]
pub struct Clock {
wrap_seconds: u64,
delta: Duration,
delta_seconds: f32,
delta_seconds_f64: f64,
elapsed: Duration,
elapsed_seconds: f32,
elapsed_seconds_f64: f64,
elapsed_wrapped: Duration,
elapsed_seconds_wrapped: f32,
elapsed_seconds_wrapped_f64: f64,
}

impl Clock {
const DEFAULT_WRAP_SECONDS: u64 = 3600; // 1 hour

pub fn new(wrap_seconds: u64) -> Clock {
Clock {
wrap_seconds,
delta: Duration::ZERO,
delta_seconds: 0.0,
delta_seconds_f64: 0.0,
elapsed: Duration::ZERO,
elapsed_seconds: 0.0,
elapsed_seconds_f64: 0.0,
elapsed_wrapped: Duration::ZERO,
elapsed_seconds_wrapped: 0.0,
elapsed_seconds_wrapped_f64: 0.0,
}
}

pub fn advance_by(&mut self, delta: Duration) {
self.delta = delta;
self.delta_seconds = self.delta.as_secs_f32();
self.delta_seconds_f64 = self.delta.as_secs_f64();
self.elapsed += delta;
self.elapsed_seconds = self.elapsed.as_secs_f32();
self.elapsed_seconds_f64 = self.elapsed.as_secs_f64();
self.elapsed_wrapped = Duration::new(self.elapsed.as_secs() % self.wrap_seconds, self.elapsed.subsec_nanos());
self.elapsed_seconds_wrapped = self.elapsed_wrapped.as_secs_f32();
self.elapsed_seconds_wrapped_f64 = self.elapsed_wrapped.as_secs_f64();
}

pub fn advance_to(&mut self, elapsed: Duration) {
self.advance_by(elapsed - self.elapsed)
}

pub fn wrap_period(&self) -> Duration {
Duration::from_secs(self.wrap_seconds)
}

pub fn set_wrap_period(&mut self, wrap_period: Duration) {
assert!(!wrap_period.is_zero(), "division by zero");
assert_eq!(wrap_period.subsec_nanos(), 0, "wrap period must be integral seconds");
self.wrap_seconds = wrap_period.as_secs();
self.elapsed_wrapped = Duration::new(self.elapsed.as_secs() % self.wrap_seconds, self.elapsed.subsec_nanos());
self.elapsed_seconds_wrapped = self.elapsed_wrapped.as_secs_f32();
self.elapsed_seconds_wrapped_f64 = self.elapsed_wrapped.as_secs_f64();
}

pub fn delta(&self) -> Duration {
self.delta
}

pub fn delta_seconds(&self) -> f32 {
self.delta_seconds
}

pub fn delta_seconds_f64(&self) -> f64 {
self.delta_seconds_f64
}

pub fn elapsed(&self) -> Duration {
self.elapsed
}

pub fn elapsed_seconds(&self) -> f32 {
self.elapsed_seconds
}

pub fn elapsed_seconds_f64(&self) -> f64 {
self.elapsed_seconds_f64
}

pub fn elapsed_wrapped(&self) -> Duration {
self.elapsed_wrapped
}

pub fn elapsed_seconds_wrapped(&self) -> f32 {
self.elapsed_seconds_wrapped
}

pub fn elapsed_seconds_wrapped_f64(&self) -> f64 {
self.elapsed_seconds_wrapped_f64
}
}

impl Default for Clock {
fn default() -> Self {
Self::new(Self::DEFAULT_WRAP_SECONDS)
}
}
8 changes: 4 additions & 4 deletions crates/bevy_time/src/common_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{fixed_timestep::FixedTime, Time, Timer, TimerMode};
use crate::{Time, Timer, TimerMode};
use bevy_ecs::system::Res;
use bevy_utils::Duration;

Expand Down Expand Up @@ -66,10 +66,10 @@ pub fn on_timer(duration: Duration) -> impl FnMut(Res<Time>) -> bool + Clone {
/// Note that this run condition may not behave as expected if `duration` is smaller
/// than the fixed timestep period, since the timer may complete multiple times in
/// one fixed update.
pub fn on_fixed_timer(duration: Duration) -> impl FnMut(Res<FixedTime>) -> bool + Clone {
pub fn on_fixed_timer(duration: Duration) -> impl FnMut(Res<Time>) -> bool + Clone {
let mut timer = Timer::new(duration, TimerMode::Repeating);
move |time: Res<FixedTime>| {
timer.tick(time.period);
move |time: Res<Time>| {
timer.tick(time.fixed_period());
timer.just_finished()
}
}
Expand Down
158 changes: 0 additions & 158 deletions crates/bevy_time/src/fixed_timestep.rs

This file was deleted.

8 changes: 3 additions & 5 deletions crates/bevy_time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

/// Common run conditions
pub mod common_conditions;
pub mod fixed_timestep;
mod stopwatch;
#[allow(clippy::module_inception)]
mod time;
mod timer;
mod clock;

use fixed_timestep::FixedTime;
pub use stopwatch::*;
pub use time::*;
pub use timer::*;
Expand All @@ -20,13 +19,13 @@ use crossbeam_channel::{Receiver, Sender};
pub mod prelude {
//! The Bevy Time Prelude.
#[doc(hidden)]
pub use crate::{fixed_timestep::FixedTime, Time, Timer, TimerMode};
pub use crate::{Time, Timer, TimerMode};
}

use bevy_app::{prelude::*, RunFixedUpdateLoop};
use bevy_ecs::prelude::*;

use crate::fixed_timestep::run_fixed_update_schedule;
use crate::time::run_fixed_update_schedule;

/// Adds time functionality to Apps.
#[derive(Default)]
Expand All @@ -44,7 +43,6 @@ impl Plugin for TimePlugin {
.register_type::<Timer>()
.register_type::<Time>()
.register_type::<Stopwatch>()
.init_resource::<FixedTime>()
.add_systems(First, time_system.in_set(TimeSystem))
.add_systems(RunFixedUpdateLoop, run_fixed_update_schedule);

Expand Down
Loading