2020-04-06 03:19:02 +00:00
|
|
|
use legion::prelude::*;
|
2019-12-03 08:30:30 +00:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
pub struct Time {
|
|
|
|
pub delta: Duration,
|
2020-05-31 04:15:39 +00:00
|
|
|
pub instant: Option<Instant>,
|
2020-03-27 22:03:47 +00:00
|
|
|
pub delta_seconds_f64: f64,
|
2020-01-11 10:11:27 +00:00
|
|
|
pub delta_seconds: f32,
|
2020-05-31 04:32:47 +00:00
|
|
|
pub seconds_since_startup: f64,
|
|
|
|
pub startup: Instant,
|
2019-12-03 08:30:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 23:35:38 +00:00
|
|
|
impl Default for Time {
|
|
|
|
fn default() -> Time {
|
2019-12-03 08:30:30 +00:00
|
|
|
Time {
|
|
|
|
delta: Duration::from_secs(0),
|
2020-05-31 04:15:39 +00:00
|
|
|
instant: None,
|
2020-05-31 04:32:47 +00:00
|
|
|
startup: Instant::now(),
|
2020-03-27 22:03:47 +00:00
|
|
|
delta_seconds_f64: 0.0,
|
2020-05-31 04:32:47 +00:00
|
|
|
seconds_since_startup: 0.0,
|
2019-12-03 08:30:30 +00:00
|
|
|
delta_seconds: 0.0,
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 23:35:38 +00:00
|
|
|
}
|
2019-12-03 08:30:30 +00:00
|
|
|
|
2020-05-13 23:35:38 +00:00
|
|
|
impl Time {
|
2020-05-31 04:15:39 +00:00
|
|
|
pub fn update(&mut self) {
|
|
|
|
let now = Instant::now();
|
|
|
|
if let Some(instant) = self.instant {
|
|
|
|
self.delta = now - instant;
|
|
|
|
self.delta_seconds_f64 =
|
|
|
|
self.delta.as_secs() as f64 + (self.delta.subsec_nanos() as f64 / 1.0e9);
|
|
|
|
self.delta_seconds = self.delta_seconds_f64 as f32;
|
|
|
|
}
|
2020-05-31 04:32:47 +00:00
|
|
|
|
|
|
|
let duration_since_startup = now - self.startup;
|
|
|
|
self.seconds_since_startup = duration_since_startup.as_secs() as f64 + (duration_since_startup.subsec_nanos() as f64 / 1.0e9);
|
2020-05-31 04:15:39 +00:00
|
|
|
self.instant = Some(now);
|
2019-12-03 08:30:30 +00:00
|
|
|
}
|
2020-05-31 04:32:47 +00:00
|
|
|
|
|
|
|
pub fn time_since_startup(&self) -> Duration {
|
|
|
|
Instant::now() - self.startup
|
|
|
|
}
|
2020-01-11 10:11:27 +00:00
|
|
|
}
|
2020-04-06 03:19:02 +00:00
|
|
|
|
2020-05-31 04:15:39 +00:00
|
|
|
pub fn timer_system(mut time: ResMut<Time>) {
|
|
|
|
time.update();
|
|
|
|
}
|