From 815d04da602c58ed8b13eeb612fe73180204039d Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 26 Feb 2024 20:05:27 +0100 Subject: fix(ecs): make Component trait not automatic & add derive macro --- ecs/src/component.rs | 53 ++++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) (limited to 'ecs/src/component.rs') 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 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(&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(system: &mut impl System, input: Self::Input) { @@ -150,7 +137,7 @@ where ) -> Self { let local_component = system - .get_local_component_mut::() + .get_local_component_mut::() .expect("Local component is uninitialized"); Self::new(local_component) @@ -164,33 +151,33 @@ where return true; }; - TypeId::of::() != *other_type_id + TypeId::of::() != *other_type_id } fn get_comparable() -> Box { - Box::new(TypeId::of::()) + Box::new(TypeId::of::()) } } -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 } } -- cgit v1.2.3-18-g5258