diff options
author | HampusM <hampus@hampusmat.com> | 2024-02-26 20:05:27 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-02-26 20:16:27 +0100 |
commit | 815d04da602c58ed8b13eeb612fe73180204039d (patch) | |
tree | c3703e598656c070edbc271ccaa9117e071d6d4f /ecs/src/component.rs | |
parent | 1019924a29527eba2c8ec8bd976ece6ed76075b0 (diff) |
fix(ecs): make Component trait not automatic & add derive macro
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r-- | ecs/src/component.rs | 53 |
1 files changed, 20 insertions, 33 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 59b737e..70ce9ba 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -7,7 +7,7 @@ use seq_macro::seq; use crate::system::{Input as SystemInput, Param as SystemParam, System}; use crate::ComponentStorage; -pub trait Component: Any +pub trait Component: SystemInput + Any { #[doc(hidden)] fn as_any_mut(&mut self) -> &mut dyn Any; @@ -16,19 +16,6 @@ pub trait Component: Any fn as_any(&self) -> &dyn Any; } -impl<Value: Any> Component for Value -{ - fn as_any_mut(&mut self) -> &mut dyn Any - { - self - } - - fn as_any(&self) -> &dyn Any - { - self - } -} - impl dyn Component { pub fn downcast_mut<Real: 'static>(&mut self) -> Option<&mut Real> @@ -117,27 +104,27 @@ seq!(C in 0..=64 { /// Holds a component which is local to a single system. #[derive(Debug)] -pub struct Local<'world, Value: SystemInput> +pub struct Local<'world, LocalComponent: Component> { - value: &'world mut Value, + local_component: &'world mut LocalComponent, } -impl<'world, Value> Local<'world, Value> +impl<'world, LocalComponent> Local<'world, LocalComponent> where - Value: SystemInput, + LocalComponent: Component, { - fn new(value: &'world mut Value) -> Self + fn new(local_component: &'world mut LocalComponent) -> Self { - Self { value } + Self { local_component } } } -unsafe impl<'world, Value: 'static> SystemParam<'world> for Local<'world, Value> +unsafe impl<'world, LocalComponent> SystemParam<'world> for Local<'world, LocalComponent> where - Value: SystemInput, + LocalComponent: Component, { type Flags = (); - type Input = Value; + type Input = LocalComponent; fn initialize<SystemImpl>(system: &mut impl System<SystemImpl>, input: Self::Input) { @@ -150,7 +137,7 @@ where ) -> Self { let local_component = system - .get_local_component_mut::<Value>() + .get_local_component_mut::<LocalComponent>() .expect("Local component is uninitialized"); Self::new(local_component) @@ -164,33 +151,33 @@ where return true; }; - TypeId::of::<Value>() != *other_type_id + TypeId::of::<LocalComponent>() != *other_type_id } fn get_comparable() -> Box<dyn Any> { - Box::new(TypeId::of::<Value>()) + Box::new(TypeId::of::<LocalComponent>()) } } -impl<'world, Value> Deref for Local<'world, Value> +impl<'world, LocalComponent> Deref for Local<'world, LocalComponent> where - Value: SystemInput, + LocalComponent: Component, { - type Target = Value; + type Target = LocalComponent; fn deref(&self) -> &Self::Target { - self.value + self.local_component } } -impl<'world, Value> DerefMut for Local<'world, Value> +impl<'world, LocalComponent> DerefMut for Local<'world, LocalComponent> where - Value: SystemInput, + LocalComponent: Component, { fn deref_mut(&mut self) -> &mut Self::Target { - self.value + self.local_component } } |