use std::any::type_name; use std::convert::Infallible; use crate::component::{ Handle as ComponentHandle, HandleMut as ComponentHandleMut, IntoParts as IntoComponentParts, Parts as ComponentParts, }; use crate::entity::{ Handle as EntityHandle, MatchingComponentIter as EntityMatchingComponentIter, }; use crate::query::{ TermWithField as QueryTermWithField, TermsBuilder as QueryTermsBuilder, TermsBuilderInterface, }; use crate::uid::{PairParams as UidPairParams, Uid, With as WithUid}; use crate::{Component, World}; #[derive(Debug)] pub struct Pair { relation: Relation, target: Target, } impl Pair { #[must_use] pub fn new(target: Uid) -> Self { Self { relation: Relation::id(), target } } #[must_use] pub fn id(&self) -> Uid { Uid::new_pair(&UidPairParams { relation: self.relation, target: self.target, }) } } impl Pair where Target: Component, { /// Returns a new pair that contains the target component as data. pub fn new_with_comp_target(target_component: Target) -> Self { Self { relation: Relation::uid(), target: target_component, } } } impl IntoComponentParts for Pair { fn into_parts(self) -> ComponentParts { ComponentParts::builder().name("Pair").build(self.id(), ()) } } impl IntoComponentParts for Pair where Target: Component, { fn into_parts(self) -> ComponentParts { let id = Uid::new_pair(&UidPairParams { relation: self.relation, target: Target::id(), }); ComponentParts::builder() .name("Pair") .build(id, self.target) } } impl QueryTermWithField for Pair where Relation: Component, Target: Component, { type Field<'a> = ComponentHandle<'a, Target>; fn apply_to_terms_builder( terms_builder: &mut QueryTermsBuilder, ) { terms_builder.with_required([Self::uid()]); } fn get_field<'world>( entity_handle: &EntityHandle<'world>, _world: &'world World, ) -> Self::Field<'world> { let target_component = entity_handle .get_matching_components(Self::uid()) .next() .expect("Not possible"); Self::Field::from_entity_component_ref(target_component).unwrap_or_else(|err| { panic!( "Creating handle to target component {} failed: {err}", type_name::() ); }) } } impl QueryTermWithField for Pair where Relation: Component, Target: Component, { type Field<'a> = ComponentHandleMut<'a, Target>; fn apply_to_terms_builder( terms_builder: &mut QueryTermsBuilder, ) { terms_builder.with_required([Self::uid()]); } fn get_field<'world>( entity_handle: &EntityHandle<'world>, _world: &'world World, ) -> Self::Field<'world> { let target_component = entity_handle .get_matching_components(Self::uid()) .next() .expect("Not possible"); Self::Field::from_entity_component_ref(target_component).unwrap_or_else(|err| { panic!( "Creating handle to target component {} failed: {err}", type_name::() ); }) } } impl QueryTermWithField for Pair where Relation: Component, { type Field<'a> = EntityTargetHandle<'a>; fn apply_to_terms_builder( terms_builder: &mut QueryTermsBuilder, ) { terms_builder.with_required([Self::uid()]); } fn get_field<'world>( entity_handle: &EntityHandle<'world>, world: &'world World, ) -> Self::Field<'world> { let first_matching_comp = entity_handle .get_matching_components(Self::uid()) .next() .expect("Not possible"); EntityTargetHandle { world, pair_uid: first_matching_comp.id(), } } } impl WithUid for Pair where Relation: Component, Target: Component, { fn uid() -> Uid { Uid::new_pair(&UidPairParams { relation: Relation::uid(), target: Target::uid(), }) } } impl WithUid for Pair where Relation: Component, Target: Component, { fn uid() -> Uid { Uid::new_pair(&UidPairParams { relation: Relation::uid(), target: Target::uid(), }) } } impl WithUid for Pair where Relation: Component, { fn uid() -> Uid { Uid::new_pair(&UidPairParams { relation: Relation::uid(), target: Wildcard::uid(), }) } } impl QueryTermWithField for &'static [Pair] where Relation: Component, { type Field<'a> = HandleIter<'a>; fn apply_to_terms_builder( terms_builder: &mut QueryTermsBuilder, ) { terms_builder.with_required([Pair::::uid()]); } fn get_field<'world>( entity_handle: &EntityHandle<'world>, world: &'world World, ) -> Self::Field<'world> { HandleIter { inner: entity_handle .get_matching_components(Pair::::uid()), world, } } } pub struct EntityTargetHandle<'world> { world: &'world World, pair_uid: Uid, } impl EntityTargetHandle<'_> { #[must_use] pub fn get_target_entity(&self) -> Option> { let archetype = self .world .data .component_storage .get_entity_archetype(self.pair_uid.target_entity())?; let Some(archetype_entity) = archetype.get_entity_by_id(self.pair_uid.target_entity()) else { unreachable!(); }; Some(EntityHandle::new(archetype, archetype_entity)) } } pub struct HandleIter<'a> { inner: EntityMatchingComponentIter<'a>, world: &'a World, } impl<'a> Iterator for HandleIter<'a> { type Item = EntityTargetHandle<'a>; fn next(&mut self) -> Option { let matching_comp = self.inner.next()?; Some(EntityTargetHandle { world: self.world, pair_uid: matching_comp.id(), }) } } /// Relation denoting a dependency to another entity #[derive(Debug, Default, Clone, Copy, Component)] pub struct DependsOn; /// Relation denoting being the child of another entity. #[derive(Debug, Default, Clone, Copy, Component)] pub struct ChildOf; #[derive(Debug)] pub struct Wildcard(Infallible); impl Wildcard { pub fn uid() -> Uid { Uid::wildcard() } }