From 136d0511972fd31a82331cf769f8d309cd3438f8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 11 Nov 2024 22:16:17 +0100 Subject: refactor(ecs): make Relationship use Component derive macro --- ecs/src/relationship.rs | 68 ++++++++----------------------------------------- 1 file changed, 11 insertions(+), 57 deletions(-) (limited to 'ecs') diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs index 259677b..0ebd9c2 100644 --- a/ecs/src/relationship.rs +++ b/ecs/src/relationship.rs @@ -1,6 +1,7 @@ -use std::any::{type_name, Any}; +use std::any::type_name; use std::marker::PhantomData; -use std::sync::LazyLock; + +use ecs_macros::Component; use crate::component::storage::Storage as ComponentStorage; use crate::component::{ @@ -9,14 +10,19 @@ use crate::component::{ FromOptionalMut as FromOptionalMutComponent, }; use crate::lock::ReadGuard; -use crate::system::{ComponentRef, ComponentRefMut, Input as SystemInput}; -use crate::type_name::TypeName; +use crate::system::{ComponentRef, ComponentRefMut}; use crate::uid::{Kind as UidKind, Uid}; use crate::World; /// A relationship to one or more targets. -#[derive(Debug)] +#[derive(Debug, Component)] +#[component( + ref_type = Relation<'component, Kind, ComponentT>, + ref_mut_type = RelationMut<'component, Kind, ComponentT>, +)] pub struct Relationship +where + Kind: 'static, { entity_uid: SingleOrMultiple, _pd: PhantomData<(Kind, ComponentT)>, @@ -55,58 +61,6 @@ where } } -static COMPONENT_EUID: LazyLock = - LazyLock::new(|| Uid::new_unique(UidKind::Component)); - -impl Component for Relationship -where - Kind: 'static, - ComponentT: Component, -{ - type Component = Self; - type Ref<'component> = Relation<'component, Kind, ComponentT>; - type RefMut<'component> = RelationMut<'component, Kind, ComponentT>; - - fn id() -> Uid - where - Self: Sized, - { - *COMPONENT_EUID - } - - fn self_id(&self) -> Uid - { - Self::id() - } - - fn as_any_mut(&mut self) -> &mut dyn Any - { - self - } - - fn as_any(&self) -> &dyn Any - { - self - } -} - -impl SystemInput for Relationship -where - Kind: 'static, - ComponentT: Component, -{ -} - -impl TypeName for Relationship -where - ComponentT: Component, -{ - fn type_name(&self) -> &'static str - { - type_name::() - } -} - pub struct RelationMut<'rel_comp, Kind, ComponentT> where Kind: 'static, -- cgit v1.2.3-18-g5258