diff options
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r-- | ecs/src/lib.rs | 167 |
1 files changed, 80 insertions, 87 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 3caaa6b..962c542 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -1,6 +1,6 @@ #![deny(clippy::all, clippy::pedantic)] -use std::any::{type_name, TypeId}; +use std::any::{type_name, Any, TypeId}; use std::cell::RefCell; use std::fmt::Debug; use std::mem::ManuallyDrop; @@ -12,9 +12,12 @@ use hashbrown::HashMap; use crate::actions::Action; use crate::component::storage::archetype::EntityComponent as ArchetypeEntityComponent; use crate::component::storage::Storage as ComponentStorage; -use crate::component::{Component, Sequence as ComponentSequence}; +use crate::component::{ + Component, + Parts as ComponentParts, + Sequence as ComponentSequence, +}; use crate::entity::CREATE_STATIC_ENTITIES; -use crate::event::component::Kind as ComponentEventKind; use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::{Lock, WriteGuard}; use crate::phase::{Phase, START as START_PHASE}; @@ -31,7 +34,6 @@ use crate::relationship::{ChildOf, DependsOn, Relationship}; use crate::sole::Sole; use crate::stats::Stats; use crate::system::{System, SystemComponent}; -use crate::type_name::TypeName; use crate::uid::{Kind as UidKind, Uid}; pub mod actions; @@ -39,7 +41,7 @@ pub mod component; pub mod entity; pub mod event; pub mod extension; -pub mod lock; +mod lock; pub mod phase; pub mod query; pub mod relationship; @@ -47,7 +49,6 @@ pub mod sole; pub mod stats; pub mod system; pub mod tuple; -pub mod type_name; pub mod uid; pub mod util; @@ -119,14 +120,16 @@ impl World Self::add_entity_components( entity_uid, - components.into_array(), + components.into_parts_array(), &mut component_storage_lock, ); } - for added_event_id in Comps::added_event_ids() { - self.emit_event_by_id(added_event_id); - } + // TODO: Emit component added events here + + // for added_event_id in Comps::added_event_ids() { + // self.emit_event_by_id(added_event_id); + // } } /// Adds a globally shared singleton value. @@ -184,10 +187,10 @@ impl World Query::new(self) } - pub fn flexible_query<'terms>( + pub fn flexible_query<const MAX_TERM_CNT: usize>( &self, - terms: QueryTerms<'terms>, - ) -> FlexibleQuery<'_, 'terms> + terms: QueryTerms<MAX_TERM_CNT>, + ) -> FlexibleQuery<'_, MAX_TERM_CNT> { FlexibleQuery::new(self, terms) } @@ -371,7 +374,7 @@ impl World for action in active_action_queue.drain(..) { match action { - Action::Spawn(components, component_added_event_ids) => { + Action::Spawn(components) => { { let mut component_storage_lock = self.lock_component_storage_rw(); @@ -395,18 +398,16 @@ impl World self.swap_event_queue(&mut has_swapped_active_queue); } - for comp_added_event_id in component_added_event_ids.ids { - self.emit_event_by_id(comp_added_event_id); - } + // TODO: Emit component added events here + + // for comp_added_event_id in component_added_event_ids.ids { + // self.emit_event_by_id(comp_added_event_id); + // } } Action::Despawn(entity_uid) => { self.despawn_entity(entity_uid, &mut has_swapped_active_queue); } - Action::AddComponents( - entity_uid, - components, - component_added_event_ids, - ) => { + Action::AddComponents(entity_uid, components) => { { let mut component_storage_lock = self.lock_component_storage_rw(); @@ -421,26 +422,22 @@ impl World self.swap_event_queue(&mut has_swapped_active_queue); } + // TODO: Emit component added events here + // TODO: Fix that events are emitted for components that haven't been // added because a error occurred (for example, the entity already has // the component) - for comp_added_event_id in component_added_event_ids.ids { - self.emit_event_by_id(comp_added_event_id); - } + // for comp_added_event_id in component_added_event_ids.ids { + // self.emit_event_by_id(comp_added_event_id); + // } } - Action::RemoveComponents( - entity_uid, - components_metadata, - component_removed_event_ids, - ) => { + Action::RemoveComponents(entity_uid, component_ids) => { { let mut component_storage_lock = self.lock_component_storage_rw(); Self::remove_entity_components( entity_uid, - components_metadata - .iter() - .map(|comp_metadata| comp_metadata.id), + component_ids, &mut component_storage_lock, ); } @@ -449,12 +446,14 @@ impl World self.swap_event_queue(&mut has_swapped_active_queue); } + // TODO: Emit component removed events here + // TODO: Fix that events are emitted for components that haven't been // removed because a error occurred (for example, the entity does not // have the component) - for comp_removed_event_id in component_removed_event_ids.ids { - self.emit_event_by_id(comp_removed_event_id); - } + // for comp_removed_event_id in component_removed_event_ids.ids { + // self.emit_event_by_id(comp_removed_event_id); + // } } Action::Stop => { self.stop.store(true, Ordering::Relaxed); @@ -468,7 +467,7 @@ impl World { let mut component_storage_lock = self.lock_component_storage_rw(); - let removed_entity = match component_storage_lock.remove_entity(entity_uid) { + let _removed_entity = match component_storage_lock.remove_entity(entity_uid) { Ok(components) => components, Err(err) => { tracing::error!("Failed to despawn entity: {err}"); @@ -476,44 +475,34 @@ impl World } }; - let component_removed_event_uids = removed_entity - .components() - .iter() - .map(|component| { - component - .component() - .read_nonblock() - .unwrap_or_else(|_| { - panic!( - "Failed to acquire read-only {} component lock", - component.name() - ) - }) - .get_event_uid(ComponentEventKind::Removed) - }) - .collect::<Vec<_>>(); - drop(component_storage_lock); if !*has_swapped_active_queue { self.swap_event_queue(has_swapped_active_queue); } - for comp_removed_event_id in component_removed_event_uids { - self.emit_event_by_id(comp_removed_event_id); - } + // TODO: Emit component removed events here + + // for comp_removed_event_id in component_removed_event_uids { + // self.emit_event_by_id(comp_removed_event_id); + // } } fn add_entity_components( entity_uid: Uid, - components: impl IntoIterator<Item = (Uid, Box<dyn Component>)>, + components: impl IntoIterator<Item = ComponentParts>, component_storage: &mut ComponentStorage, ) { - for (component_id, component) in components { - if let Err(err) = component_storage - .add_entity_component(entity_uid, (component_id, component)) - { + for component_parts in components { + if let Err(err) = component_storage.add_entity_component( + entity_uid, + ( + component_parts.id(), + component_parts.name(), + component_parts.into_data(), + ), + ) { tracing::error!("Failed to add component to entity: {err}"); } } @@ -534,13 +523,12 @@ impl World } } + #[allow(dead_code)] fn emit_event_by_id(&self, event_id: Uid) { - let mut query_required_ids = [SystemComponent::id(), event_id]; - let query = self.flexible_query( - QueryTerms::builder() - .with_required_ids(&mut query_required_ids) + QueryTerms::<2>::builder() + .with_required([SystemComponent::id(), event_id]) .build(), ); @@ -590,7 +578,7 @@ pub enum StepResult Stop, } -#[derive(Debug, Default)] +#[derive(Debug)] pub struct WorldData { component_storage: Arc<Lock<ComponentStorage>>, @@ -598,6 +586,21 @@ pub struct WorldData action_queue: Arc<ActionQueue>, } +impl Default for WorldData +{ + fn default() -> Self + { + Self { + component_storage: Arc::new(Lock::new( + ComponentStorage::default(), + type_name::<ComponentStorage>(), + )), + sole_storage: SoleStorage::default(), + action_queue: Arc::new(ActionQueue::default()), + } + } +} + #[derive(Debug)] pub struct EntityComponentRef<'a> { @@ -606,7 +609,7 @@ pub struct EntityComponentRef<'a> impl<'a> EntityComponentRef<'a> { - pub fn component(&self) -> &'a Lock<Box<dyn Component>> + fn component(&self) -> &'a Lock<Box<dyn Any>> { self.comp.component() } @@ -625,7 +628,7 @@ enum ActiveActionQueue B, } -#[derive(Debug, Default)] +#[derive(Debug)] struct ActionQueue { queue_a: Lock<Vec<Action>>, @@ -652,11 +655,15 @@ impl ActionQueue } } -impl TypeName for ActionQueue +impl Default for ActionQueue { - fn type_name(&self) -> &'static str + fn default() -> Self { - type_name::<Self>() + Self { + queue_a: Lock::new(Vec::new(), type_name::<Vec<Action>>()), + queue_b: Lock::new(Vec::new(), type_name::<Vec<Action>>()), + active_queue: RefCell::new(ActiveActionQueue::default()), + } } } @@ -701,7 +708,7 @@ impl SoleStorage self.storage.insert( sole_type_id, ManuallyDrop::new(StoredSole { - sole: Arc::new(Lock::new(Box::new(sole))), + sole: Arc::new(Lock::new(Box::new(sole), type_name::<SoleT>())), drop_last, }), ); @@ -718,18 +725,9 @@ impl Drop for SoleStorage for sole in self.storage.values_mut() { if sole.drop_last { - tracing::trace!( - "Sole {} pushed to dropping last queue", - sole.sole.read_nonblock().unwrap().type_name() - ); - soles_to_drop_last.push(sole); continue; } - tracing::trace!( - "Dropping sole {}", - sole.sole.read_nonblock().unwrap().type_name() - ); unsafe { ManuallyDrop::drop(sole); @@ -737,11 +735,6 @@ impl Drop for SoleStorage } for sole in &mut soles_to_drop_last { - tracing::trace!( - "Dropping sole {} last", - sole.sole.read_nonblock().unwrap().type_name() - ); - unsafe { ManuallyDrop::drop(sole); } |