summaryrefslogtreecommitdiff
path: root/ecs/src/event.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-05 23:06:49 +0200
committerHampusM <hampus@hampusmat.com>2024-04-06 12:52:54 +0200
commit3e6d04be56e910f77048442a3c744298ef856ca1 (patch)
treefd8e576411bcd1e8cda32d7cb2aaa2c7f36ae673 /ecs/src/event.rs
parent53baf6a6d35a904e1a38873271221af2e01f589c (diff)
feat(ecs): add event loop function to world
Diffstat (limited to 'ecs/src/event.rs')
-rw-r--r--ecs/src/event.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/ecs/src/event.rs b/ecs/src/event.rs
index 0cf6da7..01d9d80 100644
--- a/ecs/src/event.rs
+++ b/ecs/src/event.rs
@@ -2,6 +2,8 @@ use std::any::TypeId;
use std::fmt::Debug;
use std::hash::Hash;
+use seq_macro::seq;
+
pub trait Event: Debug + 'static
{
fn id(&self) -> Id;
@@ -22,3 +24,49 @@ impl Id
Self { inner: TypeId::of::<EventT>() }
}
}
+
+pub trait Ids
+{
+ type Iter<'a>: Iterator<Item = &'a Id>
+ where
+ Self: 'a;
+
+ fn iter(&self) -> Self::Iter<'_>;
+}
+
+/// A sequence of events.
+pub trait Sequence
+{
+ type Ids: Ids;
+
+ fn ids() -> Self::Ids;
+}
+
+macro_rules! impl_sequence {
+ ($c: tt) => {
+ seq!(I in 0..=$c {
+ impl Ids for [Id; $c + 1]
+ {
+ type Iter<'a> = std::slice::Iter<'a, Id>;
+
+ fn iter(&self) -> Self::Iter<'_> {
+ self.into_iter()
+ }
+ }
+
+ impl<#(Event~I: Event,)*> Sequence for (#(Event~I,)*) {
+ type Ids = [Id; $c + 1];
+
+ fn ids() -> Self::Ids {
+ [#(
+ Id::of::<Event~I>(),
+ )*]
+ }
+ }
+ });
+ };
+}
+
+seq!(C in 0..=64 {
+ impl_sequence!(C);
+});