diff options
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r-- | ecs/src/lib.rs | 169 |
1 files changed, 61 insertions, 108 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index dd43e0b..c953290 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -4,6 +4,7 @@ use std::any::{type_name, Any, TypeId}; use std::cell::RefCell; use std::fmt::Debug; use std::mem::ManuallyDrop; +use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -23,7 +24,7 @@ use crate::event::component::{ Removed as ComponentRemovedEvent, }; use crate::extension::{Collector as ExtensionCollector, Extension}; -use crate::lock::{Lock, WriteGuard}; +use crate::lock::Lock; use crate::pair::{ChildOf, DependsOn, Pair}; use crate::phase::{Phase, START as START_PHASE}; use crate::query::flexible::Query as FlexibleQuery; @@ -86,7 +87,7 @@ impl World world.add_sole(Stats::default()).ok(); for create_static_entity in CREATE_STATIC_ENTITIES { - create_static_entity(&world); + create_static_entity(&mut world); } world @@ -109,29 +110,23 @@ impl World #[tracing::instrument(skip_all)] #[doc(hidden)] - pub fn create_entity_with_uid<Comps>(&self, components: Comps, entity_uid: Uid) + pub fn create_entity_with_uid<Comps>(&mut self, components: Comps, entity_uid: Uid) where Comps: ComponentSequence, { debug_assert_eq!(entity_uid.kind(), UidKind::Entity); - let added_component_ids; - - { - let mut component_storage_lock = self.lock_component_storage_rw(); - - if let Err(err) = component_storage_lock.create_entity(entity_uid) { - tracing::warn!("Failed to create entity: {err}"); - return; - }; - - added_component_ids = Self::add_entity_components( - entity_uid, - components.into_parts_array(), - &mut component_storage_lock, - ); + if let Err(err) = self.data.component_storage.create_entity(entity_uid) { + tracing::warn!("Failed to create entity: {err}"); + return; } + let added_component_ids = Self::add_entity_components( + entity_uid, + components.into_parts_array(), + &mut self.data.component_storage, + ); + for comp_id in added_component_ids { self.emit_event_by_id::<ComponentAddedEvent>(comp_id); } @@ -203,7 +198,7 @@ impl World /// /// # Panics /// Will panic if a internal lock cannot be acquired. - pub fn step(&self) -> StepResult + pub fn step(&mut self) -> StepResult { if self.stop.load(Ordering::Relaxed) { return StepResult::Stop; @@ -219,8 +214,7 @@ impl World self.perform_phases(); - self.lock_component_storage_rw() - .create_imaginary_archetypes(); + self.data.component_storage.create_imaginary_archetypes(); self.perform_queued_actions(); @@ -249,7 +243,7 @@ impl World /// /// # Panics /// Will panic if a internal lock cannot be acquired. - pub fn start_loop(&self) + pub fn start_loop(&mut self) { while let StepResult::Continue = self.step() {} } @@ -267,13 +261,7 @@ impl World VizoxideArchetypeGraphParams, }; - let component_storage_lock = self - .data - .component_storage - .read_nonblock() - .expect("Failed to acquire read-only component storage lock"); - - component_storage_lock.create_vizoxide_archetype_graph( + self.data.component_storage.create_vizoxide_archetype_graph( name, VizoxideArchetypeGraphParams { create_node_name: |archetype, _| { @@ -339,7 +327,7 @@ impl World .build(), ); - for child_phase_entity in phase_query.iter() { + for child_phase_entity in &phase_query { self.query_and_run_systems(child_phase_entity.uid()); self.perform_child_phases(child_phase_entity.uid()); } @@ -360,7 +348,7 @@ impl World } #[tracing::instrument(skip_all)] - fn perform_queued_actions(&self) + fn perform_queued_actions(&mut self) { let mut active_action_queue = match *self.data.action_queue.active_queue.borrow() { @@ -380,27 +368,21 @@ impl World for action in active_action_queue.drain(..) { match action { Action::Spawn(components) => { - let added_component_ids; + let new_entity_uid = Uid::new_unique(UidKind::Entity); + if let Err(err) = + self.data.component_storage.create_entity(new_entity_uid) { - let mut component_storage_lock = self.lock_component_storage_rw(); - - let new_entity_uid = Uid::new_unique(UidKind::Entity); - - if let Err(err) = - component_storage_lock.create_entity(new_entity_uid) - { - tracing::warn!("Failed to create entity: {err}"); - continue; - }; - - added_component_ids = Self::add_entity_components( - new_entity_uid, - components, - &mut component_storage_lock, - ); + tracing::warn!("Failed to create entity: {err}"); + continue; } + let added_component_ids = Self::add_entity_components( + new_entity_uid, + components, + &mut self.data.component_storage, + ); + if !has_swapped_active_queue { self.swap_event_queue(&mut has_swapped_active_queue); } @@ -410,20 +392,31 @@ impl World } } Action::Despawn(entity_uid) => { - self.despawn_entity(entity_uid, &mut has_swapped_active_queue); - } - Action::AddComponents(entity_uid, components) => { - let added_component_ids; + let removed_entity = + match self.data.component_storage.remove_entity(entity_uid) { + Ok(components) => components, + Err(err) => { + tracing::error!("Failed to despawn entity: {err}"); + return; + } + }; - { - let mut component_storage_lock = self.lock_component_storage_rw(); + if !has_swapped_active_queue { + self.swap_event_queue(&mut has_swapped_active_queue); + } - added_component_ids = Self::add_entity_components( - entity_uid, - components, - &mut component_storage_lock, + for removed_ent_comp in removed_entity.components() { + self.emit_event_by_id::<ComponentRemovedEvent>( + removed_ent_comp.id(), ); } + } + Action::AddComponents(entity_uid, components) => { + let added_component_ids = Self::add_entity_components( + entity_uid, + components, + &mut self.data.component_storage, + ); if !has_swapped_active_queue { self.swap_event_queue(&mut has_swapped_active_queue); @@ -434,17 +427,11 @@ impl World } } Action::RemoveComponents(entity_uid, component_ids) => { - let removed_component_ids; - - { - let mut component_storage_lock = self.lock_component_storage_rw(); - - removed_component_ids = Self::remove_entity_components( - entity_uid, - component_ids, - &mut component_storage_lock, - ); - } + let removed_component_ids = Self::remove_entity_components( + entity_uid, + component_ids, + &mut self.data.component_storage, + ); if !has_swapped_active_queue { self.swap_event_queue(&mut has_swapped_active_queue); @@ -461,30 +448,6 @@ impl World } } - #[tracing::instrument(skip_all)] - fn despawn_entity(&self, entity_uid: Uid, has_swapped_active_queue: &mut bool) - { - let mut component_storage_lock = self.lock_component_storage_rw(); - - let removed_entity = match component_storage_lock.remove_entity(entity_uid) { - Ok(components) => components, - Err(err) => { - tracing::error!("Failed to despawn entity: {err}"); - return; - } - }; - - drop(component_storage_lock); - - if !*has_swapped_active_queue { - self.swap_event_queue(has_swapped_active_queue); - } - - for removed_ent_comp in removed_entity.components() { - self.emit_event_by_id::<ComponentRemovedEvent>(removed_ent_comp.id()); - } - } - fn add_entity_components( entity_uid: Uid, components: impl IntoIterator<Item = ComponentParts>, @@ -568,14 +531,6 @@ impl World *has_swapped_active_queue = true; } - - fn lock_component_storage_rw(&self) -> WriteGuard<'_, ComponentStorage> - { - self.data - .component_storage - .write_nonblock() - .expect("Failed to acquire read-write component storage lock") - } } impl Default for World @@ -599,9 +554,9 @@ pub enum StepResult #[derive(Debug)] pub struct WorldData { - component_storage: Arc<Lock<ComponentStorage>>, + component_storage: ComponentStorage, sole_storage: SoleStorage, - action_queue: Arc<ActionQueue>, + action_queue: Rc<ActionQueue>, } impl Default for WorldData @@ -609,12 +564,9 @@ impl Default for WorldData fn default() -> Self { Self { - component_storage: Arc::new(Lock::new( - ComponentStorage::default(), - type_name::<ComponentStorage>(), - )), + component_storage: ComponentStorage::default(), sole_storage: SoleStorage::default(), - action_queue: Arc::new(ActionQueue::default()), + action_queue: Rc::new(ActionQueue::default()), } } } @@ -633,6 +585,7 @@ impl<'a> EntityComponentRef<'a> self.component.component() } + #[must_use] pub fn id(&self) -> Uid { self.component_id |