summaryrefslogtreecommitdiff
path: root/ecs/src/query
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-04-08 17:19:30 +0200
committerHampusM <hampus@hampusmat.com>2025-04-08 17:19:30 +0200
commit7276f9c72f53f02820c3238f72d099221daf7afd (patch)
treed523d0c01a9cd35bcc44401b46d9c8d36bbb10c1 /ecs/src/query
parente4818dd4f0a57a2c9af8859253f570607f64bb12 (diff)
refactor(ecs): replace optional components with Option query term
Diffstat (limited to 'ecs/src/query')
-rw-r--r--ecs/src/query/term.rs42
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>()
+ );
+ }),
+ )
+ }
+}