From ca51244e9d462c661d29dc60ce5bf6f9056c569b Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 10 Apr 2024 20:37:53 +0200 Subject: chore(ecs): make shared singletons not components --- ecs/src/lib.rs | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index c93781d..e568fb8 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -13,6 +13,7 @@ use crate::actions::Action; use crate::component::{Component, Sequence as ComponentSequence}; use crate::event::{Event, Id as EventId, Ids, Sequence as EventSequence}; use crate::lock::Lock; +use crate::sole::Sole; use crate::system::{System, TypeErased as TypeErasedSystem}; use crate::type_name::TypeName; @@ -21,6 +22,7 @@ pub mod component; pub mod event; pub mod lock; pub mod query; +pub mod sole; pub mod system; pub mod tuple; pub mod type_name; @@ -89,18 +91,15 @@ impl World }); } - /// Adds a single component. This component will be globally shared. + /// Adds a globally shared singleton value. /// /// # Errors - /// Returns `Err` if this component has already been added as a single component. - pub fn add_single_component( - &mut self, - single_component: SingleComponent, - ) -> Result<(), SingleComponentAlreadyExistsError> + /// Returns `Err` if this [`Sole`] has already been added. + pub fn add_sole(&mut self, sole: SoleT) -> Result<(), SoleAlreadyExistsError> where - SingleComponent: Component, + SoleT: Sole, { - self.data.single_component_storage.insert(single_component) + self.data.sole_storage.insert(sole) } pub fn register_system<'this, EventT, SystemImpl>( @@ -227,7 +226,7 @@ pub struct WorldData { events: HashMap>, component_storage: Arc>, - single_component_storage: SingleComponentStorage, + sole_storage: SoleStorage, action_queue: Arc>, } @@ -348,39 +347,31 @@ impl Drop for ComponentStorage } #[derive(Debug, thiserror::Error)] -#[error("Single component {0} already exists")] -pub struct SingleComponentAlreadyExistsError(pub &'static str); +#[error("Sole {0} already exists")] +pub struct SoleAlreadyExistsError(pub &'static str); #[derive(Debug, Default)] -struct SingleComponentStorage +struct SoleStorage { - storage: HashMap>>, + storage: HashMap>>, } -impl SingleComponentStorage +impl SoleStorage { - fn get(&self) -> Option<&Lock>> + fn get(&self) -> Option<&Lock>> { - self.storage.get(&TypeId::of::()) + self.storage.get(&TypeId::of::()) } - fn insert( - &mut self, - single_component: SingleComponent, - ) -> Result<(), SingleComponentAlreadyExistsError> + fn insert(&mut self, sole: SoleT) -> Result<(), SoleAlreadyExistsError> { - let single_component_type_id = TypeId::of::(); + let sole_type_id = TypeId::of::(); - if self.storage.contains_key(&single_component_type_id) { - return Err(SingleComponentAlreadyExistsError(type_name::< - SingleComponent, - >())); + if self.storage.contains_key(&sole_type_id) { + return Err(SoleAlreadyExistsError(type_name::())); } - self.storage.insert( - single_component_type_id, - Lock::new(Box::new(single_component)), - ); + self.storage.insert(sole_type_id, Lock::new(Box::new(sole))); Ok(()) } -- cgit v1.2.3-18-g5258