diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index c159355ce8801..1dee75edb9e5b 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -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::`] in - /// [`StateTransition`] so that transitions happen before [`Update`](crate::Update), as - /// well as instances of [`initialize_state_and_enter::`] and [`remove_state_from_world::`] - /// 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::`] 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` and `NextState` - /// resources might not exist. - pub fn add_sub_state( - &mut self, - condition: impl StateMatcher, - ) -> &mut Self { - self.add_systems( - Parent::on_enter_matching(condition.clone()), - initialize_state_and_enter::, - ) - .add_systems( - Parent::on_exit_matching(condition.clone()), - remove_state_from_world::, - ) - .add_systems( - StateTransition, - apply_state_transition::.run_if(in_state(condition)), - ); - - self - } - - /// Adds a [`State`] and [`NextState`] 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::`] 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` and `NextState` - /// resources might not exist. - pub fn add_strict_sub_state( - &mut self, - condition: impl StateMatcher, - ) -> &mut Self { - self.add_systems( - Parent::on_enter_matching_strict(condition.clone()), - initialize_state_and_enter::, - ) - .add_systems( - Parent::on_enter_matching_strict(condition.clone()), - remove_state_from_world::, - ) - .add_systems( - StateTransition, - apply_state_transition::.run_if(in_state(condition)), - ); - self - } - /// Adds a system to the given schedule in this app's [`Schedules`]. /// /// # Examples diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 0b70cb21ce8c6..5ef50d1e37d8a 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -464,44 +464,6 @@ impl NextState { } } -/// If the state doesn't exist, initializes it to default runs `OnEnter` -pub fn initialize_state_and_enter(world: &mut World) { - world.insert_resource(NextState::::Keep); - - if world.contains_resource::>() { - return; - } - - let default = S::default(); - - world.insert_resource(State(default.clone())); - - world.try_run_schedule(OnEnter(default)).ok(); - world - .try_run_schedule(OnStateEntry::(PhantomData::)) - .ok(); -} - -/// If the state exists, removes it and runs `OnExit` -pub fn remove_state_from_world(world: &mut World) { - world.remove_resource::>(); - - let Some(state) = world.get_resource::>() else { - return; - }; - let state = state.0.clone(); - - world.remove_resource::>(); - - world.insert_resource(PreviousState(state.clone())); - - world.try_run_schedule(OnExit(state)).ok(); - world - .try_run_schedule(OnStateExit::(PhantomData::)) - .ok(); - world.remove_resource::>(); -} - /// Run the enter schedule (if it exists) for the current state. pub fn run_enter_schedule(world: &mut World) { let Some(state) = world.get_resource::>().map(|s| s.0.clone()) else {