From fe62665b1d62d36ee0839e6bf24e3841ea667da9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 21 Mar 2025 20:05:53 +0100 Subject: refactor(ecs): replace query options with fieldless terms --- ecs/src/query/flexible.rs | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'ecs/src/query/flexible.rs') diff --git a/ecs/src/query/flexible.rs b/ecs/src/query/flexible.rs index a3e8701..7d24dc9 100644 --- a/ecs/src/query/flexible.rs +++ b/ecs/src/query/flexible.rs @@ -1,10 +1,13 @@ //! Low-level querying. -use std::iter::{repeat_n, Filter, FlatMap, RepeatN, Zip}; +use std::iter::{repeat_n, FlatMap, RepeatN, Zip}; use crate::component::storage::archetype::{Archetype, ArchetypeEntity, EntityIter}; -use crate::component::storage::{ArchetypeRefIter, Storage as ComponentStorage}; +use crate::component::storage::{ + ArchetypeRefIter, + ArchetypeSearchTerms, + Storage as ComponentStorage, +}; use crate::lock::ReadGuard; -use crate::query::options::Options; use crate::query::Terms; use crate::uid::Uid; use crate::{EntityComponent, World}; @@ -21,19 +24,21 @@ impl<'world, 'terms> Query<'world, 'terms> { /// Iterates over the entities matching this query. #[must_use] - pub fn iter(&self) -> Iter<'_> + pub fn iter(&self) -> Iter<'_> { Iter { iter: self .component_storage - .search_archetypes(self.terms.required_components.as_ref()) + .search_archetypes(ArchetypeSearchTerms { + required_components: &self.terms.required_components, + excluded_components: &self.terms.excluded_components, + }) .flat_map( (|archetype| { repeat_n(archetype, archetype.entity_cnt()) .zip(archetype.entities()) }) as ComponentIterMapFn, - ) - .filter(|(_, entity)| OptionsT::entity_filter(&entity.components)), + ), } } @@ -85,16 +90,11 @@ impl<'query> EntityHandle<'query> #[inline] #[must_use] - pub fn components(&self) -> &'query [EntityComponent] + pub fn get_component(&self, component_uid: Uid) -> Option<&'query EntityComponent> { - &self.entity.components - } + let index = self.archetype.get_index_for_component(component_uid)?; - #[inline] - #[must_use] - pub fn get_component_index(&self, component_uid: Uid) -> Option - { - self.archetype.get_index_for_component(component_uid) + Some(self.entity.components.get(index).unwrap()) } } @@ -102,14 +102,8 @@ type ComponentIterMapFnOutput<'a> = Zip, EntityIter<'a>>; type ComponentIterMapFn = for<'a> fn(&'a Archetype) -> ComponentIterMapFnOutput<'a>; -type ComponentIterFilterFn = - for<'a, 'b> fn(&'a (&'b Archetype, &'b ArchetypeEntity)) -> bool; - -type QueryEntityIter<'query> = Filter< - FlatMap< - ArchetypeRefIter<'query>, - ComponentIterMapFnOutput<'query>, - ComponentIterMapFn, - >, - ComponentIterFilterFn, +type QueryEntityIter<'query> = FlatMap< + ArchetypeRefIter<'query, 'query>, + ComponentIterMapFnOutput<'query>, + ComponentIterMapFn, >; -- cgit v1.2.3-18-g5258