From a5c45dab18399461aff5dc13c471ea6c5ec80c34 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 30 Jul 2024 11:38:20 +0200 Subject: refactor(ecs): give archetype component IDs once in component storage --- ecs/src/component/storage.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'ecs') diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 1db9227..70981a3 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -65,13 +65,17 @@ impl Storage .map(|component| component.id()), )) .or_insert_with(|| { - self.archetypes.push(Archetype::default()); + self.archetypes.push(Archetype::new( + components.iter().map(|component| component.id()), + )); vec![self.archetypes.len() - 1] }); if archetype_indices.is_empty() { - self.archetypes.push(Archetype::default()); + self.archetypes.push(Archetype::new( + components.iter().map(|component| component.id()), + )); archetype_indices.push(self.archetypes.len() - 1); } @@ -86,10 +90,6 @@ impl Storage ) .expect("Archetype is gone"); - archetype - .component_ids - .extend(components.iter().map(|component| component.id())); - archetype.components.push( components .into_iter() @@ -147,13 +147,24 @@ impl TypeName for Storage } } -#[derive(Debug, Default)] +#[derive(Debug)] pub struct Archetype { component_ids: HashSet, pub components: Vec>, } +impl Archetype +{ + fn new(component_ids: impl IntoIterator) -> Self + { + Self { + component_ids: component_ids.into_iter().collect(), + components: Vec::new(), + } + } +} + #[derive(Debug)] pub struct ArchetypeRefIter<'component_storage> { -- cgit v1.2.3-18-g5258