use bevy_ecs::{reflect::ReflectResource, system::Resource}; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_utils::{Duration, Instant}; /// A clock that tracks how much it has advanced (and how much real time has elapsed) since /// its previous update and since its creation. /// /// See [`TimeUpdateStrategy`], which allows you to customize the way that this is updated each frame. /// /// [`TimeUpdateStrategy`]: crate::TimeUpdateStrategy #[derive(Resource, Reflect, Debug, Clone)] #[reflect(Resource, Default)] pub struct Time { startup: Instant, first_update: Option, last_update: Option, // pausing paused: bool, // scaling relative_speed: f64, // using `f64` instead of `f32` to minimize drift from rounding errors delta: Duration, delta_seconds: f32, delta_seconds_f64: f64, elapsed: Duration, elapsed_seconds: f32, elapsed_seconds_f64: f64, raw_delta: Duration, raw_delta_seconds: f32, raw_delta_seconds_f64: f64, raw_elapsed: Duration, raw_elapsed_seconds: f32, raw_elapsed_seconds_f64: f64, // wrapping wrap_period: Duration, elapsed_wrapped: Duration, elapsed_seconds_wrapped: f32, elapsed_seconds_wrapped_f64: f64, raw_elapsed_wrapped: Duration, raw_elapsed_seconds_wrapped: f32, raw_elapsed_seconds_wrapped_f64: f64, } impl Default for Time { fn default() -> Self { Self { startup: Instant::now(), first_update: None, last_update: None, paused: false, relative_speed: 1.0, delta: Duration::ZERO, delta_seconds: 0.0, delta_seconds_f64: 0.0, elapsed: Duration::ZERO, elapsed_seconds: 0.0, elapsed_seconds_f64: 0.0, raw_delta: Duration::ZERO, raw_delta_seconds: 0.0, raw_delta_seconds_f64: 0.0, raw_elapsed: Duration::ZERO, raw_elapsed_seconds: 0.0, raw_elapsed_seconds_f64: 0.0, wrap_period: Duration::from_secs(3600), // 1 hour elapsed_wrapped: Duration::ZERO, elapsed_seconds_wrapped: 0.0, elapsed_seconds_wrapped_f64: 0.0, raw_elapsed_wrapped: Duration::ZERO, raw_elapsed_seconds_wrapped: 0.0, raw_elapsed_seconds_wrapped_f64: 0.0, } } } impl Time { /// Constructs a new `Time` instance with a specific startup `Instant`. pub fn new(startup: Instant) -> Self { Self { startup, ..Default::default() } } /// Updates the internal time measurements. /// /// Calling this method as part of your app will most likely result in inaccurate timekeeping, /// as the `Time` resource is ordinarily managed by the [`TimePlugin`](crate::TimePlugin). pub fn update(&mut self) { let now = Instant::now(); self.update_with_instant(now); } /// Updates time with a specified [`Instant`]. /// /// This method is provided for use in tests. Calling this method as part of your app will most /// likely result in inaccurate timekeeping, as the `Time` resource is ordinarily managed by the /// [`TimePlugin`](crate::TimePlugin). /// /// # Examples /// /// ``` /// # use bevy_time::prelude::*; /// # use bevy_ecs::prelude::*; /// # use bevy_utils::Duration; /// # fn main () { /// # test_health_system(); /// # } /// #[derive(Resource)] /// struct Health { /// // Health value between 0.0 and 1.0 /// health_value: f32, /// } /// /// fn health_system(time: Res