diff --git a/crates/comet_ecs/src/archetypes.rs b/crates/comet_ecs/src/archetypes.rs index c6ccd11..7736ac7 100755 --- a/crates/comet_ecs/src/archetypes.rs +++ b/crates/comet_ecs/src/archetypes.rs @@ -1,9 +1,9 @@ use comet_structs::ComponentSet; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; #[derive(Debug, Clone)] pub struct Archetypes { - archetypes: HashMap>, + archetypes: HashMap>, } impl Archetypes { @@ -13,27 +13,31 @@ impl Archetypes { } } - pub fn component_sets(&self) -> Vec { - self.archetypes.keys().cloned().collect() + pub fn component_sets(&self) -> impl Iterator { + self.archetypes.keys() } pub fn create_archetype(&mut self, components: ComponentSet) { - self.archetypes.insert(components, HashSet::new()); + self.archetypes.entry(components).or_insert_with(Vec::new); } - pub fn get_archetype(&self, components: &ComponentSet) -> Option<&HashSet> { + pub fn get_archetype(&self, components: &ComponentSet) -> Option<&Vec> { self.archetypes.get(components) } pub fn add_entity_to_archetype(&mut self, components: &ComponentSet, entity: u32) { if let Some(archetype) = self.archetypes.get_mut(components) { - archetype.insert(entity); + if !archetype.iter().any(|&e| e == entity) { + archetype.push(entity); + } } } pub fn remove_entity_from_archetype(&mut self, components: &ComponentSet, entity: u32) { if let Some(archetype) = self.archetypes.get_mut(components) { - archetype.retain(|&id| id != entity); + if let Some(pos) = archetype.iter().position(|&id| id == entity) { + archetype.swap_remove(pos); + } } } diff --git a/crates/comet_ecs/src/scene.rs b/crates/comet_ecs/src/scene.rs index bc0e23c..8634527 100755 --- a/crates/comet_ecs/src/scene.rs +++ b/crates/comet_ecs/src/scene.rs @@ -319,8 +319,8 @@ impl Scene { let mut result = Vec::new(); for archetype_set in self.archetypes.component_sets() { - if component_set.is_subset(&archetype_set) { - if let Some(entities) = self.archetypes.get_archetype(&archetype_set) { + if component_set.is_subset(archetype_set) { + if let Some(entities) = self.archetypes.get_archetype(archetype_set) { for index in entities.iter() { if let Some(gen) = self.generations.get(*index as usize) { if self