From b7f0c2f965a7d460022d157ff149bf1ea498b9b4 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 16 Oct 2024 22:15:24 +0200 Subject: feat(ecs): add stats sole containing current tick --- ecs/src/lib.rs | 24 +++++++++++++++++++++++- ecs/src/stats.rs | 10 ++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 ecs/src/stats.rs (limited to 'ecs/src') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 40b2021..452f657 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -23,6 +23,7 @@ use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::{Lock, WriteGuard}; use crate::query::options::Options as QueryOptions; use crate::sole::Sole; +use crate::stats::Stats; use crate::system::{System, TypeErased as TypeErasedSystem}; use crate::tuple::Reduce as TupleReduce; use crate::type_name::TypeName; @@ -36,6 +37,7 @@ pub mod lock; pub mod query; pub mod relationship; pub mod sole; +pub mod stats; pub mod system; pub mod tuple; pub mod type_name; @@ -60,7 +62,13 @@ impl World #[must_use] pub fn new() -> Self { - Self::default() + let mut world = Self::default(); + + world + .add_sole(Stats::default()) + .expect("World already has stats sole"); + + world } /// Creates a new entity with the given components. @@ -267,6 +275,20 @@ impl World if self.stop.load(Ordering::Relaxed) { break; } + + let mut stats_lock = self + .data + .sole_storage + .get::() + .expect("No stats sole found") + .write_nonblock() + .expect("Failed to aquire read-write stats sole lock"); + + let stats = stats_lock + .downcast_mut::() + .expect("Casting stats sole to Stats type failed"); + + stats.current_tick += 1; } } diff --git a/ecs/src/stats.rs b/ecs/src/stats.rs new file mode 100644 index 0000000..410610f --- /dev/null +++ b/ecs/src/stats.rs @@ -0,0 +1,10 @@ +use ecs_macros::Sole; + +use crate as ecs; + +#[derive(Debug, Default, Sole)] +#[non_exhaustive] +pub struct Stats +{ + pub current_tick: u64, +} -- cgit v1.2.3-18-g5258