diff options
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r-- | ecs/src/component.rs | 89 |
1 files changed, 42 insertions, 47 deletions
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<ComponentRef<'component, ComponentT>>; type RefMut<'component> = Option<ComponentRefMut<'component, ComponentT>>; - fn id(&self) -> Id + fn id() -> Uid + { + ComponentT::id() + } + + fn self_id(&self) -> Uid { - Id::of::<Self>() + 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<ComponentT> SystemInput for Option<ComponentT> 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<ComponentT>() -> Self - where - ComponentT: Component, - { - Self { inner: TypeId::of::<ComponentT>() } - } -} - /// 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<ComponentT: Component + ?Sized>(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<bool> 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<ComponentT: Component>() -> bool -{ - if Id::of::<ComponentT>() == Id::of::<Option<ComponentT::Component>>() { - return true; - } - - false -} - pub trait FromOptionalMut<'comp> { fn from_optional_mut_component( @@ -260,8 +255,8 @@ macro_rules! inner { vec![ #( Metadata { - id: Id::of::<Comp~I>(), - is_optional: is_optional::<Comp~I>().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::<Comp~I::Component>() { + 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::<Comp~I::Component>() { + if comp.id == Comp~I::Component::id() { comp_~I = Some(lock_component(comp)); continue; } |