From 6f8aeb236725f673f199bce7a6f3942eb56a8318 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 2 Mar 2024 12:00:01 +0100 Subject: feat(ecs): add event trait --- ecs/examples/multiple_queries.rs | 16 +++++++++++----- ecs/examples/simple.rs | 18 ++++++++++++------ ecs/examples/with_local.rs | 22 ++++++++++++++-------- ecs/src/event.rs | 24 ++++++++++++++++++++++++ ecs/src/lib.rs | 27 ++++++++++++++------------- 5 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 ecs/src/event.rs diff --git a/ecs/examples/multiple_queries.rs b/ecs/examples/multiple_queries.rs index 9a699c2..ed02a69 100644 --- a/ecs/examples/multiple_queries.rs +++ b/ecs/examples/multiple_queries.rs @@ -1,5 +1,6 @@ use std::fmt::Display; +use ecs::event::{Event, Id as EventId}; use ecs::{Component, Query, World}; #[derive(Component)] @@ -57,16 +58,21 @@ fn do_attacks( } #[derive(Debug, PartialEq, Eq, Hash)] -enum Event +struct Start; + +impl Event for Start { - Start, + fn id(&self) -> EventId + { + EventId::of::() + } } fn main() { - let mut world = World::::new(); + let mut world = World::new(); - world.register_system(Event::Start, do_attacks); + world.register_system(&Start, do_attacks); world.create_entity(( Health { health: 100 }, @@ -86,5 +92,5 @@ fn main() world.create_entity((AttackStrength::Strong,)); world.create_entity((AttackStrength::Weak,)); - world.emit(&Event::Start); + world.emit(&Start); } diff --git a/ecs/examples/simple.rs b/ecs/examples/simple.rs index 76d9538..9983c4a 100644 --- a/ecs/examples/simple.rs +++ b/ecs/examples/simple.rs @@ -1,3 +1,4 @@ +use ecs::event::{Event, Id as EventId}; use ecs::{Component, Query, World}; #[derive(Component)] @@ -19,17 +20,22 @@ fn say_hello(query: Query<(SomeData, Greeting)>) } } -#[derive(Debug, PartialEq, Eq, Hash)] -enum Event +#[derive(Debug)] +struct Start; + +impl Event for Start { - Start, + fn id(&self) -> EventId + { + EventId::of::() + } } fn main() { - let mut world = World::::new(); + let mut world = World::new(); - world.register_system(Event::Start, say_hello); + world.register_system(&Start, say_hello); world.create_entity(( SomeData { num: 987_654 }, @@ -43,5 +49,5 @@ fn main() Greeting { greeting: "Good evening".to_string() }, )); - world.emit(&Event::Start); + world.emit(&Start); } diff --git a/ecs/examples/with_local.rs b/ecs/examples/with_local.rs index b37a5c3..0342930 100644 --- a/ecs/examples/with_local.rs +++ b/ecs/examples/with_local.rs @@ -1,4 +1,5 @@ use ecs::component::Local; +use ecs::event::{Event, Id as EventId}; use ecs::system::{Into, System}; use ecs::{Component, Query, World}; @@ -41,25 +42,30 @@ fn say_whats_up(query: Query<(SomeData, Name)>, mut state: Local) } } -#[derive(Debug, PartialEq, Eq, Hash)] -enum Event +#[derive(Debug)] +struct Update; + +impl Event for Update { - Update, + fn id(&self) -> EventId + { + EventId::of::() + } } fn main() { - let mut world = World::::new(); + let mut world = World::new(); world.register_system( - Event::Update, + &Update, say_hello .into_system() .initialize((SayHelloState { cnt: 0 },)), ); world.register_system( - Event::Update, + &Update, say_whats_up .into_system() .initialize((SayHelloState { cnt: 0 },)), @@ -69,9 +75,9 @@ fn main() world.create_entity((SomeData { num: 345 },)); - world.emit(&Event::Update); + world.emit(&Update); println!("Haha"); - world.emit(&Event::Update); + world.emit(&Update); } 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() -> Self + { + Self { inner: TypeId::of::() } + } +} 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 +pub struct World { systems: Vec, - events: HashMap>, + events: HashMap>, component_storage: ComponentStorage, } -impl World +impl World { #[must_use] pub fn new() -> Self @@ -57,15 +58,17 @@ impl World .push(Entity { components: components.into_vec() }); } - pub fn register_system(&mut self, event: Event, system: TSystem) - where - Event: Hash + PartialEq + Eq, + pub fn register_system( + &mut self, + event: &impl Event, + system: TSystem, + ) where TSystem: System, { 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 World /// /// # 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 World } } -impl Default for World +impl Default for World { fn default() -> Self { -- cgit v1.2.3-18-g5258