diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-14 20:05:30 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-14 20:05:30 +0200 |
commit | 07aa59a122cc5e14d2fb2e2c6e3d8f82e4397bde (patch) | |
tree | 0ac63f5262d97d3d7f50ab1c72d1ace61935608c /ecs/src/event.rs | |
parent | e9074af15cae7b3c354e524e9fa78cbddb20ff84 (diff) |
feat(ecs): add component added event
Diffstat (limited to 'ecs/src/event.rs')
-rw-r--r-- | ecs/src/event.rs | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/ecs/src/event.rs b/ecs/src/event.rs index 93d4545..0caa56f 100644 --- a/ecs/src/event.rs +++ b/ecs/src/event.rs @@ -1,10 +1,21 @@ use std::any::TypeId; use std::fmt::Debug; -use std::hash::Hash; +use std::hash::{DefaultHasher, Hash, Hasher}; use seq_macro::seq; -pub trait Event: Debug + 'static {} +pub mod component; + +pub trait Event: Debug + 'static +{ + /// Returns the ID of this event. + fn id() -> Id + where + Self: Sized, + { + Id::new::<Self, ()>(None) + } +} pub mod start; @@ -13,15 +24,27 @@ pub mod start; pub struct Id { inner: TypeId, + extra: Option<u64>, } impl Id { - /// Returns the id of a [`Event`]; #[must_use] - pub fn of<EventT: Event>() -> Self + pub fn new<EventT, Extra>(extra: Option<Extra>) -> Self + where + EventT: Event, + Extra: Hash, { - Self { inner: TypeId::of::<EventT>() } + Self { + inner: TypeId::of::<EventT>(), + extra: extra.map(|extra| { + let mut hasher = DefaultHasher::new(); + + extra.hash(&mut hasher); + + hasher.finish() + }), + } } } @@ -59,7 +82,7 @@ macro_rules! impl_sequence { fn ids() -> Self::Ids { [#( - Id::of::<Event~I>(), + Event~I::id(), )*] } } |