diff options
Diffstat (limited to 'ecs/src/query/term.rs')
-rw-r--r-- | ecs/src/query/term.rs | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/ecs/src/query/term.rs b/ecs/src/query/term.rs index ce453f0..78668c5 100644 --- a/ecs/src/query/term.rs +++ b/ecs/src/query/term.rs @@ -1,7 +1,13 @@ +use std::any::type_name; use std::marker::PhantomData; -use crate::component::Component; -use crate::query::{TermWithoutField, TermsBuilder, TermsBuilderInterface}; +use crate::component::{Component, HandleFromEntityComponentRef, Ref as ComponentRef}; +use crate::query::{ + TermWithField, + TermWithoutField, + TermsBuilder, + TermsBuilderInterface, +}; pub struct With<ComponentT> where @@ -40,3 +46,35 @@ where terms_builder.without::<ComponentT>(); } } + +impl<ComponentRefT> TermWithField for Option<ComponentRefT> +where + ComponentRefT: ComponentRef, +{ + type Field<'a> = Option<ComponentRefT::Handle<'a>>; + + fn apply_to_terms_builder<const MAX_TERM_CNT: usize>( + _terms_builder: &mut TermsBuilder<MAX_TERM_CNT>, + ) + { + } + + fn get_field<'world>( + entity_handle: &crate::entity::Handle<'world>, + world: &'world crate::World, + ) -> Self::Field<'world> + { + Some( + ComponentRefT::Handle::<'world>::from_entity_component_ref( + Some(entity_handle.get_component(ComponentRefT::Component::id())?), + world, + ) + .unwrap_or_else(|err| { + panic!( + "Creating handle to component {} failed: {err}", + type_name::<ComponentRefT::Component>() + ); + }), + ) + } +} |