From 9b6611cd11199346cbe1f14ad44930347f90dec2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 22 Jun 2024 16:15:12 +0200 Subject: feat(ecs): add query options filter entities --- ecs/src/query/options.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 ecs/src/query/options.rs (limited to 'ecs/src/query') diff --git a/ecs/src/query/options.rs b/ecs/src/query/options.rs new file mode 100644 index 0000000..d895073 --- /dev/null +++ b/ecs/src/query/options.rs @@ -0,0 +1,66 @@ +use std::collections::HashSet; +use std::marker::PhantomData; + +use crate::component::{Component, Id as ComponentId}; +use crate::EntityComponent; + +/// Query options. +pub trait Options +{ + fn entity_filter<'component>( + components: impl IntoIterator, + ) -> bool; +} + +impl Options for () +{ + fn entity_filter<'component>( + _: impl IntoIterator, + ) -> bool + { + true + } +} + +pub struct With +where + ComponentT: Component, +{ + _pd: PhantomData, +} + +impl Options for With +where + ComponentT: Component, +{ + fn entity_filter<'component>( + components: impl IntoIterator, + ) -> bool + { + let ids_set = components + .into_iter() + .map(|component| component.id) + .collect::>(); + + ids_set.contains(&ComponentId::of::()) + } +} + +pub struct Not +where + OptionsT: Options, +{ + _pd: PhantomData, +} + +impl Options for Not +where + OptionsT: Options, +{ + fn entity_filter<'component>( + components: impl IntoIterator, + ) -> bool + { + !OptionsT::entity_filter(components) + } +} -- cgit v1.2.3-18-g5258