diff options
author | HampusM <hampus@hampusmat.com> | 2024-04-05 23:06:49 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-04-06 12:52:54 +0200 |
commit | 3e6d04be56e910f77048442a3c744298ef856ca1 (patch) | |
tree | fd8e576411bcd1e8cda32d7cb2aaa2c7f36ae673 /ecs/src/lib.rs | |
parent | 53baf6a6d35a904e1a38873271221af2e01f589c (diff) |
feat(ecs): add event loop function to world
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r-- | ecs/src/lib.rs | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 4d6e4e5..339a314 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -12,7 +12,7 @@ use std::vec::Drain; use crate::actions::Action; use crate::component::{Component, Sequence as ComponentSequence}; -use crate::event::{Event, Id as EventId}; +use crate::event::{Event, Id as EventId, Ids, Sequence as EventSequence}; use crate::lock::{Lock, ReadGuard}; use crate::system::{ NoInitParamFlag as NoInitSystemParamFlag, @@ -110,18 +110,7 @@ impl World /// Will panic if a system has dissapeared. pub fn emit(&self, event: &impl Event) { - let Some(system_indices) = self.data.events.get(&event.id()).cloned() else { - return; - }; - - for system_index in system_indices { - let system = self.systems.get(system_index).unwrap(); - - // SAFETY: The world data lives long enough - unsafe { - system.run(&self.data); - } - } + self.emit_event_by_id(event.id()); } pub fn query<Comps>(&self) -> Query<Comps> @@ -166,6 +155,35 @@ impl World } } } + + pub fn event_loop<EventSeq: EventSequence>(&self) + { + let event_seq = EventSeq::ids(); + + loop { + for event_id in event_seq.iter() { + self.emit_event_by_id(*event_id); + } + + self.perform_queued_actions(); + } + } + + fn emit_event_by_id(&self, event_id: EventId) + { + let Some(system_indices) = self.data.events.get(&event_id) else { + return; + }; + + for system_index in system_indices { + let system = self.systems.get(*system_index).unwrap(); + + // SAFETY: The world data lives long enough + unsafe { + system.run(&self.data); + } + } + } } #[derive(Debug, Default)] |