|
| 1 | +pub use common_conditions::*; |
| 2 | + |
| 3 | +use crate::system::BoxedSystem; |
| 4 | + |
| 5 | +pub type BoxedCondition = BoxedSystem<(), bool>; |
| 6 | + |
| 7 | +/// A system that determines if one or more scheduled systems should run. |
| 8 | +/// |
| 9 | +/// Implemented for functions and closures that convert into [`System<In=(), Out=bool>`](crate::system::System) |
| 10 | +/// with [read-only](crate::system::ReadOnlySystemParam) parameters. |
| 11 | +pub trait Condition<Params>: sealed::Condition<Params> {} |
| 12 | + |
| 13 | +impl<Params, F> Condition<Params> for F where F: sealed::Condition<Params> {} |
| 14 | + |
| 15 | +mod sealed { |
| 16 | + use crate::system::{IntoSystem, IsFunctionSystem, ReadOnlySystemParam, SystemParamFunction}; |
| 17 | + |
| 18 | + pub trait Condition<Params>: IntoSystem<(), bool, Params> {} |
| 19 | + |
| 20 | + impl<Params, Marker, F> Condition<(IsFunctionSystem, Params, Marker)> for F |
| 21 | + where |
| 22 | + F: SystemParamFunction<(), bool, Params, Marker> + Send + Sync + 'static, |
| 23 | + Params: ReadOnlySystemParam + 'static, |
| 24 | + Marker: 'static, |
| 25 | + { |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +mod common_conditions { |
| 30 | + use crate::schedule_v3::{State, States}; |
| 31 | + use crate::system::{Res, Resource}; |
| 32 | + |
| 33 | + /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` |
| 34 | + /// if the resource exists. |
| 35 | + pub fn resource_exists<T>() -> impl FnMut(Option<Res<T>>) -> bool |
| 36 | + where |
| 37 | + T: Resource, |
| 38 | + { |
| 39 | + move |res: Option<Res<T>>| res.is_some() |
| 40 | + } |
| 41 | + |
| 42 | + /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` |
| 43 | + /// if the resource is equal to `value`. |
| 44 | + /// |
| 45 | + /// # Panics |
| 46 | + /// |
| 47 | + /// The condition will panic if the resource does not exist. |
| 48 | + pub fn resource_equals<T>(value: T) -> impl FnMut(Res<T>) -> bool |
| 49 | + where |
| 50 | + T: Resource + PartialEq, |
| 51 | + { |
| 52 | + move |res: Res<T>| *res == value |
| 53 | + } |
| 54 | + |
| 55 | + /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` |
| 56 | + /// if the resource exists and is equal to `value`. |
| 57 | + /// |
| 58 | + /// The condition will return `false` if the resource does not exist. |
| 59 | + pub fn resource_exists_and_equals<T>(value: T) -> impl FnMut(Option<Res<T>>) -> bool |
| 60 | + where |
| 61 | + T: Resource + PartialEq, |
| 62 | + { |
| 63 | + move |res: Option<Res<T>>| match res { |
| 64 | + Some(res) => *res == value, |
| 65 | + None => false, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` |
| 70 | + /// if the state machine exists. |
| 71 | + pub fn state_exists<S: States>() -> impl FnMut(Option<Res<State<S>>>) -> bool { |
| 72 | + move |current_state: Option<Res<State<S>>>| current_state.is_some() |
| 73 | + } |
| 74 | + |
| 75 | + /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` |
| 76 | + /// if the state machine is currently in `state`. |
| 77 | + /// |
| 78 | + /// # Panics |
| 79 | + /// |
| 80 | + /// The condition will panic if the resource does not exist. |
| 81 | + pub fn state_equals<S: States>(state: S) -> impl FnMut(Res<State<S>>) -> bool { |
| 82 | + move |current_state: Res<State<S>>| current_state.0 == state |
| 83 | + } |
| 84 | + |
| 85 | + /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` |
| 86 | + /// if the state machine exists and is currently in `state`. |
| 87 | + /// |
| 88 | + /// The condition will return `false` if the state does not exist. |
| 89 | + pub fn state_exists_and_equals<S: States>( |
| 90 | + state: S, |
| 91 | + ) -> impl FnMut(Option<Res<State<S>>>) -> bool { |
| 92 | + move |current_state: Option<Res<State<S>>>| match current_state { |
| 93 | + Some(current_state) => current_state.0 == state, |
| 94 | + None => false, |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments