diff options
Diffstat (limited to 'ecs/src/pair.rs')
-rw-r--r-- | ecs/src/pair.rs | 114 |
1 files changed, 96 insertions, 18 deletions
diff --git a/ecs/src/pair.rs b/ecs/src/pair.rs index 85be9f6..985a2b3 100644 --- a/ecs/src/pair.rs +++ b/ecs/src/pair.rs @@ -19,8 +19,91 @@ use crate::query::{ TermsBuilderInterface, }; use crate::uid::{PairParams as UidPairParams, Uid, With as WithUid}; +use crate::util::impl_multiple; use crate::{Component, EntityComponentRef, World}; +/// Pair builder. +#[derive(Debug)] +pub struct Builder<Relation, Target> +{ + relation: Relation, + target: Target, +} + +impl<Relation, Target> Builder<Relation, Target> +{ + pub fn relation<NewRelation: Component>(self) -> Builder<Uid, Target> + { + Builder { + relation: NewRelation::id(), + target: self.target, + } + } + + pub fn relation_id(self, id: Uid) -> Builder<Uid, Target> + { + Builder { relation: id, target: self.target } + } + + pub fn target<NewTarget: Component>(self) -> Builder<Relation, Uid> + { + Builder { + relation: self.relation, + target: NewTarget::id(), + } + } + + pub fn target_id(self, id: Uid) -> Builder<Relation, Uid> + { + Builder { relation: self.relation, target: id } + } +} + +impl_multiple!( + Builder, + (impl<Target> _<Uid, Target>, impl<Target> _<(), Target>) + (ty_param_1, ty_param_2) => { + pub fn target_as_data<NewTarget: Component>( + self, + data: NewTarget, + ) -> Builder<$ty_param_1, NewTarget> + { + Builder { + relation: self.relation, + target: data, + } + } + } +); + +impl_multiple!( + Builder, + ( + impl _<Uid, Uid>, + impl<Relation: Component> _<Relation, Uid>, + impl<Target: Component> _<Uid, Target>, + impl<Relation: Component, Target: Component> _<Relation, Target> + ) + (ty_param_1, ty_param_2) => { + #[must_use] + pub fn build(self) -> Pair<$ty_param_1, $ty_param_2> + { + Pair { + relation: self.relation, + target: self.target + } + } + } +); + +impl Default for Builder<(), ()> +{ + fn default() -> Self + { + Self { relation: (), target: () } + } +} + #[derive(Debug)] pub struct Pair<Relation, Target> { @@ -28,14 +111,17 @@ pub struct Pair<Relation, Target> target: Target, } -impl Pair<Uid, Uid> +impl Pair<(), ()> { #[must_use] - pub fn new<Relation: Component>(target: Uid) -> Self + pub fn builder() -> Builder<(), ()> { - Self { relation: Relation::id(), target } + Builder { relation: (), target: () } } +} +impl Pair<Uid, Uid> +{ #[must_use] pub fn id(&self) -> Uid { @@ -46,20 +132,6 @@ impl Pair<Uid, Uid> } } -impl<Target> Pair<Uid, Target> -where - Target: Component, -{ - /// Returns a new pair that contains the target component as data. - pub fn new_with_comp_target<Relation: Component>(target_component: Target) -> Self - { - Self { - relation: Relation::uid(), - target: target_component, - } - } -} - impl IntoComponentParts for Pair<Uid, Uid> { fn into_parts(self) -> ComponentParts @@ -358,7 +430,13 @@ impl<'a, Relation: Component> MultipleWithWildcard<'a, Relation, Wildcard> world: self.world, component_ref: self .entity_handle - .get_matching_components(Pair::new::<Relation>(target_id).id()) + .get_matching_components( + Pair::builder() + .relation::<Relation>() + .target_id(target_id) + .build() + .id(), + ) .next()?, _pd: PhantomData, }) |