Skip to content

Commit

Permalink
remove conditional sub states
Browse files Browse the repository at this point in the history
Signed-off-by: lee-orr <lee-orr@users.noreply.github.com>
  • Loading branch information
lee-orr committed Oct 2, 2023
1 parent 00cd494 commit 9ce4545
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 98 deletions.
60 changes: 0 additions & 60 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,66 +360,6 @@ impl App {
self
}

/// Sets up a state `S: States` that will only exist when the `Parent: States` state's matches
/// the provided condition. Creates an instance of [`apply_state_transition::<S>`] in
/// [`StateTransition`] so that transitions happen before [`Update`](crate::Update), as
/// well as instances of [`initialize_state_and_enter::<S>`] and [`remove_state_from_world::<S>`]
/// to set up and run [`OnEnter`] and [`OnExit`] schedules for `S` when appropriate.
///
/// Whenever the parent enters a matching state, `S` will be set to `S::default()`.
///
/// In addition, adds an instance of [`apply_state_transition::<S>`] in
/// [`StateTransition`] so that transitions happen before [`Update`](crate::Update).
///
/// You can treat these just like any other states, but bear in mind that the `State<S>` and `NextState<S>`
/// resources might not exist.
pub fn add_sub_state<S: States, Parent: States>(
&mut self,
condition: impl StateMatcher<Parent>,
) -> &mut Self {
self.add_systems(
Parent::on_enter_matching(condition.clone()),
initialize_state_and_enter::<S>,
)
.add_systems(
Parent::on_exit_matching(condition.clone()),
remove_state_from_world::<S>,
)
.add_systems(
StateTransition,
apply_state_transition::<S>.run_if(in_state(condition)),
);

self
}

/// Adds a [`State<S>`] and [`NextState<S>`] whenever entering a matching `Parent` state
/// regardless of whether the previous `Parent` state matches, and runs OnEnter/OnExit when those occur.
///
/// In addition, adds an instance of [`apply_state_transition::<S>`] in
/// [`StateTransition`] so that transitions happen before [`Update`](crate::Update).
///
/// You can treat these just like any other states, but bear in mind that the `State<S>` and `NextState<S>`
/// resources might not exist.
pub fn add_strict_sub_state<S: States, Parent: States>(
&mut self,
condition: impl StateMatcher<Parent>,
) -> &mut Self {
self.add_systems(
Parent::on_enter_matching_strict(condition.clone()),
initialize_state_and_enter::<S>,
)
.add_systems(
Parent::on_enter_matching_strict(condition.clone()),
remove_state_from_world::<S>,
)
.add_systems(
StateTransition,
apply_state_transition::<S>.run_if(in_state(condition)),
);
self
}

/// Adds a system to the given schedule in this app's [`Schedules`].
///
/// # Examples
Expand Down
38 changes: 0 additions & 38 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,44 +464,6 @@ impl<S: States> NextState<S> {
}
}

/// If the state doesn't exist, initializes it to default runs `OnEnter`
pub fn initialize_state_and_enter<S: States>(world: &mut World) {
world.insert_resource(NextState::<S>::Keep);

if world.contains_resource::<State<S>>() {
return;
}

let default = S::default();

world.insert_resource(State(default.clone()));

world.try_run_schedule(OnEnter(default)).ok();
world
.try_run_schedule(OnStateEntry::<S>(PhantomData::<S>))
.ok();
}

/// If the state exists, removes it and runs `OnExit`
pub fn remove_state_from_world<S: States>(world: &mut World) {
world.remove_resource::<NextState<S>>();

let Some(state) = world.get_resource::<State<S>>() else {
return;
};
let state = state.0.clone();

world.remove_resource::<State<S>>();

world.insert_resource(PreviousState(state.clone()));

world.try_run_schedule(OnExit(state)).ok();
world
.try_run_schedule(OnStateExit::<S>(PhantomData::<S>))
.ok();
world.remove_resource::<PreviousState<S>>();
}

/// Run the enter schedule (if it exists) for the current state.
pub fn run_enter_schedule<S: States>(world: &mut World) {
let Some(state) = world.get_resource::<State<S>>().map(|s| s.0.clone()) else {
Expand Down

0 comments on commit 9ce4545

Please sign in to comment.