Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor state management to use StateScoped instead of explicit cleanup #7

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading