diff options
author | HampusM <hampus@hampusmat.com> | 2024-06-22 16:15:12 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-06-22 16:15:12 +0200 |
commit | 9b6611cd11199346cbe1f14ad44930347f90dec2 (patch) | |
tree | 6c9c3717878e1603b99686f6eccda113828ae2a5 /ecs/src/query | |
parent | a0dce2164cf348416ea15c63c5baa52afa66508f (diff) |
feat(ecs): add query options filter entities
Diffstat (limited to 'ecs/src/query')
-rw-r--r-- | ecs/src/query/options.rs | 66 |
1 files changed, 66 insertions, 0 deletions
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<Item = &'component EntityComponent>, + ) -> bool; +} + +impl Options for () +{ + fn entity_filter<'component>( + _: impl IntoIterator<Item = &'component EntityComponent>, + ) -> bool + { + true + } +} + +pub struct With<ComponentT> +where + ComponentT: Component, +{ + _pd: PhantomData<ComponentT>, +} + +impl<ComponentT> Options for With<ComponentT> +where + ComponentT: Component, +{ + fn entity_filter<'component>( + components: impl IntoIterator<Item = &'component EntityComponent>, + ) -> bool + { + let ids_set = components + .into_iter() + .map(|component| component.id) + .collect::<HashSet<_>>(); + + ids_set.contains(&ComponentId::of::<ComponentT>()) + } +} + +pub struct Not<OptionsT> +where + OptionsT: Options, +{ + _pd: PhantomData<OptionsT>, +} + +impl<OptionsT> Options for Not<OptionsT> +where + OptionsT: Options, +{ + fn entity_filter<'component>( + components: impl IntoIterator<Item = &'component EntityComponent>, + ) -> bool + { + !OptionsT::entity_filter(components) + } +} |