From 9f65dba3afd4e8f20881914fc86fa997cb64a13d Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 21 Feb 2024 23:54:37 +0100 Subject: feat(ecs): add support for system local components --- ecs/src/system/stateful.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 ecs/src/system/stateful.rs (limited to 'ecs/src/system/stateful.rs') diff --git a/ecs/src/system/stateful.rs b/ecs/src/system/stateful.rs new file mode 100644 index 0000000..b641cf2 --- /dev/null +++ b/ecs/src/system/stateful.rs @@ -0,0 +1,67 @@ +use std::marker::PhantomData; + +use crate::component::Local; +use crate::system::{System, TypeErased}; +use crate::{ComponentStorage, Query}; + +/// A stateful system. +pub struct Stateful +{ + func: Func, + local_component: Option, + _comps_pd: PhantomData, +} + +impl + System, Local)> + for Stateful +where + Func: Fn(Query, Local), +{ + type Input = LocalComponent; + type Query<'a> = Query<'a, Comps>; + + fn initialize(mut self, input: Self::Input) -> Self + { + self.local_component = Some(input); + + self + } + + fn run(&mut self, component_storage: &mut ComponentStorage) + { + (self.func)( + Query::new(component_storage), + Local::new(self.local_component.as_mut().unwrap()), + ); + } + + fn into_type_erased(self) -> TypeErased + { + TypeErased { + data: Box::new(self), + func: Box::new(move |data, component_storage| { + let this = data.downcast_mut::().unwrap(); + + this.run(component_storage); + }), + } + } +} + +impl + crate::system::Into, Local)> for Func +where + Func: Fn(Query, Local), +{ + type System = Stateful; + + fn into_system(self) -> Self::System + { + Self::System { + func: self, + local_component: None, + _comps_pd: PhantomData, + } + } +} -- cgit v1.2.3-18-g5258