diff options
Diffstat (limited to 'ecs/src/actions.rs')
-rw-r--r-- | ecs/src/actions.rs | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index f7b00d3..89fd84a 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -6,7 +6,7 @@ use crate::component::{ Metadata as ComponentMetadata, Sequence as ComponentSequence, }; -use crate::system::{NoInitParamFlag, Param as SystemParam, System}; +use crate::system::{Param as SystemParam, System}; use crate::uid::{Kind as UidKind, Uid}; use crate::{ActionQueue, World}; @@ -20,36 +20,61 @@ 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<Comps: ComponentSequence>(&mut self, components: Comps) { - self.action_queue.push(Action::Spawn(components.into_vec())); + self.action_queue.push(Action::Spawn( + components.into_array().into(), + EventIds { ids: Comps::added_event_ids() }, + )); } - /// Adds component(s) to a entity. + /// Queues up despawning a entity at the end of the current tick. + pub fn despawn(&mut self, entity_uid: Uid) + { + debug_assert_eq!(entity_uid.kind(), UidKind::Entity); + + self.action_queue.push(Action::Despawn(entity_uid)); + } + + /// Queues up adding component(s) to a entity at the end of the current tick. pub fn add_components<Comps>(&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())); + if Comps::COUNT == 0 { + return; + } + + self.action_queue.push(Action::AddComponents( + entity_uid, + components.into_array().into(), + 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<Comps>(&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())); + if Comps::COUNT == 0 { + return; + } + + self.action_queue.push(Action::RemoveComponents( + entity_uid, + Comps::metadata().into_iter().collect(), + 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); @@ -75,9 +100,8 @@ impl<'world> Actions<'world> } } -unsafe impl<'world> SystemParam<'world> for Actions<'world> +impl<'world> SystemParam<'world> for Actions<'world> { - type Flags = NoInitParamFlag; type Input = (); fn initialize<SystemImpl>( @@ -135,12 +159,19 @@ impl<'weak_ref> Ref<'weak_ref> } } +#[derive(Debug)] +pub(crate) struct EventIds +{ + pub(crate) ids: Vec<Uid>, +} + /// A action for a [`System`] to perform. #[derive(Debug)] pub(crate) enum Action { - Spawn(Vec<Box<dyn Component>>), - AddComponents(Uid, Vec<Box<dyn Component>>), - RemoveComponents(Uid, Vec<ComponentMetadata>), + Spawn(Vec<Box<dyn Component>>, EventIds), + Despawn(Uid), + AddComponents(Uid, Vec<Box<dyn Component>>, EventIds), + RemoveComponents(Uid, Vec<ComponentMetadata>, EventIds), Stop, } |