use std::ops::Deref; use std::time::{Duration, Instant}; use engine_ecs_macros::Sole; use crate::sole::Single; #[derive(Debug, Sole)] #[non_exhaustive] pub struct Time { /// Time passed since the beginning of the previous tick. pub delta_time: Duration, /// Time at the beginning of the current tick. pub tick_begin: Option, } impl Deref for Single<'_, Time> { type Target = Time; fn deref(&self) -> &Self::Target { let Ok(time) = self.get() else { // Time is always created by World::new unreachable!(); }; time } } pub(crate) fn update(time: &mut Time) { let current_time = Instant::now(); let tick_begin = time.tick_begin.get_or_insert(current_time); time.delta_time = current_time.duration_since(*tick_begin); time.tick_begin = Some(current_time); }