1use crate::world::World;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct State<S: Clone + PartialEq + Eq + Send + Sync + 'static> {
6 current: S,
7 next: Option<S>,
8}
9
10impl<S: Clone + PartialEq + Eq + Send + Sync + 'static> State<S> {
11 pub fn new(initial: S) -> Self {
12 Self {
13 current: initial,
14 next: None,
15 }
16 }
17
18 pub fn get(&self) -> &S {
19 &self.current
20 }
21
22 pub fn set(&mut self, state: S) {
23 if self.current != state {
24 self.next = Some(state);
25 }
26 }
27
28 pub fn apply_transitions(&mut self) -> bool {
30 if let Some(next) = self.next.take() {
31 self.current = next;
32 true
33 } else {
34 false
35 }
36 }
37}
38
39pub fn in_state<S>(state: S) -> impl FnMut(&World) -> bool + Send + Sync + 'static
41where
42 S: Clone + PartialEq + Eq + Send + Sync + 'static,
43{
44 move |world: &World| {
45 if let Some(current_state) = world.get_resource::<State<S>>() {
46 *current_state.get() == state
47 } else {
48 false
49 }
50 }
51}