

Add the following to your main.rs file:
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]Try to cut plugins by domain

pub struct InternalAudioPlugin;
// This plugin is responsible for controlling the game audio
impl Plugin for InternalAudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(AudioPlugin)
.add_systems(OnEnter(GameState::Playing), start_audio)
.add_systems(
Update,
control_audio
.after(set_movement_actions)
.run_if(in_state(GameState::Playing)),
);
}
}
#[derive(Resource)]
struct FlyingAudio(Handle<AudioInstance>);
fn start_audio(mut commands: Commands, audio_assets: Res<AudioAssets>, audio: Res<Audio>) { ... }
fn control_audio(actions: Res<Actions>, audio: Res<FlyingAudio>, mut instances: ResMut<Assets<AudioInstance>>) { ... }pub struct LoadingPlugin;
impl Plugin for LoadingPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_system_set(SystemSet::on_enter(GameState::Loading).with_system(start_loading.system()))
.add_system_set(SystemSet::on_update(GameState::Loading).with_system(check_state.system()));
}
}
struct LoadingState { handles: Vec<HandleUntyped> }
pub struct AudioAssets { pub flying: Handle<AudioSource> }
pub struct TextureAssets { pub texture_bevy: Handle<Texture> }
fn start_loading(mut commands: Commands, asset_server: Res<AssetServer>) {
let handles = vec![asset_server.load_untyped("flying.ogg"), asset_server.load_untyped("sprite.png")];
commands.insert_resource(LoadingState { handles });
}
fn check_state(mut commands: Commands, mut state: ResMut<State<GameState>>, server: Res<AssetServer>, loading_state: Res<LoadingState>) {
if LoadState::Loaded != server.get_group_load_state(loading_state.handles.iter().map(|handle| handle.id)) { return; }
commands.insert_resource(AudioAssets { flying: server.get_handle("flying.ogg") });
commands.insert_resource(TextureAssets { texture_bevy: server.get_handle("sprite.png") });
state.set(GameState::Menu).unwrap();
}Cutting "internal" plugins by domain makes it easy to move them to other crates

pub struct LoadingPlugin;
impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
app.add_loading_state(
LoadingState::new(GameState::Loading)
.continue_to_state(GameState::Menu)
.load_collection::<AudioAssets>()
.load_collection::<TextureAssets>(),
);
}
}
#[derive(AssetCollection, Resource)]
pub struct AudioAssets {
#[asset(path = "audio/flying.ogg")]
pub flying: Handle<AudioSource>,
}
#[derive(AssetCollection, Resource)]
pub struct TextureAssets {
#[asset(path = "textures/bevy.png")]
pub bevy: Handle<Image>,
#[asset(path = "textures/github.png")]
pub github: Handle<Image>,
}