Skip to content

Commit

Permalink
Move StateTransitionSteps registration to states plugin (#13939)
Browse files Browse the repository at this point in the history
# Objective

Fixes #13920

## Solution

As described in the issue.

## Testing

Moved a custom transition plugin in example before any of the app-state
methods.
  • Loading branch information
MiniaczQ authored and mockersf committed Jun 21, 2024
1 parent 47ad37e commit 783fc29
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_state/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl AppExtStates for SubApp {
fn init_state<S: FreelyMutableState + FromWorld>(&mut self) -> &mut Self {
warn_if_no_states_plugin_installed(self);
if !self.world().contains_resource::<State<S>>() {
setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern()));
self.init_resource::<State<S>>()
.init_resource::<NextState<S>>()
.add_event::<StateTransitionEvent<S>>();
Expand All @@ -90,7 +89,6 @@ impl AppExtStates for SubApp {
fn insert_state<S: FreelyMutableState>(&mut self, state: S) -> &mut Self {
warn_if_no_states_plugin_installed(self);
if !self.world().contains_resource::<State<S>>() {
setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern()));
self.insert_resource::<State<S>>(State::new(state.clone()))
.init_resource::<NextState<S>>()
.add_event::<StateTransitionEvent<S>>();
Expand Down Expand Up @@ -121,7 +119,6 @@ impl AppExtStates for SubApp {
.world()
.contains_resource::<Events<StateTransitionEvent<S>>>()
{
setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern()));
self.add_event::<StateTransitionEvent<S>>();
let schedule = self.get_schedule_mut(StateTransition).unwrap();
S::register_computed_state_systems(schedule);
Expand All @@ -147,7 +144,6 @@ impl AppExtStates for SubApp {
.world()
.contains_resource::<Events<StateTransitionEvent<S>>>()
{
setup_state_transitions_in_world(self.world_mut(), Some(Startup.intern()));
self.init_resource::<NextState<S>>();
self.add_event::<StateTransitionEvent<S>>();
let schedule = self.get_schedule_mut(StateTransition).unwrap();
Expand Down Expand Up @@ -219,13 +215,15 @@ impl Plugin for StatesPlugin {
fn build(&self, app: &mut App) {
let mut schedule = app.world_mut().resource_mut::<MainScheduleOrder>();
schedule.insert_after(PreUpdate, StateTransition);
setup_state_transitions_in_world(app.world_mut(), Some(Startup.intern()));
}
}

#[cfg(test)]
mod tests {
use crate::{
self as bevy_state,
app::StatesPlugin,
state::{State, StateTransition, StateTransitionEvent},
};
use bevy_app::App;
Expand All @@ -245,6 +243,7 @@ mod tests {
#[test]
fn insert_state_can_overwrite_init_state() {
let mut app = App::new();
app.add_plugins(StatesPlugin);

app.init_state::<TestState>();
app.insert_state(TestState::B);
Expand All @@ -264,6 +263,7 @@ mod tests {
#[test]
fn insert_state_can_overwrite_insert_state() {
let mut app = App::new();
app.add_plugins(StatesPlugin);

app.insert_state(TestState::B);
app.insert_state(TestState::C);
Expand Down
9 changes: 7 additions & 2 deletions examples/state/custom_transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ enum AppState {

fn main() {
App::new()
.add_plugins(DefaultPlugins)
// We insert the custom transitions plugin for `AppState`.
.add_plugins((
DefaultPlugins,
IdentityTransitionsPlugin::<AppState>::default(),
))
.init_state::<AppState>()
.add_systems(Startup, setup)
.add_systems(OnEnter(AppState::Menu), setup_menu)
.add_systems(Update, menu.run_if(in_state(AppState::Menu)))
.add_systems(OnExit(AppState::Menu), cleanup_menu)
// We will restart the game progress every time we re-enter into it.
.add_plugins(IdentityTransitionsPlugin::<AppState>::default())
.add_systems(OnReenter(AppState::InGame), setup_game)
.add_systems(OnReexit(AppState::InGame), teardown_game)
// Doing it this way allows us to restart the game without any additional in-between states.
Expand Down Expand Up @@ -225,10 +228,12 @@ fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) {
texture: asset_server.load("branding/icon.png"),
..default()
});
info!("Setup game");
}

fn teardown_game(mut commands: Commands, player: Query<Entity, With<Sprite>>) {
commands.entity(player.single()).despawn();
info!("Teardown game");
}

#[derive(Resource)]
Expand Down

0 comments on commit 783fc29

Please sign in to comment.