diff options
Diffstat (limited to 'ecs/src/system')
-rw-r--r-- | ecs/src/system/stateful.rs | 67 |
1 files changed, 67 insertions, 0 deletions
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, LocalComponent, Comps> +{ + func: Func, + local_component: Option<LocalComponent>, + _comps_pd: PhantomData<Comps>, +} + +impl<Func: 'static, Comps: 'static, LocalComponent: 'static> + System<fn(Query<Comps>, Local<LocalComponent>)> + for Stateful<Func, LocalComponent, Comps> +where + Func: Fn(Query<Comps>, Local<LocalComponent>), +{ + 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::<Self>().unwrap(); + + this.run(component_storage); + }), + } + } +} + +impl<Func: 'static, Comps, LocalComponent: 'static> + crate::system::Into<fn(Query<Comps>, Local<LocalComponent>)> for Func +where + Func: Fn(Query<Comps>, Local<LocalComponent>), +{ + type System = Stateful<Func, LocalComponent, Comps>; + + fn into_system(self) -> Self::System + { + Self::System { + func: self, + local_component: None, + _comps_pd: PhantomData, + } + } +} |