From 7276f9c72f53f02820c3238f72d099221daf7afd Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 8 Apr 2025 17:19:30 +0200 Subject: refactor(ecs): replace optional components with Option query term --- ecs/src/query/term.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'ecs/src/query') 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 where @@ -40,3 +46,35 @@ where terms_builder.without::(); } } + +impl TermWithField for Option +where + ComponentRefT: ComponentRef, +{ + type Field<'a> = Option>; + + fn apply_to_terms_builder( + _terms_builder: &mut TermsBuilder, + ) + { + } + + 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::() + ); + }), + ) + } +} -- cgit v1.2.3-18-g5258