From d50a2f6e63c25adf3b64652310c423717bd3966f Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 16 Jun 2024 13:17:57 +0200 Subject: refactor(ecs): add component ID struct --- ecs/src/component/storage.rs | 62 ++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'ecs/src/component/storage.rs') 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_lookup: HashMap>>, - pending_archetype_lookup_entries: Vec>, + pending_archetype_lookup_entries: Vec>, } 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, + component_ids: HashSet, pub components: Vec>, } @@ -154,7 +154,7 @@ struct ArchetypeComponentsHash impl ArchetypeComponentsHash { - fn new(component_ids: impl IntoIterator) -> Self + fn new(component_ids: impl IntoIterator) -> Self { let mut hasher = DefaultHasher::new(); @@ -181,13 +181,13 @@ const unsafe fn nonnull_slice_to_ref_slice(slice: &[NonNull]) -> &[& #[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::(), - TypeId::of::(), + ComponentId::of::(), + ComponentId::of::(), ])) .expect("Expected entry in archetype lookup map"); @@ -274,23 +274,23 @@ mod tests component_storage.archetypes.push(Archetype { component_ids: HashSet::from([ - TypeId::of::(), - TypeId::of::(), - TypeId::of::(), + ComponentId::of::(), + ComponentId::of::(), + ComponentId::of::(), ]), components: vec![ vec![EntityComponent { - id: TypeId::of::(), + id: ComponentId::of::(), name: "Iron boots", component: Lock::new(Box::new(IronBoots)), }], vec![EntityComponent { - id: TypeId::of::(), + id: ComponentId::of::(), name: "Health potion", component: Lock::new(Box::new(HealthPotion { _hp_restoration: 20 })), }], vec![EntityComponent { - id: TypeId::of::(), + id: ComponentId::of::(), 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::(), - TypeId::of::(), - TypeId::of::(), - TypeId::of::(), + ComponentId::of::(), + ComponentId::of::(), + ComponentId::of::(), + ComponentId::of::(), ]), components: vec![ vec![EntityComponent { - id: TypeId::of::(), + id: ComponentId::of::(), name: "Deku nut", component: Lock::new(Box::new(DekuNut { _throwing_damage: 5 })), }], vec![EntityComponent { - id: TypeId::of::(), + id: ComponentId::of::(), name: "Iron boots", component: Lock::new(Box::new(IronBoots)), }], vec![EntityComponent { - id: TypeId::of::(), + id: ComponentId::of::(), name: "Bow", component: Lock::new(Box::new(Bow { _damage: 20 })), }], vec![EntityComponent { - id: TypeId::of::(), + id: ComponentId::of::(), name: "Hookshot", component: Lock::new(Box::new(Hookshot { _range: 67 })), }], @@ -329,8 +329,8 @@ mod tests }); component_storage.add_archetype_lookup_entry(&[ - TypeId::of::(), - TypeId::of::(), + ComponentId::of::(), + ComponentId::of::(), ]); 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::(), - TypeId::of::(), + ComponentId::of::(), + ComponentId::of::(), ])) .expect(concat!( "Expected a archetype for IronBoots & Hookshot to be found in the ", -- cgit v1.2.3-18-g5258