Skip to content

Commit

Permalink
Refactor state management to use StateScoped instead of explicit clea…
Browse files Browse the repository at this point in the history
…nup (#7)
  • Loading branch information
ShenMian authored Jul 10, 2024
1 parent 3dbe165 commit 9d647ff
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 22 deletions.
7 changes: 1 addition & 6 deletions src/game_over.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@ use crate::{
settings::DifficultySetting,
stats::Stats,
ui::{UiAssets, BUTTON_TEXT, TITLE_TEXT},
util::cleanup,
GameState,
};

pub struct GameOverPlugin;
impl Plugin for GameOverPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::GameOver), init);
app.add_systems(OnExit(GameState::GameOver), cleanup::<GameOverScene>);
app.add_systems(Update, menu_button.run_if(in_state(GameState::GameOver)));
}
}

#[derive(Component)]
struct GameOverScene;

#[derive(Component)]
struct MenuButton;

Expand Down Expand Up @@ -67,7 +62,7 @@ fn init(
..default()
},
NineSliceUiTexture::from_image(ui_assets.nine_panel.clone()),
GameOverScene,
StateScoped(GameState::GameOver),
))
.id();

Expand Down
9 changes: 2 additions & 7 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
enemy::EnemyKind,
tilemap::{AtlasHandle, TileKind},
ui::UiAssets,
util::cleanup,
GameState,
};

Expand All @@ -34,14 +33,10 @@ impl Plugin for LoadingPlugin {
.add_systems(
Update,
log_pipelines.run_if(resource_changed::<PipelinesReady>),
)
.add_systems(OnExit(GameState::Loading), cleanup::<LoadingScene>);
);
}
}

#[derive(Component)]
struct LoadingScene;

#[derive(Component)]
pub struct LoadingImage {
frames: Vec<usize>,
Expand Down Expand Up @@ -105,7 +100,7 @@ fn init_loading_scene(
..default()
},
NineSliceUiTexture::from_image(ui_assets.nine_button.clone()),
LoadingScene,
StateScoped(GameState::Loading),
))
.with_children(|parent| {
parent.spawn(TextBundle {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ fn main() {

app.insert_resource(Msaa::Off)
.insert_resource(ClearColor(Color::BLACK))
.init_state::<GameState>();
.init_state::<GameState>()
.enable_state_scoped_entities::<GameState>();

app.run();
}
10 changes: 2 additions & 8 deletions src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ impl Plugin for MainMenuPlugin {
)
.run_if(in_state(GameState::MainMenu)),
)
.add_systems(
OnExit(GameState::MainMenu),
(crate::util::cleanup::<MainMenuScene>, cleanup_background),
);
.add_systems(OnExit(GameState::MainMenu), cleanup_background);
}
}

#[derive(Component)]
struct MainMenuScene;

#[derive(Resource)]
struct MainMenuAssets {
background: Handle<Map>,
Expand Down Expand Up @@ -122,7 +116,7 @@ fn setup_menu(
..default()
},
NineSliceUiTexture::from_image(ui_assets.nine_panel.clone()),
MainMenuScene,
StateScoped(GameState::MainMenu),
))
.id();

Expand Down

0 comments on commit 9d647ff

Please sign in to comment.