summaryrefslogtreecommitdiff
path: root/engine-ecs/src/time.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine-ecs/src/time.rs')
-rw-r--r--engine-ecs/src/time.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/engine-ecs/src/time.rs b/engine-ecs/src/time.rs
new file mode 100644
index 0000000..3f888cb
--- /dev/null
+++ b/engine-ecs/src/time.rs
@@ -0,0 +1,43 @@
+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<Instant>,
+}
+
+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);
+}