summaryrefslogtreecommitdiff
path: root/ecs/src/component
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-07-30 11:38:20 +0200
committerHampusM <hampus@hampusmat.com>2024-07-30 11:38:20 +0200
commita5c45dab18399461aff5dc13c471ea6c5ec80c34 (patch)
tree1a850c1dde374637ac83fad94e04d65a63262214 /ecs/src/component
parent3c13cf569d7d65da1d194f559fca3e43b51b724c (diff)
refactor(ecs): give archetype component IDs once in component storage
Diffstat (limited to 'ecs/src/component')
-rw-r--r--ecs/src/component/storage.rs25
1 files changed, 18 insertions, 7 deletions
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<ComponentId>,
pub components: Vec<Vec<EntityComponent>>,
}
+impl Archetype
+{
+ fn new(component_ids: impl IntoIterator<Item = ComponentId>) -> Self
+ {
+ Self {
+ component_ids: component_ids.into_iter().collect(),
+ components: Vec::new(),
+ }
+ }
+}
+
#[derive(Debug)]
pub struct ArchetypeRefIter<'component_storage>
{