diff options
author | HampusM <hampus@hampusmat.com> | 2024-02-21 23:54:37 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-02-22 19:27:52 +0100 |
commit | 9f65dba3afd4e8f20881914fc86fa997cb64a13d (patch) | |
tree | d760f8f478af6591648ef3f53d4e0cb34ee76908 /ecs/src/system/stateful.rs | |
parent | b0ef7f2cf8787c5732e0eb5554161ad75179a4b3 (diff) |
feat(ecs): add support for system local components
Diffstat (limited to 'ecs/src/system/stateful.rs')
-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, + } + } +} |