pebble/ecs/
system_condition.rs1use crate::ecs::{
2 resources::Resources,
3 system::{IntoSystem, System},
4 system_set::IntoSystemSet,
5};
6
7pub trait RunCondition: 'static {
16 fn should_run(world: &hecs::World, resources: &Resources) -> bool;
17}
18
19pub struct ResourceExists<T>(std::marker::PhantomData<T>);
21impl<T: 'static + Send + Sync> RunCondition for ResourceExists<T> {
22 fn should_run(world: &hecs::World, resources: &Resources) -> bool {
23 resources.has_resource::<T>(world)
24 }
25}
26
27pub struct And<A, B>(std::marker::PhantomData<(A, B)>);
29impl<A: RunCondition, B: RunCondition> RunCondition for And<A, B> {
30 fn should_run(world: &hecs::World, resources: &Resources) -> bool {
31 A::should_run(world, resources) && B::should_run(world, resources)
32 }
33}
34
35pub struct Or<A, B>(std::marker::PhantomData<(A, B)>);
37impl<A: RunCondition, B: RunCondition> RunCondition for Or<A, B> {
38 fn should_run(world: &hecs::World, resources: &Resources) -> bool {
39 A::should_run(world, resources) || B::should_run(world, resources)
40 }
41}
42
43pub struct Conditional<S, C> {
46 inner: S,
47 _marker: std::marker::PhantomData<C>,
48}
49
50impl<S: System, C: RunCondition> System for Conditional<S, C> {
51 fn run(&mut self, world: &hecs::World, resources: &Resources) {
52 if C::should_run(world, resources) {
53 self.inner.run(world, resources);
54 }
55 }
56}
57
58pub trait RunIfExt<Marker>: IntoSystem<Marker> + Sized {
61 fn run_if<C: RunCondition>(self) -> RunIfSystem<Self, Marker, C> {
62 RunIfSystem {
63 inner: self,
64 _marker: std::marker::PhantomData,
65 }
66 }
67}
68
69impl<Marker, T: IntoSystem<Marker>> RunIfExt<Marker> for T {}
70
71pub struct RunIfSystem<T, Marker, C> {
72 inner: T,
73 _marker: std::marker::PhantomData<(Marker, C)>,
74}
75
76impl<T, Marker, C> IntoSystem<Marker> for RunIfSystem<T, Marker, C>
77where
78 T: IntoSystem<Marker>,
79 C: RunCondition,
80{
81 type System = Conditional<T::System, C>;
82
83 fn into_system(self) -> Self::System {
84 Conditional {
85 inner: self.inner.into_system(),
86 _marker: std::marker::PhantomData,
87 }
88 }
89}
90
91pub trait SystemSetRunIfExt<M>: IntoSystemSet<M> + Sized {
95 fn run_if<C: RunCondition>(self) -> ConditionalSet<Self, M, C> {
96 ConditionalSet {
97 inner: self,
98 _marker: std::marker::PhantomData,
99 }
100 }
101}
102
103impl<M, T: IntoSystemSet<M>> SystemSetRunIfExt<M> for T {}
104
105pub struct ConditionalSet<T, M, C> {
106 inner: T,
107 _marker: std::marker::PhantomData<(M, C)>,
108}
109
110struct BoxedConditional<C> {
114 inner: Box<dyn System>,
115 _marker: std::marker::PhantomData<C>,
116}
117
118impl<C: RunCondition> System for BoxedConditional<C> {
119 fn run(&mut self, world: &hecs::World, resources: &Resources) {
120 if C::should_run(world, resources) {
121 self.inner.run(world, resources);
122 }
123 }
124}
125
126impl<T, M, C> IntoSystemSet<M> for ConditionalSet<T, M, C>
127where
128 T: IntoSystemSet<M>,
129 C: RunCondition,
130{
131 fn into_system_set(self) -> Vec<Box<dyn System>> {
132 self.inner
133 .into_system_set()
134 .into_iter()
135 .map(|system| {
136 Box::new(BoxedConditional::<C> {
137 inner: system,
138 _marker: std::marker::PhantomData,
139 }) as Box<dyn System>
140 })
141 .collect()
142 }
143}