Newer
Older
use bevy::app::Plugin;
use bevy::asset::{Handle, LoadState};
use bevy::prelude::*;
use bevy_kira_audio::AudioSource;
/// We store our asset handles in this to avoid Bevy from dropping the assets and reloading
/// when we switch tracks
pub struct AudioResources {
pub white_kitty: Handle<AudioSource>,
pub great_madeja: Handle<AudioSource>,
}
#[derive(Default, Eq, PartialEq, Debug, Clone, Hash, States)]
pub enum AppState {
#[default]
Loading,
Running,
}
pub fn load_resources(mut commands: Commands, assets: Res<AssetServer>) {
let white_kitty = assets.load("The-White-Kitty.mp3");
let great_madeja = assets.load("The-Great-Madeja.mp3");
commands.insert_resource(AudioResources {
white_kitty,
great_madeja,
})
}
pub fn check_load_state(
assets: Res<AssetServer>,
resources: Res<AudioResources>,
appstate: Res<State<AppState>>,
mut next_state: ResMut<NextState<AppState>>,
let load_state = assets.get_group_load_state(vec![
resources.white_kitty.id(),
resources.great_madeja.id(),
]);
log::info!("STATE {:?}", appstate);
*next_state = NextState(Some(AppState::Running)); // appstate.set(AppState::Running);
}
LoadState::Loading => {}
_ => {
log::error!("The resources are in a bad state! This is a problem");
}
}
}
// pub fn has_audio_resources(res: Option<Res<AudioResources>>) -> ShouldRun {
// res.is_some().into()
// }
// pub fn is_state_loading(state: Res<AppState>) -> ShouldRun {
// (*state == AppState::Loading).into()
// }
// pub fn is_state_running(state: Res<AppState>) -> ShouldRun {
// (*state == AppState::Running).into()
// }
/// This component allows us to easily grab the on screen text
#[derive(Component)]
pub struct TextMarker;
/// This component allows us to easily grab the blank details text area
#[derive(Component)]
pub struct DetailsMarker;
pub fn create_ui(mut commands: Commands, assets: Res<AssetServer>) {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
flex_direction: FlexDirection::Column,
..Default::default()
},
..Default::default()
})
.with_children(|children| {
children
text: Text::from_section(
"Loading Audio Tracks",
TextStyle {
color: Color::BLACK,
font_size: 48.0,
font: assets.load("KenneyBlocks.ttf"),
},
),
..Default::default()
})
.insert(TextMarker);
children
text: Text::from_section(
"...",
TextStyle {
color: Color::BLACK,
font_size: 32.0,
font: assets.load("KenneyBlocks.ttf"),
},
),
..Default::default()
})
.insert(DetailsMarker);
});
}
pub struct SetupPlugin;
impl Plugin for SetupPlugin {
fn build(&self, app: &mut App) {
.add_startup_system(load_resources)
.add_startup_system(create_ui)
.add_system(
check_load_state
.run_if(resource_exists::<AudioResources>())
.run_if(in_state(AppState::Loading)),