diff options
Diffstat (limited to 'ecs')
-rw-r--r-- | ecs/examples/event_loop.rs | 32 | ||||
-rw-r--r-- | ecs/examples/multiple_queries.rs | 14 | ||||
-rw-r--r-- | ecs/examples/simple.rs | 14 | ||||
-rw-r--r-- | ecs/examples/with_local.rs | 18 | ||||
-rw-r--r-- | ecs/examples/with_single.rs | 26 | ||||
-rw-r--r-- | ecs/src/event.rs | 5 | ||||
-rw-r--r-- | ecs/src/lib.rs | 19 |
7 files changed, 42 insertions, 86 deletions
diff --git a/ecs/examples/event_loop.rs b/ecs/examples/event_loop.rs index b6a10e3..8d8d84f 100644 --- a/ecs/examples/event_loop.rs +++ b/ecs/examples/event_loop.rs @@ -1,5 +1,5 @@ use ecs::actions::Actions; -use ecs::event::{Event, Id as EventId}; +use ecs::event::Event; use ecs::{Component, Query, World}; #[derive(Component)] @@ -67,43 +67,25 @@ fn age(query: Query<(Health, Name)>, mut actions: Actions) #[derive(Debug)] struct EventA; -impl Event for EventA -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for EventA {} #[derive(Debug)] struct EventB; -impl Event for EventB -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for EventB {} #[derive(Debug)] struct EventC; -impl Event for EventC -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for EventC {} fn main() { let mut world = World::new(); - world.register_system(&EventA, sheer); - world.register_system(&EventB, feed); - world.register_system(&EventC, age); + world.register_system(EventA, sheer); + world.register_system(EventB, feed); + world.register_system(EventC, age); world.create_entity(( Wool { remaining: 30 }, diff --git a/ecs/examples/multiple_queries.rs b/ecs/examples/multiple_queries.rs index e914cc6..ab442be 100644 --- a/ecs/examples/multiple_queries.rs +++ b/ecs/examples/multiple_queries.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use ecs::event::{Event, Id as EventId}; +use ecs::event::Event; use ecs::{Component, Query, World}; #[derive(Component)] @@ -60,19 +60,13 @@ fn do_attacks( #[derive(Debug, PartialEq, Eq, Hash)] struct Start; -impl Event for Start -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for Start {} fn main() { let mut world = World::new(); - world.register_system(&Start, do_attacks); + world.register_system(Start, do_attacks); world.create_entity(( Health { health: 100 }, @@ -92,5 +86,5 @@ fn main() world.create_entity((AttackStrength::Strong,)); world.create_entity((AttackStrength::Weak,)); - world.emit(&Start); + world.emit(Start); } diff --git a/ecs/examples/simple.rs b/ecs/examples/simple.rs index 9983c4a..6949308 100644 --- a/ecs/examples/simple.rs +++ b/ecs/examples/simple.rs @@ -1,4 +1,4 @@ -use ecs::event::{Event, Id as EventId}; +use ecs::event::Event; use ecs::{Component, Query, World}; #[derive(Component)] @@ -23,19 +23,13 @@ fn say_hello(query: Query<(SomeData, Greeting)>) #[derive(Debug)] struct Start; -impl Event for Start -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for Start {} fn main() { let mut world = World::new(); - world.register_system(&Start, say_hello); + world.register_system(Start, say_hello); world.create_entity(( SomeData { num: 987_654 }, @@ -49,5 +43,5 @@ fn main() Greeting { greeting: "Good evening".to_string() }, )); - world.emit(&Start); + world.emit(Start); } diff --git a/ecs/examples/with_local.rs b/ecs/examples/with_local.rs index eb5de28..5890b90 100644 --- a/ecs/examples/with_local.rs +++ b/ecs/examples/with_local.rs @@ -1,5 +1,5 @@ use ecs::component::local::Local; -use ecs::event::{Event, Id as EventId}; +use ecs::event::Event; use ecs::system::{Into, System}; use ecs::{Component, Query, World}; @@ -45,27 +45,21 @@ fn say_whats_up(query: Query<(SomeData, Name)>, mut state: Local<SayHelloState>) #[derive(Debug)] struct Update; -impl Event for Update -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for Update {} fn main() { let mut world = World::new(); world.register_system( - &Update, + Update, say_hello .into_system() .initialize((SayHelloState { cnt: 0 },)), ); world.register_system( - &Update, + Update, say_whats_up .into_system() .initialize((SayHelloState { cnt: 0 },)), @@ -75,9 +69,9 @@ fn main() world.create_entity((SomeData { num: 345 },)); - world.emit(&Update); + world.emit(Update); println!("Haha"); - world.emit(&Update); + world.emit(Update); } diff --git a/ecs/examples/with_single.rs b/ecs/examples/with_single.rs index 25b73d1..d222f76 100644 --- a/ecs/examples/with_single.rs +++ b/ecs/examples/with_single.rs @@ -1,5 +1,5 @@ use ecs::component::single::Single; -use ecs::event::{Event, Id as EventId}; +use ecs::event::Event; use ecs::{Component, Query, World}; #[derive(Component)] @@ -33,31 +33,19 @@ fn print_total_ammo_count(ammo_counter: Single<AmmoCounter>) #[derive(Debug)] struct EventA; -impl Event for EventA -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for EventA {} #[derive(Debug)] struct EventB; -impl Event for EventB -{ - fn id(&self) -> EventId - { - EventId::of::<Self>() - } -} +impl Event for EventB {} fn main() { let mut world = World::new(); - world.register_system(&EventA, count_ammo); - world.register_system(&EventB, print_total_ammo_count); + world.register_system(EventA, count_ammo); + world.register_system(EventB, print_total_ammo_count); world.create_entity((Ammo { ammo_left: 4 },)); world.create_entity((Ammo { ammo_left: 7 },)); @@ -65,7 +53,7 @@ fn main() world.add_single_component(AmmoCounter::default()).unwrap(); - world.emit(&EventA); + world.emit(EventA); - world.emit(&EventB); + world.emit(EventB); } diff --git a/ecs/src/event.rs b/ecs/src/event.rs index 9545318..6a4c3a8 100644 --- a/ecs/src/event.rs +++ b/ecs/src/event.rs @@ -4,10 +4,7 @@ use std::hash::Hash; use seq_macro::seq; -pub trait Event: Debug + 'static -{ - fn id(&self) -> Id; -} +pub trait Event: Debug + 'static {} /// The ID of a [`Event`]. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index ce1a7d1..c93781d 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -103,19 +103,22 @@ impl World self.data.single_component_storage.insert(single_component) } - pub fn register_system<'this, SystemImpl>( + pub fn register_system<'this, EventT, SystemImpl>( &'this mut self, - event: &impl Event, + event: EventT, system: impl System<'this, SystemImpl>, - ) + ) where + EventT: Event, { self.systems.push(system.into_type_erased()); self.data .events - .entry(event.id()) + .entry(EventId::of::<EventT>()) .or_default() .push(self.systems.len() - 1); + + drop(event); } /// Emits a event, running all systems listening to the event for each compatible @@ -123,9 +126,13 @@ impl World /// /// # Panics /// Will panic if a system has dissapeared. - pub fn emit(&self, event: &impl Event) + pub fn emit<EventT>(&self, event: EventT) + where + EventT: Event, { - self.emit_event_by_id(event.id()); + self.emit_event_by_id(EventId::of::<EventT>()); + + drop(event); } pub fn query<Comps>(&self) -> Query<Comps> |