use crate::world::World;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct State<S: Clone + PartialEq + Eq + Send + Sync + 'static> {
current: S,
next: Option<S>,
}
impl<S: Clone + PartialEq + Eq + Send + Sync + 'static> State<S> {
pub fn new(initial: S) -> Self {
Self {
current: initial,
next: None,
}
}
pub fn get(&self) -> &S {
&self.current
}
pub fn set(&mut self, state: S) {
if self.current != state {
self.next = Some(state);
}
}
pub fn apply_transitions(&mut self) -> bool {
if let Some(next) = self.next.take() {
self.current = next;
true
} else {
false
}
}
}
pub fn in_state<S>(state: S) -> impl FnMut(&World) -> bool + Send + Sync + 'static
where
S: Clone + PartialEq + Eq + Send + Sync + 'static,
{
move |world: &World| {
if let Some(current_state) = world.get_resource::<State<S>>() {
*current_state.get() == state
} else {
false
}
}
}