You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
martian-chess/src/loading.rs

113 lines
3.5 KiB
Rust

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::<Loading>)
.add_systems(OnExit(GameState::Loading), deactivate::<Loading>);
}
}
#[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<AssetServer>,
sprites: Res<Assets<Image>>,
gltfs: Res<Assets<Gltf>>,
tweakfile: Res<Assets<audio::AudioSettings>>,
mut next_state: ResMut<NextState<GameState>>,
) {
let s_ids = sprites.ids().collect::<Vec<AssetId<Image>>>();
let g_ids = gltfs.ids().collect::<Vec<AssetId<Gltf>>>();
let a_ids = tweakfile
.ids()
.collect::<Vec<AssetId<audio::AudioSettings>>>();
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)
}
}
}