From 3f099cddf16f2e7e002cc24217ed3cc5da422156 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 7 Jun 2026 00:41:15 +0200 Subject: feat(engine-ecs): add component reflection support --- engine-ecs/src/component.rs | 60 ++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 22 deletions(-) (limited to 'engine-ecs/src/component.rs') diff --git a/engine-ecs/src/component.rs b/engine-ecs/src/component.rs index 17b279b..f881128 100644 --- a/engine-ecs/src/component.rs +++ b/engine-ecs/src/component.rs @@ -2,6 +2,7 @@ use std::any::{type_name, Any}; use std::fmt::Debug; use std::ops::{Deref, DerefMut}; +use engine_ecs_macros::Component; use seq_macro::seq; use crate::event::component::Changed; @@ -30,6 +31,10 @@ pub trait Component: SystemInput + Any where Self: Sized; + fn type_reflection() -> Option<&'static crate::reflection::Type> + where + Self: Sized; + /// Returns the name of this component. fn name(&self) -> &'static str; } @@ -243,6 +248,7 @@ where { Parts::builder() .name(type_name::()) + .type_reflection(Self::type_reflection()) .build(Self::id(), self) } } @@ -252,41 +258,26 @@ where #[non_exhaustive] pub struct Parts { - id: Uid, - name: &'static str, - data: Box, + pub id: Uid, + pub name: &'static str, + pub type_reflection: Option<&'static crate::reflection::Type>, + pub data: Box, } impl Parts { - #[must_use] - pub fn id(&self) -> Uid - { - self.id - } - - #[must_use] - pub fn name(&self) -> &'static str - { - self.name - } - #[must_use] pub fn builder() -> PartsBuilder { PartsBuilder::default() } - - pub(crate) fn into_data(self) -> Box - { - self.data - } } #[derive(Debug)] pub struct PartsBuilder { name: &'static str, + type_reflection: Option<&'static crate::reflection::Type>, } impl PartsBuilder @@ -298,12 +289,23 @@ impl PartsBuilder self } + #[must_use] + pub fn type_reflection( + mut self, + type_reflection: Option<&'static crate::reflection::Type>, + ) -> Self + { + self.type_reflection = type_reflection; + self + } + #[must_use] pub fn build(self, id: Uid, data: Data) -> Parts { Parts { id, name: self.name, + type_reflection: self.type_reflection, data: Box::new(data), } } @@ -311,7 +313,12 @@ impl PartsBuilder #[must_use] pub fn build_with_any_data(self, id: Uid, data: Box) -> Parts { - Parts { id, name: self.name, data } + Parts { + id, + name: self.name, + type_reflection: self.type_reflection, + data, + } } } @@ -319,6 +326,15 @@ impl Default for PartsBuilder { fn default() -> Self { - Self { name: "(unspecified)" } + Self { + name: "(unspecified)", + type_reflection: None, + } } } + +#[derive(Debug, Clone, Component)] +pub struct Type +{ + pub type_reflection: &'static crate::reflection::Type, +} -- cgit v1.2.3-18-g5258