diff options
-rw-r--r-- | ecs/src/component/storage.rs | 80 |
1 files changed, 50 insertions, 30 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 9c149ed..44addb7 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -259,7 +259,7 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage> #[cfg(test)] mod tests { - use std::collections::HashSet; + use std::collections::HashMap; use ecs_macros::Component; @@ -270,6 +270,7 @@ mod tests IsOptional as ComponentIsOptional, Metadata as ComponentMetadata, }; + use crate::entity::Uid as EntityUid; use crate::lock::Lock; use crate::{self as ecs, EntityComponent}; @@ -331,18 +332,22 @@ mod tests assert_eq!(component_storage.archetype_lookup.len(), 1); + let mut components_metadata = [ + ComponentMetadata { + id: ComponentId::of::<HealthPotion>(), + is_optional: ComponentIsOptional::No, + }, + ComponentMetadata { + id: ComponentId::of::<Hookshot>(), + is_optional: ComponentIsOptional::No, + }, + ]; + + components_metadata.sort_by_key(|comp_metadata| comp_metadata.id); + let lookup = component_storage .archetype_lookup - .get(&ArchetypeId::from_components_metadata([ - ComponentMetadata { - id: ComponentId::of::<HealthPotion>(), - is_optional: ComponentIsOptional::No, - }, - ComponentMetadata { - id: ComponentId::of::<Hookshot>(), - is_optional: ComponentIsOptional::No, - }, - ])) + .get(&ArchetypeId::from_components_metadata(components_metadata)) .expect("Expected entry in archetype lookup map"); let first_archetype_index = lookup @@ -358,10 +363,15 @@ mod tests let mut component_storage = Storage::default(); component_storage.archetypes.push(Archetype { - component_ids: HashSet::from([ - ComponentId::of::<IronBoots>(), - ComponentId::of::<HealthPotion>(), - ComponentId::of::<Hookshot>(), + component_ids: HashMap::from([ + (ComponentId::of::<IronBoots>(), 0), + (ComponentId::of::<HealthPotion>(), 1), + (ComponentId::of::<Hookshot>(), 2), + ]), + entity_lookup: HashMap::from([ + (EntityUid::new_unique(), 0), + (EntityUid::new_unique(), 1), + (EntityUid::new_unique(), 2), ]), components: vec![ vec![EntityComponent { @@ -383,11 +393,17 @@ mod tests }); component_storage.archetypes.push(Archetype { - component_ids: HashSet::from([ - ComponentId::of::<DekuNut>(), - ComponentId::of::<IronBoots>(), - ComponentId::of::<Bow>(), - ComponentId::of::<Hookshot>(), + component_ids: HashMap::from([ + (ComponentId::of::<DekuNut>(), 0), + (ComponentId::of::<IronBoots>(), 1), + (ComponentId::of::<Bow>(), 2), + (ComponentId::of::<Hookshot>(), 3), + ]), + entity_lookup: HashMap::from([ + (EntityUid::new_unique(), 0), + (EntityUid::new_unique(), 1), + (EntityUid::new_unique(), 2), + (EntityUid::new_unique(), 3), ]), components: vec![ vec![EntityComponent { @@ -430,18 +446,22 @@ mod tests assert_eq!(component_storage.archetype_lookup.len(), 1); + let mut comps_metadata = [ + ComponentMetadata { + id: ComponentId::of::<IronBoots>(), + is_optional: ComponentIsOptional::No, + }, + ComponentMetadata { + id: ComponentId::of::<Hookshot>(), + is_optional: ComponentIsOptional::No, + }, + ]; + + comps_metadata.sort_by_key(|comp_metadata| comp_metadata.id); + let archetypes = component_storage .archetype_lookup - .get(&ArchetypeId::from_components_metadata([ - ComponentMetadata { - id: ComponentId::of::<IronBoots>(), - is_optional: ComponentIsOptional::No, - }, - ComponentMetadata { - id: ComponentId::of::<Hookshot>(), - is_optional: ComponentIsOptional::No, - }, - ])) + .get(&ArchetypeId::from_components_metadata(comps_metadata)) .expect(concat!( "Expected a archetype for IronBoots & Hookshot to be found in the ", "archetype lookup map" |