From 3e6d04be56e910f77048442a3c744298ef856ca1 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 5 Apr 2024 23:06:49 +0200 Subject: feat(ecs): add event loop function to world --- ecs/src/lib.rs | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'ecs/src/lib.rs') 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(&self) -> Query @@ -166,6 +155,35 @@ impl World } } } + + pub fn event_loop(&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)] -- cgit v1.2.3-18-g5258