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 fn name(&self) -> &'static str {
63 self.inner.name()
64 }
65}
66
67pub trait RunIfExt<Marker>: IntoSystem<Marker> + Sized {
70 fn run_if<C: RunCondition>(self) -> RunIfSystem<Self, Marker, C> {
71 RunIfSystem {
72 inner: self,
73 _marker: std::marker::PhantomData,
74 }
75 }
76}
77
78impl<Marker, T: IntoSystem<Marker>> RunIfExt<Marker> for T {}
79
80pub struct RunIfSystem<T, Marker, C> {
81 inner: T,
82 _marker: std::marker::PhantomData<(Marker, C)>,
83}
84
85impl<T, Marker, C> IntoSystem<Marker> for RunIfSystem<T, Marker, C>
86where
87 T: IntoSystem<Marker>,
88 C: RunCondition,
89{
90 type System = Conditional<T::System, C>;
91
92 fn into_system(self) -> Self::System {
93 Conditional {
94 inner: self.inner.into_system(),
95 _marker: std::marker::PhantomData,
96 }
97 }
98}
99
100pub trait SystemSetRunIfExt<M>: IntoSystemSet<M> + Sized {
104 fn run_if<C: RunCondition>(self) -> ConditionalSet<Self, M, C> {
105 ConditionalSet {
106 inner: self,
107 _marker: std::marker::PhantomData,
108 }
109 }
110}
111
112impl<M, T: IntoSystemSet<M>> SystemSetRunIfExt<M> for T {}
113
114pub struct ConditionalSet<T, M, C> {
115 inner: T,
116 _marker: std::marker::PhantomData<(M, C)>,
117}
118
119struct BoxedConditional<C> {
123 inner: Box<dyn System>,
124 _marker: std::marker::PhantomData<C>,
125}
126
127impl<C: RunCondition> System for BoxedConditional<C> {
128 fn run(&mut self, world: &hecs::World, resources: &Resources) {
129 if C::should_run(world, resources) {
130 self.inner.run(world, resources);
131 }
132 }
133
134 fn name(&self) -> &'static str {
136 self.inner.name()
137 }
138}
139
140impl<T, M, C> IntoSystemSet<M> for ConditionalSet<T, M, C>
141where
142 T: IntoSystemSet<M>,
143 C: RunCondition,
144{
145 fn into_system_set(self) -> Vec<Box<dyn System>> {
146 self.inner
147 .into_system_set()
148 .into_iter()
149 .map(|system| {
150 Box::new(BoxedConditional::<C> {
151 inner: system,
152 _marker: std::marker::PhantomData,
153 }) as Box<dyn System>
154 })
155 .collect()
156 }
157}