summaryrefslogtreecommitdiff
path: root/ecs/src
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/src')
-rw-r--r--ecs/src/event.rs24
-rw-r--r--ecs/src/lib.rs27
2 files changed, 38 insertions, 13 deletions
diff --git a/ecs/src/event.rs b/ecs/src/event.rs
new file mode 100644
index 0000000..0cf6da7
--- /dev/null
+++ b/ecs/src/event.rs
@@ -0,0 +1,24 @@
+use std::any::TypeId;
+use std::fmt::Debug;
+use std::hash::Hash;
+
+pub trait Event: Debug + 'static
+{
+ fn id(&self) -> Id;
+}
+
+/// The ID of a [`Event`].
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub struct Id
+{
+ inner: TypeId,
+}
+
+impl Id
+{
+ /// Returns the id of a [`Event`];
+ pub fn of<EventT: Event>() -> Self
+ {
+ Self { inner: TypeId::of::<EventT>() }
+ }
+}
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs
index a9ef2a4..e22913f 100644
--- a/ecs/src/lib.rs
+++ b/ecs/src/lib.rs
@@ -3,11 +3,11 @@
use std::any::{Any, TypeId};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
-use std::hash::Hash;
use std::marker::PhantomData;
use std::slice::{Iter as SliceIter, IterMut as SliceIterMut};
use crate::component::{Component, Sequence as ComponentSequence};
+use crate::event::{Event, Id as EventId};
use crate::system::{
NoInitParamFlag as NoInitSystemParamFlag,
Param as SystemParam,
@@ -17,6 +17,7 @@ use crate::system::{
use crate::tuple::FilterExclude as TupleFilterExclude;
pub mod component;
+pub mod event;
pub mod system;
pub mod tuple;
@@ -29,14 +30,14 @@ struct Entity
}
#[derive(Debug)]
-pub struct World<Event>
+pub struct World
{
systems: Vec<TypeErasedSystem>,
- events: HashMap<Event, Vec<usize>>,
+ events: HashMap<EventId, Vec<usize>>,
component_storage: ComponentStorage,
}
-impl<Event> World<Event>
+impl World
{
#[must_use]
pub fn new() -> Self
@@ -57,15 +58,17 @@ impl<Event> World<Event>
.push(Entity { components: components.into_vec() });
}
- pub fn register_system<TSystem, SystemImpl>(&mut self, event: Event, system: TSystem)
- where
- Event: Hash + PartialEq + Eq,
+ pub fn register_system<TSystem, SystemImpl>(
+ &mut self,
+ event: &impl Event,
+ system: TSystem,
+ ) where
TSystem: System<SystemImpl>,
{
self.systems.push(system.into_type_erased());
self.events
- .entry(event)
+ .entry(event.id())
.or_default()
.push(self.systems.len() - 1);
}
@@ -75,11 +78,9 @@ impl<Event> World<Event>
///
/// # Panics
/// Will panic if a system has dissapeared.
- pub fn emit(&mut self, event: &Event)
- where
- Event: Hash + PartialEq + Eq,
+ pub fn emit(&mut self, event: &impl Event)
{
- let Some(system_indices) = self.events.get(event).cloned() else {
+ let Some(system_indices) = self.events.get(&event.id()).cloned() else {
return;
};
@@ -98,7 +99,7 @@ impl<Event> World<Event>
}
}
-impl<Event> Default for World<Event>
+impl Default for World
{
fn default() -> Self
{