diff options
Diffstat (limited to 'ecs/src/query.rs')
| -rw-r--r-- | ecs/src/query.rs | 166 |
1 files changed, 106 insertions, 60 deletions
diff --git a/ecs/src/query.rs b/ecs/src/query.rs index 1d27b7a..5f13579 100644 --- a/ecs/src/query.rs +++ b/ecs/src/query.rs @@ -3,10 +3,14 @@ use std::marker::PhantomData; use seq_macro::seq; -use crate::component::{Component, HandleFromEntityComponentRef, Ref as ComponentRef}; +use crate::component::{ + Component, + Handle as ComponentHandle, + HandleMut as ComponentHandleMut, +}; use crate::entity::Handle as EntityHandle; use crate::query::flexible::{Iter as FlexibleQueryIter, Query as FlexibleQuery}; -use crate::system::{Param as SystemParam, System}; +use crate::system::{Metadata as SystemMetadata, Param as SystemParam}; use crate::uid::{Kind as UidKind, Uid, With as WithUid}; use crate::util::array_vec::ArrayVec; use crate::util::Array; @@ -15,15 +19,16 @@ use crate::World; pub mod flexible; pub mod term; +// A term tuple type can have a maximum of 17 elements +pub const MAX_TERM_CNT: usize = 17; + #[derive(Debug)] pub struct Query<'world, FieldTerms, FieldlessTerms = ()> where FieldTerms: TermWithFieldTuple, FieldlessTerms: TermWithoutFieldTuple, { - world: &'world World, - // A term tuple type can have a maximum of 17 elements - inner: FlexibleQuery<'world, 17>, + inner: FlexibleQuery<'world, MAX_TERM_CNT>, _pd: PhantomData<(FieldTerms, FieldlessTerms)>, } @@ -42,8 +47,8 @@ where tracing::trace!("Searching for {}", std::any::type_name::<FieldTerms>()); Iter { - world: self.world, - iter: self.inner.iter(), + world: self.inner.world(), + inner: self.inner.iter(), comps_pd: PhantomData, } } @@ -58,7 +63,7 @@ where tracing::trace!("Searching for {}", std::any::type_name::<FieldTerms>()); ComponentAndEuidIter { - world: self.world, + world: self.inner.world(), iter: self.inner.iter(), comps_pd: PhantomData, } @@ -80,8 +85,8 @@ where tracing::trace!("Searching for {}", std::any::type_name::<FieldTerms>()); Iter { - world: self.world, - iter: func(self.inner.iter()), + world: self.inner.world(), + inner: func(self.inner.iter()), comps_pd: PhantomData, } } @@ -93,6 +98,25 @@ where Some(self.inner.iter().nth(entity_index)?.uid()) } + /// Returns a new `Query` created from a [`FlexibleQuery`]. + /// + /// # Important notes + /// The terms in `FieldTerms` and `FieldlessTerms` must be compatible with the terms + /// in the given [`FlexibleQuery`], otherwise any method call or iterating might + /// panic. + #[must_use] + pub fn from_flexible_query( + flexible_query: FlexibleQuery<'world, MAX_TERM_CNT>, + ) -> Self + { + // TODO: Check compatability of terms + + Self { + inner: flexible_query, + _pd: PhantomData, + } + } + pub(crate) fn new(world: &'world World) -> Self { let mut terms_builder = Terms::builder(); @@ -101,7 +125,6 @@ where FieldlessTerms::apply_terms_to_builder(&mut terms_builder); Self { - world, inner: world.flexible_query(terms_builder.build()), _pd: PhantomData, } @@ -131,17 +154,7 @@ where { type Input = (); - fn initialize<SystemImpl>( - _system: &mut impl System<'world, SystemImpl>, - _input: Self::Input, - ) - { - } - - fn new<SystemImpl>( - _system: &'world impl System<'world, SystemImpl>, - world: &'world World, - ) -> Self + fn new(world: &'world World, _system_metadata: &SystemMetadata) -> Self { Self::new(world) } @@ -163,12 +176,14 @@ impl<const MAX_TERM_CNT: usize> Terms<MAX_TERM_CNT> } #[derive(Debug, Default)] +#[must_use] pub struct TermsBuilder<const MAX_TERM_CNT: usize> { required_components: ArrayVec<Uid, MAX_TERM_CNT>, excluded_components: ArrayVec<Uid, MAX_TERM_CNT>, } +#[allow(clippy::return_self_not_must_use)] pub trait TermsBuilderInterface { fn with<WithUidT: WithUid>(self) -> Self; @@ -228,7 +243,7 @@ impl_terms_builder! { ids.as_mut().sort(); } - if self.required_components.len() == 0 { + if self.required_components.is_empty() { self.required_components.extend(ids); return self; } @@ -260,7 +275,7 @@ impl_terms_builder! { ids.as_mut().sort(); } - if self.excluded_components.len() == 0 { + if self.excluded_components.is_empty() { self.excluded_components.extend(ids); return self; } @@ -288,10 +303,11 @@ impl_terms_builder! { impl<const MAX_TERM_CNT: usize> TermsBuilder<MAX_TERM_CNT> { + #[must_use] pub fn build(self) -> Terms<MAX_TERM_CNT> { - assert!(self.required_components.is_sorted()); - assert!(self.excluded_components.is_sorted()); + debug_assert!(self.required_components.is_sorted()); + debug_assert!(self.excluded_components.is_sorted()); Terms { required_components: self.required_components, @@ -321,15 +337,56 @@ pub trait TermWithField ) -> Self::Field<'world>; } -impl<ComponentRefT: ComponentRef> TermWithField for ComponentRefT +impl<ComponentT: Component> TermWithField for &ComponentT { - type Field<'a> = ComponentRefT::Handle<'a>; + type Field<'a> = ComponentHandle<'a, ComponentT>; fn apply_to_terms_builder<const MAX_TERM_CNT: usize>( terms_builder: &mut TermsBuilder<MAX_TERM_CNT>, ) { - terms_builder.with::<ComponentRefT::Component>(); + terms_builder.with::<ComponentT>(); + } + + fn get_field<'world>( + entity_handle: &EntityHandle<'world>, + _world: &'world World, + ) -> Self::Field<'world> + { + assert_eq!(ComponentT::id().kind(), UidKind::Component); + + let Some(component) = entity_handle + .get_matching_components(ComponentT::id()) + .next() + else { + panic!( + concat!( + "Component {} was not found in entity {}. There ", + "is most likely a bug in the entity querying" + ), + type_name::<ComponentT>(), + entity_handle.uid() + ); + }; + + Self::Field::from_entity_component_ref(&component).unwrap_or_else(|err| { + panic!( + "Creating handle to component {} failed: {err}", + type_name::<ComponentT>() + ); + }) + } +} + +impl<ComponentT: Component> TermWithField for &mut ComponentT +{ + type Field<'a> = ComponentHandleMut<'a, ComponentT>; + + fn apply_to_terms_builder<const MAX_TERM_CNT: usize>( + terms_builder: &mut TermsBuilder<MAX_TERM_CNT>, + ) + { + terms_builder.with::<ComponentT>(); } fn get_field<'world>( @@ -337,18 +394,26 @@ impl<ComponentRefT: ComponentRef> TermWithField for ComponentRefT world: &'world World, ) -> Self::Field<'world> { - assert_eq!(ComponentRefT::Component::id().kind(), UidKind::Component); - - Self::Field::from_entity_component_ref( - entity_handle - .get_matching_components(ComponentRefT::Component::id()) - .next(), - world, - ) - .unwrap_or_else(|err| { + assert_eq!(ComponentT::id().kind(), UidKind::Component); + + let Some(component) = entity_handle + .get_matching_components(ComponentT::id()) + .next() + else { + panic!( + concat!( + "Component {} was not found in entity {}. There ", + "is most likely a bug in the entity querying" + ), + type_name::<ComponentT>(), + entity_handle.uid() + ); + }; + + Self::Field::from_entity_component_ref(&component, world).unwrap_or_else(|err| { panic!( "Creating handle to component {} failed: {err}", - type_name::<ComponentRefT::Component>() + type_name::<ComponentT>() ); }) } @@ -381,29 +446,10 @@ where EntityHandleIter: Iterator<Item = EntityHandle<'query>>, { world: &'world World, - iter: EntityHandleIter, + inner: EntityHandleIter, comps_pd: PhantomData<FieldTerms>, } -impl<'query, 'world, FieldTerms, EntityHandleIter> - Iter<'query, 'world, FieldTerms, EntityHandleIter> -where - FieldTerms: TermWithFieldTuple, - EntityHandleIter: Iterator<Item = EntityHandle<'query>>, - 'world: 'query, -{ - /// Creates a new iterator from the given entity handle iterator. - /// - /// # Important - /// All of the yielded entities of the entity handle iterator should match the - /// terms `Terms`. The [`Self::next`] function will panic if it encounters a - /// entity that does not match the terms `Terms`. - pub fn new(world: &'world World, iter: EntityHandleIter) -> Self - { - Self { world, iter, comps_pd: PhantomData } - } -} - impl<'query, 'world, FieldTerms, EntityHandleIter> Iterator for Iter<'query, 'world, FieldTerms, EntityHandleIter> where @@ -415,7 +461,7 @@ where fn next(&mut self) -> Option<Self::Item> { - let entity_handle = self.iter.next()?; + let entity_handle = self.inner.next()?; Some(FieldTerms::get_fields(&entity_handle, self.world)) } |
