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 +++++++++++++++-------------------- ecs/src/query/options.rs | 59 ----------------------------------------------- ecs/src/query/term.rs | 38 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 84 deletions(-) delete mode 100644 ecs/src/query/options.rs create mode 100644 ecs/src/query/term.rs (limited to 'ecs/src/query') 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, >; diff --git a/ecs/src/query/options.rs b/ecs/src/query/options.rs deleted file mode 100644 index 6318359..0000000 --- a/ecs/src/query/options.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::marker::PhantomData; - -use hashbrown::HashSet; - -use crate::component::Component; -use crate::EntityComponent; - -/// Query options. -pub trait Options -{ - fn entity_filter(components: &[EntityComponent]) -> bool; -} - -impl Options for () -{ - fn entity_filter(_components: &[EntityComponent]) -> bool - { - true - } -} - -pub struct With -where - ComponentT: Component, -{ - _pd: PhantomData, -} - -impl Options for With -where - ComponentT: Component, -{ - fn entity_filter(components: &[EntityComponent]) -> bool - { - let ids_set = components - .iter() - .map(|component| component.id) - .collect::>(); - - ids_set.contains(&ComponentT::id()) - } -} - -pub struct Not -where - OptionsT: Options, -{ - _pd: PhantomData, -} - -impl Options for Not -where - OptionsT: Options, -{ - fn entity_filter(components: &[EntityComponent]) -> bool - { - !OptionsT::entity_filter(components) - } -} diff --git a/ecs/src/query/term.rs b/ecs/src/query/term.rs new file mode 100644 index 0000000..7f24147 --- /dev/null +++ b/ecs/src/query/term.rs @@ -0,0 +1,38 @@ +use std::marker::PhantomData; + +use crate::component::Component; +use crate::query::{TermWithoutField, TermsBuilder, TermsBuilderInterface}; + +pub struct With +where + ComponentT: Component, +{ + _pd: PhantomData, +} + +impl TermWithoutField for With +where + ComponentT: Component, +{ + fn apply_to_terms_builder(terms_builder: &mut TermsBuilder<'_>) + { + terms_builder.with::(); + } +} + +pub struct Without +where + ComponentT: Component, +{ + _pd: PhantomData, +} + +impl TermWithoutField for Without +where + ComponentT: Component, +{ + fn apply_to_terms_builder(terms_builder: &mut TermsBuilder<'_>) + { + terms_builder.without::(); + } +} -- cgit v1.2.3-18-g5258