Make state private and only accessible through getter for State resource (#8009)
# Objective State requires a kind of awkward `state.0` to get the current state and exposes the field directly to manipulation. ## Solution Make it accessible through a getter method as well as privatize the field to make sure false assumptions about setting the state aren't made. ## Migration Guide - Use `State::get` instead of accessing the tuple field directly.
This commit is contained in:
parent
5fe7a403dc
commit
ed50c8b4d9
@ -685,14 +685,14 @@ pub mod common_conditions {
|
||||
/// app.run(&mut world);
|
||||
/// assert_eq!(world.resource::<Counter>().0, 1);
|
||||
///
|
||||
/// *world.resource_mut::<State<GameState>>() = State(GameState::Paused);
|
||||
/// *world.resource_mut::<State<GameState>>() = State::new(GameState::Paused);
|
||||
///
|
||||
/// // Now that we are in `GameState::Pause`, `pause_system` will run
|
||||
/// app.run(&mut world);
|
||||
/// assert_eq!(world.resource::<Counter>().0, 0);
|
||||
/// ```
|
||||
pub fn in_state<S: States>(state: S) -> impl FnMut(Res<State<S>>) -> bool + Clone {
|
||||
move |current_state: Res<State<S>>| current_state.0 == state
|
||||
move |current_state: Res<State<S>>| *current_state == state
|
||||
}
|
||||
|
||||
/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
|
||||
@ -741,7 +741,7 @@ pub mod common_conditions {
|
||||
/// app.run(&mut world);
|
||||
/// assert_eq!(world.resource::<Counter>().0, 1);
|
||||
///
|
||||
/// *world.resource_mut::<State<GameState>>() = State(GameState::Paused);
|
||||
/// *world.resource_mut::<State<GameState>>() = State::new(GameState::Paused);
|
||||
///
|
||||
/// // Now that we are in `GameState::Pause`, `pause_system` will run
|
||||
/// app.run(&mut world);
|
||||
@ -751,7 +751,7 @@ pub mod common_conditions {
|
||||
state: S,
|
||||
) -> impl FnMut(Option<Res<State<S>>>) -> bool + Clone {
|
||||
move |current_state: Option<Res<State<S>>>| match current_state {
|
||||
Some(current_state) => current_state.0 == state,
|
||||
Some(current_state) => *current_state == state,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
@ -803,7 +803,7 @@ pub mod common_conditions {
|
||||
/// app.run(&mut world);
|
||||
/// assert_eq!(world.resource::<Counter>().0, 1);
|
||||
///
|
||||
/// *world.resource_mut::<State<GameState>>() = State(GameState::Paused);
|
||||
/// *world.resource_mut::<State<GameState>>() = State::new(GameState::Paused);
|
||||
///
|
||||
/// // Now that `GameState` has been updated `my_system` will run
|
||||
/// app.run(&mut world);
|
||||
|
||||
@ -73,7 +73,27 @@ pub struct OnTransition<S: States> {
|
||||
///
|
||||
/// The starting state is defined via the [`Default`] implementation for `S`.
|
||||
#[derive(Resource, Default, Debug)]
|
||||
pub struct State<S: States>(pub S);
|
||||
pub struct State<S: States>(S);
|
||||
|
||||
impl<S: States> State<S> {
|
||||
/// Creates a new state with a specific value.
|
||||
///
|
||||
/// To change the state use [`NextState<S>`] rather than using this to modify the `State<S>`.
|
||||
pub fn new(state: S) -> Self {
|
||||
Self(state)
|
||||
}
|
||||
|
||||
/// Get the current state.
|
||||
pub fn get(&self) -> &S {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: States> PartialEq<S> for State<S> {
|
||||
fn eq(&self, other: &S) -> bool {
|
||||
self.get() == other
|
||||
}
|
||||
}
|
||||
|
||||
/// The next state of [`State<S>`].
|
||||
///
|
||||
|
||||
Loading…
Reference in New Issue
Block a user