use bevy::asset::{DependencyLoadState, RecursiveDependencyLoadState}; use crate::prelude::*; pub(crate) struct LoadingPlugin; impl Plugin for LoadingPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, initialize) .add_systems(Update, loading.run_if(in_state(GameState::Loading))) .add_systems(OnEnter(GameState::Loading), activate::) .add_systems(OnExit(GameState::Loading), deactivate::); } } #[derive(Debug, Component)] struct Loading; fn initialize(mut commands: Commands) { commands .spawn(( Loading, NodeBundle { style: Style { width: Val::Percent(100.0), height: Val::Percent(100.0), justify_content: JustifyContent::Center, align_items: AlignItems::Center, position_type: PositionType::Absolute, ..default() }, visibility: Visibility::Hidden, ..default() }, )) .with_children(|parent| { parent.spawn((TextBundle { text: Text { alignment: TextAlignment::Center, sections: vec![TextSection { value: "l o a d i n g . . .".into(), style: TextStyle { color: Color::WHITE.into(), ..default() }, }], ..default() }, background_color: Color::BLACK.with_a(0.5).into(), ..default() },)); }); } fn loading( server: Res, sprites: Res>, gltfs: Res>, tweakfile: Res>, mut next_state: ResMut>, ) { let s_ids = sprites.ids().collect::>>(); let g_ids = gltfs.ids().collect::>>(); let a_ids = tweakfile .ids() .collect::>>(); debug!( "Sprite len: {:?} | GLTF len: {:?} | Audio Tweakfile: {:?}", s_ids.len(), g_ids.len(), a_ids.len(), ); if s_ids.len() > 0 && g_ids.len() > 0 { let s_ready = s_ids.iter().all(|&id| { let load_state = server.get_load_states(id); load_state == Some(( LoadState::Loaded, DependencyLoadState::Loaded, RecursiveDependencyLoadState::Loaded, )) || load_state == None }); let g_ready = g_ids.iter().all(|&id| { let load_state = server.get_load_states(id); load_state == Some(( LoadState::Loaded, DependencyLoadState::Loaded, RecursiveDependencyLoadState::Loaded, )) || load_state == None }); let a_ready = a_ids.iter().all(|&id| { let load_state = server.get_load_states(id); load_state == Some(( LoadState::Loaded, DependencyLoadState::Loaded, RecursiveDependencyLoadState::Loaded, )) || load_state == None }); if s_ready && g_ready && a_ready { next_state.set(GameState::Menu) } } }