summaryrefslogtreecommitdiff
path: root/engine/src/delta_time.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-14 12:34:52 +0200
committerHampusM <hampus@hampusmat.com>2024-04-14 12:35:28 +0200
commit101b455e51f9b702da5517cabe2c3b1086fcb2e7 (patch)
tree470e28acd7a3777dbb4be0208f9cd3177bba52a9 /engine/src/delta_time.rs
parentef7b76ff39d501028852835649f618fcbe17a003 (diff)
feat(engine): use ECS architecture
Diffstat (limited to 'engine/src/delta_time.rs')
-rw-r--r--engine/src/delta_time.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/engine/src/delta_time.rs b/engine/src/delta_time.rs
new file mode 100644
index 0000000..33a2fc8
--- /dev/null
+++ b/engine/src/delta_time.rs
@@ -0,0 +1,32 @@
+use std::time::{Duration, Instant};
+
+use ecs::component::local::Local;
+use ecs::sole::Single;
+use ecs::{Component, Sole};
+
+#[derive(Debug, Clone, Default, Sole)]
+pub struct DeltaTime
+{
+ pub duration: Duration,
+}
+
+#[derive(Debug, Clone, Default, Component)]
+pub struct LastUpdate
+{
+ pub time: Option<Instant>,
+}
+
+/// Updates the current delta time.
+///
+/// # Panics
+/// Will panic if no delta time component exists.
+pub fn update(mut delta_time: Single<DeltaTime>, mut last_update: Local<LastUpdate>)
+{
+ let current_time = Instant::now();
+
+ if let Some(last_update_time) = last_update.time {
+ delta_time.duration = current_time.duration_since(last_update_time);
+ }
+
+ last_update.time = Some(current_time);
+}