use bevy_ecs::{reflect::ReflectResource, system::Resource}; use bevy_reflect::Reflect; use bevy_utils::{Duration, Instant}; /// Tracks elapsed time since the last update and since the App has started #[derive(Resource, Reflect, Debug, Clone)] #[reflect(Resource)] pub struct Time { delta: Duration, last_update: Option, delta_seconds_f64: f64, delta_seconds: f32, seconds_since_startup: f64, time_since_startup: Duration, startup: Instant, } impl Default for Time { fn default() -> Time { Time { delta: Duration::from_secs(0), last_update: None, startup: Instant::now(), delta_seconds_f64: 0.0, seconds_since_startup: 0.0, time_since_startup: Duration::from_secs(0), delta_seconds: 0.0, } } } impl Time { /// Updates the internal time measurements. /// /// Calling this method on the [`Time`] resource as part of your app will most likely result in /// inaccurate timekeeping, as the resource is ordinarily managed by the /// [`TimePlugin`](crate::TimePlugin). pub fn update(&mut self) { self.update_with_instant(Instant::now()); } /// Update time with a specified [`Instant`] /// /// This method is provided for use in tests. Calling this method on the [`Time`] resource as /// part of your app will most likely result in inaccurate timekeeping, as the 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