From daf0bc236df25c0e9f44bc3e30839c16cda3f638 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 11 Nov 2024 00:11:22 +0100 Subject: refactor(ecs): use same ID for entities & components --- ecs/src/component.rs | 89 +++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 47 deletions(-) (limited to 'ecs/src/component.rs') diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 0506346..e1f2858 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -1,8 +1,9 @@ -use std::any::{type_name, Any, TypeId}; +use std::any::{type_name, Any}; use std::fmt::Debug; use seq_macro::seq; +use crate::uid::Uid; use crate::lock::{ReadGuard, WriteGuard}; use crate::system::{ComponentRef, ComponentRefMut, Input as SystemInput}; use crate::type_name::TypeName; @@ -27,8 +28,13 @@ pub trait Component: SystemInput + Any + TypeName where Self: Sized; - /// Returns the ID of this component's type. - fn id(&self) -> Id; + /// Returns the ID of this component. + fn id() -> Uid + where + Self: Sized; + + /// The ID of the component `self`. Returns the same value as [`Component::id`]. + fn self_id(&self) -> Uid; #[doc(hidden)] fn as_any_mut(&mut self) -> &mut dyn Any; @@ -36,9 +42,19 @@ pub trait Component: SystemInput + Any + TypeName #[doc(hidden)] fn as_any(&self) -> &dyn Any; - fn is_optional(&self) -> bool + /// Whether the component `self` is optional. Returns the same value as + /// [`Component::is_optional`]. + fn self_is_optional(&self) -> IsOptional { - false + IsOptional::No + } + + /// Returns whether this component is optional. + fn is_optional() -> IsOptional + where + Self: Sized, + { + IsOptional::No } } @@ -84,9 +100,14 @@ where type Ref<'component> = Option>; type RefMut<'component> = Option>; - fn id(&self) -> Id + fn id() -> Uid + { + ComponentT::id() + } + + fn self_id(&self) -> Uid { - Id::of::() + Self::id() } fn as_any_mut(&mut self) -> &mut dyn Any @@ -99,9 +120,14 @@ where self } - fn is_optional(&self) -> bool + fn self_is_optional(&self) -> IsOptional + { + Self::is_optional() + } + + fn is_optional() -> IsOptional { - true + IsOptional::Yes } } @@ -117,24 +143,6 @@ where impl SystemInput for Option where ComponentT: Component {} -/// The ID of a [`Component`] type. -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Id -{ - inner: TypeId, -} - -impl Id -{ - #[must_use] - pub fn of() -> Self - where - ComponentT: Component, - { - Self { inner: TypeId::of::() } - } -} - /// A sequence of components. pub trait Sequence { @@ -172,7 +180,7 @@ pub trait Sequence #[non_exhaustive] pub struct Metadata { - pub id: Id, + pub id: Uid, pub is_optional: IsOptional, } @@ -181,8 +189,8 @@ impl Metadata pub fn of(component: &ComponentT) -> Self { Self { - id: component.id(), - is_optional: component.is_optional().into(), + id: component.self_id(), + is_optional: component.self_is_optional(), } } } @@ -207,19 +215,6 @@ impl From for IsOptional } } -/// Returns whether the given component type is a optional component. -/// -/// Will return `true` if the component is a [`Option`]. -#[must_use] -pub fn is_optional() -> bool -{ - if Id::of::() == Id::of::>() { - return true; - } - - false -} - pub trait FromOptionalMut<'comp> { fn from_optional_mut_component( @@ -260,8 +255,8 @@ macro_rules! inner { vec![ #( Metadata { - id: Id::of::(), - is_optional: is_optional::().into() + id: Comp~I::id(), + is_optional: Comp~I::is_optional() }, )* ] @@ -281,7 +276,7 @@ macro_rules! inner { for comp in components { #( - if comp.id == Id::of::() { + if comp.id == Comp~I::Component::id() { comp_~I = Some(lock_component(comp)); continue; } @@ -308,7 +303,7 @@ macro_rules! inner { for comp in components { #( - if comp.id == Id::of::() { + if comp.id == Comp~I::Component::id() { comp_~I = Some(lock_component(comp)); continue; } -- cgit v1.2.3-18-g5258