diff options
| author | HampusM <hampus@hampusmat.com> | 2026-06-20 16:01:00 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-06-23 00:25:26 +0200 |
| commit | 7b3374ad9585f78c60e1b158126ab54384a83f32 (patch) | |
| tree | ee14cd0bebd6554fab43685da89c459310f0e97d /engine-ecs/src/lib.rs | |
| parent | d6cf708a4fd1caf0f2e193ceb7c23fa8e77cc1db (diff) | |
refactor(engine-ecs): store soles as components
Diffstat (limited to 'engine-ecs/src/lib.rs')
| -rw-r--r-- | engine-ecs/src/lib.rs | 87 |
1 files changed, 8 insertions, 79 deletions
diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index 1c157e3..25866ca 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -1,14 +1,10 @@ #![deny(clippy::all, clippy::pedantic)] -use std::any::{type_name, Any, TypeId}; +use std::any::Any; use std::fmt::Debug; use std::hint::cold_path; -use std::mem::ManuallyDrop; use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; - -use hashbrown::HashMap; use crate::actions::Action; use crate::component::storage::archetype::EntityComponent as ArchetypeEntityComponent; @@ -156,7 +152,7 @@ impl World entity_decl.create(self); } - /// Adds a globally shared singleton value. + /// Adds a singleton. /// /// # Errors /// Returns `Err` if this [`Sole`] has already been added. @@ -164,7 +160,9 @@ impl World where SoleT: Sole, { - self.data.sole_storage.insert(sole) + self.create_ent(SoleT::id(), [sole.into_parts()]); + + Ok(()) } pub fn register_observer<'this, SystemImpl, ObserverT>( @@ -253,7 +251,9 @@ impl World pub fn get_sole<SoleT: Sole>(&self) -> Option<Single<'_, SoleT>> { - Some(Single::new(self.data.sole_storage.get::<SoleT>()?)) + let ent = self.get_entity(SoleT::id())?; + + Some(Single::new(ent.get_with_id_mut(SoleT::id())?)) } pub fn event_submitter(&self) -> EventSubmitter<'_> @@ -701,7 +701,6 @@ pub enum StepResult struct WorldData { component_storage: ComponentStorage, - sole_storage: SoleStorage, action_queue: Rc<ActionQueue>, new_events: Lock<NewEvents>, } @@ -765,73 +764,3 @@ impl ActionQueue #[error("Sole {0} already exists")] pub struct SoleAlreadyExistsError(pub &'static str); -#[derive(Debug)] -struct StoredSole -{ - sole: Arc<Lock<Box<dyn Sole>>>, - drop_last: bool, -} - -#[derive(Debug, Default)] -struct SoleStorage -{ - storage: HashMap<TypeId, ManuallyDrop<StoredSole>>, -} - -impl SoleStorage -{ - fn get<SoleT: Sole>(&self) -> Option<&Arc<Lock<Box<dyn Sole>>>> - { - self.storage - .get(&TypeId::of::<SoleT>()) - .map(|sole| &sole.sole) - } - - fn insert<SoleT: Sole>(&mut self, sole: SoleT) -> Result<(), SoleAlreadyExistsError> - { - let sole_type_id = TypeId::of::<SoleT>(); - - if self.storage.contains_key(&sole_type_id) { - return Err(SoleAlreadyExistsError(type_name::<SoleT>())); - } - - let drop_last = sole.drop_last(); - - // TODO: Reconsider this maybe? - #[allow(clippy::arc_with_non_send_sync)] - self.storage.insert( - sole_type_id, - ManuallyDrop::new(StoredSole { - sole: Arc::new(Lock::new(Box::new(sole), type_name::<SoleT>())), - drop_last, - }), - ); - - Ok(()) - } -} - -impl Drop for SoleStorage -{ - fn drop(&mut self) - { - let mut soles_to_drop_last = Vec::new(); - - for sole in self.storage.values_mut() { - if sole.drop_last { - soles_to_drop_last.push(sole); - continue; - } - - unsafe { - ManuallyDrop::drop(sole); - } - } - - for sole in &mut soles_to_drop_last { - unsafe { - ManuallyDrop::drop(sole); - } - } - } -} |
