diff options
author | HampusM <hampus@hampusmat.com> | 2024-06-16 13:17:57 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-06-16 13:17:57 +0200 |
commit | d50a2f6e63c25adf3b64652310c423717bd3966f (patch) | |
tree | 3edf4ee3d1eec93a52a8de4fdc5a7be5c487c711 /ecs/src/component/storage.rs | |
parent | 69d90ece7f54996f0f51fc120a38d37717c5248e (diff) |
refactor(ecs): add component ID struct
Diffstat (limited to 'ecs/src/component/storage.rs')
-rw-r--r-- | ecs/src/component/storage.rs | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index cc9e911..bd53da0 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -1,9 +1,9 @@ -use std::any::{type_name, TypeId}; +use std::any::type_name; use std::collections::{HashMap, HashSet}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::ptr::NonNull; -use crate::component::{Component, IsOptional as ComponentIsOptional}; +use crate::component::{Component, Id as ComponentId, IsOptional as ComponentIsOptional}; use crate::lock::Lock; use crate::type_name::TypeName; use crate::EntityComponent; @@ -13,14 +13,14 @@ pub struct ComponentStorage { archetypes: Vec<Archetype>, archetype_lookup: HashMap<ArchetypeComponentsHash, Vec<NonNull<Archetype>>>, - pending_archetype_lookup_entries: Vec<Vec<TypeId>>, + pending_archetype_lookup_entries: Vec<Vec<ComponentId>>, } impl ComponentStorage { pub fn find_entities( &self, - component_ids: &[(TypeId, ComponentIsOptional)], + component_ids: &[(ComponentId, ComponentIsOptional)], ) -> Option<&[&Archetype]> { let ids = component_ids @@ -63,7 +63,7 @@ impl ComponentStorage components .iter() .filter(|component| !component.is_optional()) - .map(|component| (*component).type_id()), + .map(|component| component.id()), )) .or_insert_with(|| { self.archetypes.push(Archetype::default()); @@ -83,13 +83,13 @@ impl ComponentStorage archetype .component_ids - .extend(components.iter().map(|component| (*component).type_id())); + .extend(components.iter().map(|component| component.id())); archetype.components.push( components .into_iter() .map(|component| EntityComponent { - id: (*component).type_id(), + id: component.id(), name: component.type_name(), component: Lock::new(component), }) @@ -97,7 +97,7 @@ impl ComponentStorage ); } - pub fn add_archetype_lookup_entry(&mut self, component_ids: &[TypeId]) + pub fn add_archetype_lookup_entry(&mut self, component_ids: &[ComponentId]) { self.pending_archetype_lookup_entries .push(component_ids.to_vec()); @@ -142,7 +142,7 @@ impl TypeName for ComponentStorage #[derive(Debug, Default)] pub struct Archetype { - component_ids: HashSet<TypeId>, + component_ids: HashSet<ComponentId>, pub components: Vec<Vec<EntityComponent>>, } @@ -154,7 +154,7 @@ struct ArchetypeComponentsHash impl ArchetypeComponentsHash { - fn new(component_ids: impl IntoIterator<Item = TypeId>) -> Self + fn new(component_ids: impl IntoIterator<Item = ComponentId>) -> Self { let mut hasher = DefaultHasher::new(); @@ -181,13 +181,13 @@ const unsafe fn nonnull_slice_to_ref_slice<Item>(slice: &[NonNull<Item>]) -> &[& #[cfg(test)] mod tests { - use std::any::TypeId; use std::collections::HashSet; use std::ptr::addr_of; use ecs_macros::Component; use super::{Archetype, ArchetypeComponentsHash, ComponentStorage}; + use crate::component::Id as ComponentId; use crate::lock::Lock; use crate::{self as ecs, EntityComponent}; @@ -252,8 +252,8 @@ mod tests let lookup = component_storage .archetype_lookup .get(&ArchetypeComponentsHash::new([ - TypeId::of::<HealthPotion>(), - TypeId::of::<Hookshot>(), + ComponentId::of::<HealthPotion>(), + ComponentId::of::<Hookshot>(), ])) .expect("Expected entry in archetype lookup map"); @@ -274,23 +274,23 @@ mod tests component_storage.archetypes.push(Archetype { component_ids: HashSet::from([ - TypeId::of::<IronBoots>(), - TypeId::of::<HealthPotion>(), - TypeId::of::<Hookshot>(), + ComponentId::of::<IronBoots>(), + ComponentId::of::<HealthPotion>(), + ComponentId::of::<Hookshot>(), ]), components: vec![ vec![EntityComponent { - id: TypeId::of::<IronBoots>(), + id: ComponentId::of::<IronBoots>(), name: "Iron boots", component: Lock::new(Box::new(IronBoots)), }], vec![EntityComponent { - id: TypeId::of::<HealthPotion>(), + id: ComponentId::of::<HealthPotion>(), name: "Health potion", component: Lock::new(Box::new(HealthPotion { _hp_restoration: 20 })), }], vec![EntityComponent { - id: TypeId::of::<Hookshot>(), + id: ComponentId::of::<Hookshot>(), name: "Hookshot", component: Lock::new(Box::new(Hookshot { _range: 67 })), }], @@ -299,29 +299,29 @@ mod tests component_storage.archetypes.push(Archetype { component_ids: HashSet::from([ - TypeId::of::<DekuNut>(), - TypeId::of::<IronBoots>(), - TypeId::of::<Bow>(), - TypeId::of::<Hookshot>(), + ComponentId::of::<DekuNut>(), + ComponentId::of::<IronBoots>(), + ComponentId::of::<Bow>(), + ComponentId::of::<Hookshot>(), ]), components: vec![ vec![EntityComponent { - id: TypeId::of::<DekuNut>(), + id: ComponentId::of::<DekuNut>(), name: "Deku nut", component: Lock::new(Box::new(DekuNut { _throwing_damage: 5 })), }], vec![EntityComponent { - id: TypeId::of::<IronBoots>(), + id: ComponentId::of::<IronBoots>(), name: "Iron boots", component: Lock::new(Box::new(IronBoots)), }], vec![EntityComponent { - id: TypeId::of::<Bow>(), + id: ComponentId::of::<Bow>(), name: "Bow", component: Lock::new(Box::new(Bow { _damage: 20 })), }], vec![EntityComponent { - id: TypeId::of::<Hookshot>(), + id: ComponentId::of::<Hookshot>(), name: "Hookshot", component: Lock::new(Box::new(Hookshot { _range: 67 })), }], @@ -329,8 +329,8 @@ mod tests }); component_storage.add_archetype_lookup_entry(&[ - TypeId::of::<IronBoots>(), - TypeId::of::<Hookshot>(), + ComponentId::of::<IronBoots>(), + ComponentId::of::<Hookshot>(), ]); assert_eq!(component_storage.pending_archetype_lookup_entries.len(), 1); @@ -342,8 +342,8 @@ mod tests let archetypes = component_storage .archetype_lookup .get(&ArchetypeComponentsHash::new([ - TypeId::of::<IronBoots>(), - TypeId::of::<Hookshot>(), + ComponentId::of::<IronBoots>(), + ComponentId::of::<Hookshot>(), ])) .expect(concat!( "Expected a archetype for IronBoots & Hookshot to be found in the ", |