diff options
author | HampusM <hampus@hampusmat.com> | 2024-11-11 00:11:22 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-11-11 00:11:22 +0100 |
commit | daf0bc236df25c0e9f44bc3e30839c16cda3f638 (patch) | |
tree | 7475e4e58686dd34366e641ec32f5a9374d66533 /ecs/src/component/storage.rs | |
parent | 17f63d9859e1c82a30c07bf110cf2b9872e2427e (diff) |
refactor(ecs): use same ID for entities & components
Diffstat (limited to 'ecs/src/component/storage.rs')
-rw-r--r-- | ecs/src/component/storage.rs | 72 |
1 files changed, 34 insertions, 38 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 041c124..141fea7 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -8,12 +8,11 @@ use std::vec::IntoIter as OwnedVecIter; use crate::archetype::Id as ArchetypeId; use crate::component::{ Component, - Id as ComponentId, IsOptional as ComponentIsOptional, Metadata as ComponentMetadata, }; -use crate::entity::Uid as EntityUid; use crate::type_name::TypeName; +use crate::uid::Uid; use crate::util::Sortable; use crate::EntityComponent; @@ -22,7 +21,7 @@ pub struct Storage { archetypes: Vec<Archetype>, archetype_lookup: RefCell<HashMap<ArchetypeId, ArchetypeLookupEntry>>, - entity_archetype_lookup: HashMap<EntityUid, ArchetypeId>, + entity_archetype_lookup: HashMap<Uid, ArchetypeId>, } impl Storage @@ -72,7 +71,7 @@ impl Storage self.iter_archetypes_by_lookup(archetype_id) } - pub fn get_entity_archetype(&self, entity_uid: EntityUid) -> Option<&Archetype> + pub fn get_entity_archetype(&self, entity_uid: Uid) -> Option<&Archetype> { let archetype_id = self.entity_archetype_lookup.get(&entity_uid)?; @@ -85,15 +84,15 @@ impl Storage #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] pub fn push_entity( &mut self, - entity_uid: EntityUid, + entity_uid: Uid, mut components: Vec<Box<dyn Component>>, - ) -> Result<(ArchetypeId, EntityUid), Error> + ) -> Result<(ArchetypeId, Uid), Error> { if self.entity_archetype_lookup.contains_key(&entity_uid) { return Err(Error::EntityAlreadyExists(entity_uid)); } - components.sort_by_key(|component| component.id()); + components.sort_by_key(|component| component.self_id()); #[cfg(feature = "debug")] tracing::debug!( @@ -141,7 +140,7 @@ impl Storage pub fn add_components_to_entity( &mut self, - entity_uid: EntityUid, + entity_uid: Uid, components: Vec<Box<dyn Component>>, ) -> Option<()> { @@ -154,7 +153,7 @@ impl Storage let contains_component_already = components .iter() - .any(|component| archetype.component_ids.contains_key(&component.id())); + .any(|component| archetype.component_ids.contains_key(&component.self_id())); if contains_component_already { let component_cnt = components.len(); @@ -193,8 +192,8 @@ impl Storage pub fn remove_components_from_entity( &mut self, - entity_uid: EntityUid, - component_ids: impl IntoIterator<Item = ComponentId>, + entity_uid: Uid, + component_ids: impl IntoIterator<Item = Uid>, ) -> Option<()> { let archetype_id = self.entity_archetype_lookup.get(&entity_uid)?; @@ -216,7 +215,7 @@ impl Storage .components .into_iter() .map(|component| component.component.into_inner()) - .filter(|component| !component_ids_set.contains(&component.id())) + .filter(|component| !component_ids_set.contains(&component.self_id())) .collect(), ) .expect("Not supposed to return Err since the entity is removed"); @@ -226,7 +225,7 @@ impl Storage fn populate_matching_archetype_lookup_entries( &mut self, - comp_ids_set: &HashSet<ComponentId>, + comp_ids_set: &HashSet<Uid>, archetype_index: usize, ) { @@ -251,7 +250,7 @@ impl Storage fn get_or_create_archetype( &mut self, archetype_id: ArchetypeId, - comp_ids_set: &HashSet<ComponentId>, + comp_ids_set: &HashSet<Uid>, components: &[Box<dyn Component>], ) -> usize { @@ -266,7 +265,7 @@ impl Storage if lookup_entry.archetype_indices.is_empty() { self.archetypes.push(Archetype::new( - components.iter().map(|component| component.id()), + components.iter().map(|component| component.self_id()), )); lookup_entry @@ -282,7 +281,7 @@ impl Storage fn find_archetype_index_with_entity( &self, archetype_id: ArchetypeId, - entity_uid: EntityUid, + entity_uid: Uid, ) -> Option<usize> { let archetype_lookup = self.archetype_lookup.borrow_mut(); @@ -330,7 +329,7 @@ impl Storage pub enum Error { #[error("Entity already exists")] - EntityAlreadyExists(EntityUid), + EntityAlreadyExists(Uid), } impl TypeName for Storage @@ -344,21 +343,21 @@ impl TypeName for Storage #[derive(Debug)] struct ArchetypeLookupEntry { - component_ids: HashSet<ComponentId>, + component_ids: HashSet<Uid>, archetype_indices: Vec<usize>, } #[derive(Debug)] pub struct Archetype { - component_ids: HashMap<ComponentId, usize>, - entity_lookup: HashMap<EntityUid, usize>, + component_ids: HashMap<Uid, usize>, + entity_lookup: HashMap<Uid, usize>, entities: Vec<ArchetypeEntity>, } impl Archetype { - fn new(component_ids: impl IntoIterator<Item = ComponentId>) -> Self + fn new(component_ids: impl IntoIterator<Item = Uid>) -> Self { Self { component_ids: component_ids @@ -371,10 +370,7 @@ impl Archetype } } - pub fn component_ids_is_superset( - &self, - other_component_ids: &HashSet<ComponentId>, - ) -> bool + pub fn component_ids_is_superset(&self, other_component_ids: &HashSet<Uid>) -> bool { if other_component_ids.len() <= self.component_ids.len() { other_component_ids @@ -385,7 +381,7 @@ impl Archetype } } - pub fn get_entity(&self, entity_uid: EntityUid) -> Option<&ArchetypeEntity> + pub fn get_entity(&self, entity_uid: Uid) -> Option<&ArchetypeEntity> { let entity_index = *self.entity_lookup.get(&entity_uid)?; @@ -397,14 +393,14 @@ impl Archetype EntityIter { iter: self.entities.iter() } } - pub fn get_index_for_component(&self, component_id: &ComponentId) -> Option<usize> + pub fn get_index_for_component(&self, component_id: &Uid) -> Option<usize> { self.component_ids.get(component_id).copied() } fn push_entity( &mut self, - entity_uid: EntityUid, + entity_uid: Uid, components: impl IntoIterator<Item = Box<dyn Component>>, ) { @@ -414,7 +410,7 @@ impl Archetype }); } - pub fn take_entity(&mut self, entity_uid: EntityUid) -> Option<ArchetypeEntity> + pub fn take_entity(&mut self, entity_uid: Uid) -> Option<ArchetypeEntity> { let entity_index = self.entity_lookup.remove(&entity_uid)?; @@ -429,7 +425,7 @@ impl Archetype Some(entity) } - fn has_entity(&self, entity_uid: EntityUid) -> bool + fn has_entity(&self, entity_uid: Uid) -> bool { self.entity_lookup.contains_key(&entity_uid) } @@ -438,13 +434,13 @@ impl Archetype #[derive(Debug)] pub struct ArchetypeEntity { - uid: EntityUid, + uid: Uid, components: Vec<EntityComponent>, } impl ArchetypeEntity { - pub fn uid(&self) -> EntityUid + pub fn uid(&self) -> Uid { self.uid } @@ -501,7 +497,7 @@ impl<'archetype> Iterator for EntityIter<'archetype> fn create_non_opt_component_id_set<Item>( component_metadata_iter: impl IntoIterator<Item = Item>, -) -> HashSet<ComponentId> +) -> HashSet<Uid> where Item: Borrow<ComponentMetadata>, { @@ -528,11 +524,11 @@ mod tests use super::Storage; use crate::archetype::Id as ArchetypeId; use crate::component::{ - Id as ComponentId, + Component, IsOptional as ComponentIsOptional, Metadata as ComponentMetadata, }; - use crate::entity::Uid as EntityUid; + use crate::uid::{Kind as UidKind, Uid}; use crate::{self as ecs}; #[derive(Debug, Component)] @@ -569,7 +565,7 @@ mod tests component_storage .push_entity( - EntityUid::new_unique(), + Uid::new_unique(UidKind::Entity), vec![ Box::new(HealthPotion { _hp_restoration: 12 }), Box::new(Hookshot { _range: 50 }), @@ -600,11 +596,11 @@ mod tests let mut components_metadata = [ ComponentMetadata { - id: ComponentId::of::<HealthPotion>(), + id: HealthPotion::id(), is_optional: ComponentIsOptional::No, }, ComponentMetadata { - id: ComponentId::of::<Hookshot>(), + id: Hookshot::id(), is_optional: ComponentIsOptional::No, }, ]; |