From b0ef7f2cf8787c5732e0eb5554161ad75179a4b3 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 20 Feb 2024 23:02:57 +0100 Subject: refactor(ecs): add system trait --- ecs/src/system.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ecs/src/system.rs (limited to 'ecs/src/system.rs') diff --git a/ecs/src/system.rs b/ecs/src/system.rs new file mode 100644 index 0000000..ecf1885 --- /dev/null +++ b/ecs/src/system.rs @@ -0,0 +1,64 @@ +use std::any::Any; +use std::fmt::Debug; + +use crate::component::Sequence as ComponentSequence; +use crate::{ComponentStorage, Query}; + +pub trait System: 'static +{ + type Query<'a>; + + fn run(&self, component_storage: &mut ComponentStorage); + + fn into_type_erased(self) -> TypeErased; +} + +impl System)> for Func +where + Func: Fn(Query) + 'static, + Comps: ComponentSequence, +{ + type Query<'a> = Query<'a, Comps>; + + fn run(&self, component_storage: &mut ComponentStorage) + { + self(Query::new(component_storage)); + } + + fn into_type_erased(self) -> TypeErased + { + TypeErased { + data: Box::new(self), + func: Box::new(|data, component_storage| { + let me = data.downcast_mut::().unwrap(); + + me.run(component_storage); + }), + } + } +} + +pub struct TypeErased +{ + data: Box, + func: Box, +} + +impl TypeErased +{ + pub fn run(&mut self, component_storage: &mut ComponentStorage) + { + (self.func)(self.data.as_mut(), component_storage); + } +} + +impl Debug for TypeErased +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + formatter.debug_struct("TypeErased").finish_non_exhaustive() + } +} + +/// Function in [`TypeErased`] used to run the system. +type TypeErasedFunc = dyn Fn(&mut dyn Any, &mut ComponentStorage); -- cgit v1.2.3-18-g5258