diff options
author | HampusM <hampus@hampusmat.com> | 2025-03-18 16:59:53 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-03-19 20:24:12 +0100 |
commit | 76e7e612e7b516bf52b508ae5bb367b1ddc3babc (patch) | |
tree | 1975b4db0eecb74583ab78e58afa50b5326347b2 /ecs/src/component | |
parent | 44cd47fd67102902b649c98b85c5abb9a0da39f8 (diff) |
refactor(ecs): replace component::RefSequence with query terms
Diffstat (limited to 'ecs/src/component')
-rw-r--r-- | ecs/src/component/storage.rs | 75 |
1 files changed, 23 insertions, 52 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 5b7b101..21f9a37 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -16,7 +16,7 @@ use crate::component::storage::graph::{ ArchetypeEdges, Graph, }; -use crate::component::{Component, Metadata as ComponentMetadata}; +use crate::component::Component; use crate::type_name::TypeName; use crate::uid::{Kind as UidKind, Uid}; use crate::util::{BorrowedOrOwned, Either, StreamingIterator, VecExt}; @@ -36,12 +36,9 @@ pub struct Storage impl Storage { - pub fn search_archetypes( - &self, - component_metadata: &[ComponentMetadata], - ) -> ArchetypeRefIter<'_> + pub fn search_archetypes(&self, component_ids: &[Uid]) -> ArchetypeRefIter<'_> { - let archetype_id = ArchetypeId::from_components_metadata(component_metadata); + let archetype_id = ArchetypeId::new(&component_ids); let Some(add_edge_recursive_iter) = self.graph.dfs_archetype_add_edges(archetype_id) @@ -50,19 +47,10 @@ impl Storage .borrow_mut() .push(ImaginaryArchetype { id: archetype_id, - component_ids: component_metadata - .iter() - .filter_map(|comp_metadata| { - if comp_metadata.is_optional { - return None; - } - - Some(comp_metadata.id) - }) - .collect::<Vec<_>>(), + component_ids: component_ids.to_vec(), }); - let found_archetypes = self.find_all_archetype_with_comps(component_metadata); + let found_archetypes = self.find_all_archetype_with_comps(component_ids); return ArchetypeRefIter { storage: self, @@ -333,16 +321,8 @@ impl Storage } } - fn find_all_archetype_with_comps( - &self, - component_metadata: &[ComponentMetadata], - ) -> Vec<ArchetypeId> + fn find_all_archetype_with_comps(&self, component_ids: &[Uid]) -> Vec<ArchetypeId> { - let comp_metadata_not_opt_cnt = component_metadata - .iter() - .filter(|comp_metadata| !comp_metadata.is_optional) - .count(); - let Some(mut search_iter) = self.graph.dfs_archetype_add_edges(ArchetypeId::new(&[])) else { @@ -362,16 +342,13 @@ impl Storage .get_node_by_id(node_id) .expect("Graph node found through DFS doesn't exist"); - if node.archetype().component_cnt() < comp_metadata_not_opt_cnt { + if node.archetype().component_cnt() < component_ids.len() { continue; } - if !component_metadata + if !component_ids .iter() - .filter(|comp_metadata| !comp_metadata.is_optional) - .all(|comp_metadata| { - node.archetype().has_component_with_id(comp_metadata.id) - }) + .all(|comp_id| node.archetype().has_component_with_id(*comp_id)) { continue; } @@ -431,14 +408,12 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage> add_edge_archetype_id, add_edge_component_id, } => { - let mut add_edge_archetype_comps = archetype - .component_ids_sorted() - .map(|id| ComponentMetadata { id, is_optional: false }) - .collect::<Vec<_>>(); + let mut add_edge_archetype_comps = + archetype.component_ids_sorted().collect::<Vec<_>>(); add_edge_archetype_comps.insert_at_partition_point_by_key( - ComponentMetadata::new_non_optional(add_edge_component_id), - |comp_metadata| comp_metadata.id, + add_edge_component_id, + |comp_id| *comp_id, ); self.storage.imaginary_archetypes.borrow_mut().push( @@ -446,7 +421,7 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage> id: add_edge_archetype_id, component_ids: add_edge_archetype_comps .iter() - .map(|comp_metadata| comp_metadata.id) + .map(|comp_id| *comp_id) .collect::<Vec<_>>(), }, ); @@ -457,10 +432,7 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage> self.dfs_iter.push(( BorrowedOrOwned::Owned(Archetype::new( add_edge_archetype_id, - add_edge_archetype_comps - .iter() - .map(|metadata| metadata.id) - .collect::<Vec<_>>(), + add_edge_archetype_comps.clone(), )), found.into_iter(), )); @@ -485,7 +457,7 @@ impl ArchetypeRefIter<'_> { fn find_edges_of_imaginary_archetype( &self, - imaginary_archetype_comps: &[ComponentMetadata], + imaginary_archetype_comps: &[Uid], ) -> Vec<(Uid, ArchetypeEdges)> { self.storage @@ -500,17 +472,16 @@ impl ArchetypeRefIter<'_> let unique_comp_id = found_archetype .component_ids_sorted() - .find(|id| { - !imaginary_archetype_comps - .iter() - .any(|comp_metadata| comp_metadata.id == *id) + .find(|found_archetype_comp_id| { + !imaginary_archetype_comps.iter().any( + |imaginary_archetype_comp_id| { + *imaginary_archetype_comp_id == *found_archetype_comp_id + }, + ) }) .expect("Oh noooo"); - let mut add_edge_comp_ids = imaginary_archetype_comps - .iter() - .map(|comp_metadata| comp_metadata.id) - .collect::<Vec<_>>(); + let mut add_edge_comp_ids = imaginary_archetype_comps.to_vec(); add_edge_comp_ids .insert_at_partition_point_by_key(unique_comp_id, |id| *id); |