use bevy_ecs::{system::Commands, world::World};1use log::debug;23use crate::state::{FreelyMutableState, NextState};45/// Extension trait for [`Commands`] adding `bevy_state` helpers.6pub trait CommandsStatesExt {7/// Sets the next state the app should move to.8///9/// Internally this schedules a command that updates the [`NextState<S>`](crate::prelude::NextState)10/// resource with `state`.11///12/// Note that commands introduce sync points to the ECS schedule, so modifying `NextState`13/// directly may be more efficient depending on your use-case.14fn set_state<S: FreelyMutableState>(&mut self, state: S);1516/// Sets the next state the app should move to, skipping any state transitions if the next state is the same as the current state.17///18/// Internally this schedules a command that updates the [`NextState<S>`](crate::prelude::NextState)19/// resource with `state`.20///21/// Note that commands introduce sync points to the ECS schedule, so modifying `NextState`22/// directly may be more efficient depending on your use-case.23fn set_state_if_neq<S: FreelyMutableState>(&mut self, state: S);24}2526impl CommandsStatesExt for Commands<'_, '_> {27fn set_state<S: FreelyMutableState>(&mut self, state: S) {28self.queue(move |w: &mut World| {29let mut next = w.resource_mut::<NextState<S>>();30if let NextState::PendingIfNeq(prev) = &*next {31debug!("overwriting next state {prev:?} with {state:?}");32}33next.set(state);34});35}3637fn set_state_if_neq<S: FreelyMutableState>(&mut self, state: S) {38self.queue(move |w: &mut World| {39let mut next = w.resource_mut::<NextState<S>>();40if let NextState::PendingIfNeq(prev) = &*next41&& *prev != state42{43debug!("overwriting next state {prev:?} with {state:?} if not equal");44}45next.set_if_neq(state);46});47}48}495051