From daf0bc236df25c0e9f44bc3e30839c16cda3f638 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 11 Nov 2024 00:11:22 +0100 Subject: refactor(ecs): use same ID for entities & components --- ecs/src/component/storage.rs | 72 +++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 38 deletions(-) (limited to 'ecs/src/component/storage.rs') 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_lookup: RefCell>, - entity_archetype_lookup: HashMap, + entity_archetype_lookup: HashMap, } 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>, - ) -> 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>, ) -> 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, + entity_uid: Uid, + component_ids: impl IntoIterator, ) -> 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, + comp_ids_set: &HashSet, archetype_index: usize, ) { @@ -251,7 +250,7 @@ impl Storage fn get_or_create_archetype( &mut self, archetype_id: ArchetypeId, - comp_ids_set: &HashSet, + comp_ids_set: &HashSet, components: &[Box], ) -> 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 { 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, + component_ids: HashSet, archetype_indices: Vec, } #[derive(Debug)] pub struct Archetype { - component_ids: HashMap, - entity_lookup: HashMap, + component_ids: HashMap, + entity_lookup: HashMap, entities: Vec, } impl Archetype { - fn new(component_ids: impl IntoIterator) -> Self + fn new(component_ids: impl IntoIterator) -> Self { Self { component_ids: component_ids @@ -371,10 +370,7 @@ impl Archetype } } - pub fn component_ids_is_superset( - &self, - other_component_ids: &HashSet, - ) -> bool + pub fn component_ids_is_superset(&self, other_component_ids: &HashSet) -> 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 + pub fn get_index_for_component(&self, component_id: &Uid) -> Option { self.component_ids.get(component_id).copied() } fn push_entity( &mut self, - entity_uid: EntityUid, + entity_uid: Uid, components: impl IntoIterator>, ) { @@ -414,7 +410,7 @@ impl Archetype }); } - pub fn take_entity(&mut self, entity_uid: EntityUid) -> Option + pub fn take_entity(&mut self, entity_uid: Uid) -> Option { 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, } 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( component_metadata_iter: impl IntoIterator, -) -> HashSet +) -> HashSet where Item: Borrow, { @@ -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::(), + id: HealthPotion::id(), is_optional: ComponentIsOptional::No, }, ComponentMetadata { - id: ComponentId::of::(), + id: Hookshot::id(), is_optional: ComponentIsOptional::No, }, ]; -- cgit v1.2.3-18-g5258