summaryrefslogtreecommitdiff
path: root/ecs/src/component.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r--ecs/src/component.rs37
1 files changed, 31 insertions, 6 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs
index 5c0b9ce..512c60d 100644
--- a/ecs/src/component.rs
+++ b/ecs/src/component.rs
@@ -15,7 +15,7 @@ pub(crate) mod storage;
pub trait Component: SystemInput + Any + TypeName
{
/// The component type in question. Will usually be `Self`
- type Component
+ type Component: Component
where
Self: Sized;
@@ -23,6 +23,9 @@ pub trait Component: SystemInput + Any + TypeName
where
Self: Sized;
+ /// Returns the ID of this component's type.
+ fn id(&self) -> Id;
+
#[doc(hidden)]
fn as_any_mut(&mut self) -> &mut dyn Any;
@@ -76,6 +79,11 @@ where
type Component = ComponentT;
type RefMut<'component> = Option<ComponentRefMut<'component, ComponentT>>;
+ fn id(&self) -> Id
+ {
+ Id::of::<Self>()
+ }
+
fn as_any_mut(&mut self) -> &mut dyn Any
{
self
@@ -104,6 +112,23 @@ 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
+{
+ pub fn of<ComponentT>() -> Self
+ where
+ ComponentT: Component,
+ {
+ Self { inner: TypeId::of::<ComponentT>() }
+ }
+}
+
/// A sequence of components.
pub trait Sequence
{
@@ -113,7 +138,7 @@ pub trait Sequence
fn into_vec(self) -> Vec<Box<dyn Component>>;
- fn type_ids() -> Vec<(TypeId, IsOptional)>;
+ fn ids() -> Vec<(Id, IsOptional)>;
fn from_components<'component>(
components: impl Iterator<Item = &'component EntityComponent>,
@@ -145,7 +170,7 @@ impl From<bool> for IsOptional
/// Will return `true` if the component is a [`Option`].
pub fn is_optional<ComponentT: Component>() -> bool
{
- if TypeId::of::<ComponentT>() == TypeId::of::<Option<ComponentT::Component>>() {
+ if Id::of::<ComponentT>() == Id::of::<Option<ComponentT::Component>>() {
return true;
}
@@ -174,11 +199,11 @@ macro_rules! inner {
Vec::from_iter([#(Box::new(self.I) as Box<dyn Component>,)*])
}
- fn type_ids() -> Vec<(TypeId, IsOptional)>
+ fn ids() -> Vec<(Id, IsOptional)>
{
vec![
#(
- (TypeId::of::<Comp~I>(), is_optional::<Comp~I>().into()),
+ (Id::of::<Comp~I>(), is_optional::<Comp~I>().into()),
)*
]
}
@@ -193,7 +218,7 @@ macro_rules! inner {
for comp in components {
#(
- if comp.id == TypeId::of::<Comp~I::Component>() {
+ if comp.id == Id::of::<Comp~I::Component>() {
comp_~I = Some(lock_component(comp));
continue;
}