From dcc40c9205e5f4cf484523f97eb12a561d7b2b22 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 9 Dec 2024 14:05:33 +0100 Subject: refactor(ecs): use phases for system ordering --- ecs/src/actions.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'ecs/src/actions.rs') diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index f7b00d3..35e9569 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -20,36 +20,45 @@ pub struct Actions<'world> impl<'world> Actions<'world> { - /// Adds a spawning a new entity to the action queue. + /// Queues up a entity to spawn at the end of the current tick. pub fn spawn(&mut self, components: Comps) { - self.action_queue.push(Action::Spawn(components.into_vec())); + self.action_queue.push(Action::Spawn( + components.into_vec(), + EventIds { ids: Comps::added_event_ids() }, + )); } - /// Adds component(s) to a entity. + /// Queues up adding component(s) to a entity at the end of the current tick. pub fn add_components(&mut self, entity_uid: Uid, components: Comps) where Comps: ComponentSequence, { debug_assert_eq!(entity_uid.kind(), UidKind::Entity); - self.action_queue - .push(Action::AddComponents(entity_uid, components.into_vec())); + self.action_queue.push(Action::AddComponents( + entity_uid, + components.into_vec(), + EventIds { ids: Comps::added_event_ids() }, + )); } - /// Removes component(s) from a entity. + /// Queues up removing component(s) from a entity at the end of the current tick. pub fn remove_components(&mut self, entity_uid: Uid) where Comps: ComponentSequence, { debug_assert_eq!(entity_uid.kind(), UidKind::Entity); - self.action_queue - .push(Action::RemoveComponents(entity_uid, Comps::metadata())); + self.action_queue.push(Action::RemoveComponents( + entity_uid, + Comps::metadata(), + EventIds { ids: Comps::removed_event_ids() }, + )); } - /// Adds stopping the loop in [`Engine::event_loop`] at the next opportune time to the - /// action queue. + /// Stops the [`World`]. The world will finish the current tick and that tick will be + /// the last. pub fn stop(&mut self) { self.action_queue.push(Action::Stop); @@ -135,12 +144,18 @@ impl<'weak_ref> Ref<'weak_ref> } } +#[derive(Debug)] +pub(crate) struct EventIds +{ + pub(crate) ids: Vec, +} + /// A action for a [`System`] to perform. #[derive(Debug)] pub(crate) enum Action { - Spawn(Vec>), - AddComponents(Uid, Vec>), - RemoveComponents(Uid, Vec), + Spawn(Vec>, EventIds), + AddComponents(Uid, Vec>, EventIds), + RemoveComponents(Uid, Vec, EventIds), Stop, } -- cgit v1.2.3-18-g5258