use std::any::type_name; use std::convert::Infallible; use crate::component::{ Handle as ComponentHandle, HandleError as ComponentHandleError, 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, EntityComponentRef, 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> = WildcardTargetHandle<'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"); WildcardTargetHandle { world, component_ref: first_matching_comp, } } } 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> = WildcardTargetIter<'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> { WildcardTargetIter { inner: entity_handle .get_matching_components(Pair::::uid()), world, } } } pub struct WildcardTargetHandle<'world> { world: &'world World, component_ref: EntityComponentRef<'world>, } impl WildcardTargetHandle<'_> { /// Attempts to retrieve the target as a entity, returning `None` if not found. #[must_use] pub fn get_entity(&self) -> Option> { let archetype = self .world .data .component_storage .get_entity_archetype(self.component_ref.id().target_entity())?; let Some(archetype_entity) = archetype.get_entity_by_id(self.component_ref.id().target_entity()) else { unreachable!(); }; Some(EntityHandle::new(archetype, archetype_entity)) } /// Attempts to retrieve the target as a component, returning `None` if the component /// type is incorrect. /// /// # Panics /// Will panic if: /// - The component is mutably borrowed elsewhere pub fn get_component( &self, ) -> Option> where ComponentData: 'static, { ComponentHandle::::from_entity_component_ref(&self.component_ref) .map_or_else( |err| match err { ComponentHandleError::IncorrectType => None, err @ ComponentHandleError::AcquireLockFailed(_) => { panic!( "Creating handle to component {} failed: {err}", type_name::() ); } }, |handle| Some(handle), ) } /// Attempts to retrieve the target as a component, returning `None` if the component /// type is incorrect. /// /// # Panics /// Will panic if: /// - The component is borrowed elsewhere pub fn get_component_mut( &self, ) -> Option> where ComponentData: 'static, { ComponentHandleMut::::from_entity_component_ref( &self.component_ref, ) .map_or_else( |err| match err { ComponentHandleError::IncorrectType => None, err @ ComponentHandleError::AcquireLockFailed(_) => { panic!( "Creating handle to component {} failed: {err}", type_name::() ); } }, |handle| Some(handle), ) } } pub struct WildcardTargetIter<'a> { inner: EntityMatchingComponentIter<'a>, world: &'a World, } impl<'a> Iterator for WildcardTargetIter<'a> { type Item = WildcardTargetHandle<'a>; fn next(&mut self) -> Option { let matching_comp = self.inner.next()?; Some(WildcardTargetHandle { world: self.world, component_ref: matching_comp, }) } } /// 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() } }