bevy/crates/bevy_core/src/time.rs

42 lines
926 B
Rust
Raw Normal View History

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,
pub instant: 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,
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),
instant: Instant::now(),
2020-03-27 22:03:47 +00:00
delta_seconds_f64: 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 {
2019-12-03 08:30:30 +00:00
pub fn start(&mut self) {
self.instant = Instant::now();
}
pub fn stop(&mut self) {
self.delta = Instant::now() - self.instant;
2020-03-27 22:03:47 +00:00
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;
2019-12-03 08:30:30 +00:00
}
2020-01-11 10:11:27 +00:00
}
2020-04-06 03:19:02 +00:00
2020-05-14 00:52:47 +00:00
pub fn start_timer_system(mut time: ResMut<Time>) {
time.start();
2020-04-06 03:19:02 +00:00
}
2020-05-14 00:52:47 +00:00
pub fn stop_timer_system(mut time: ResMut<Time>) {
time.stop();
2020-05-01 05:30:51 +00:00
}