Skip to content
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
273 changes: 256 additions & 17 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ debugdump = ["bevy_mod_debugdump"]
[dependencies]
bevy = { version = "0.16", default-features = false, features = [
"bevy_asset",
"bevy_audio",
"bevy_core_pipeline",
"bevy_render",
"bevy_sprite",
Expand All @@ -20,7 +21,9 @@ bevy = { version = "0.16", default-features = false, features = [
"bevy_ui",
"bevy_winit",
"bevy_window",
"default_font",
"multi_threaded",
"vorbis",
"webgl2",
"x11",
] }
Expand All @@ -42,6 +45,7 @@ log = { version = "0.4", features = [
"max_level_debug",
"release_max_level_warn",
] }
bevy_pipelines_ready = "0.6.0"

[dev-dependencies]
approx = "0.5.1"
Expand Down
Binary file added assets/music/galactic_odyssey_by_alkakrab.ogg
Binary file not shown.
56 changes: 56 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
use crate::{save::SaveFile, GameState, Handles, MainCamera};
use bevy::{asset::LoadState, prelude::*};
use bevy_pipelines_ready::{PipelinesReady, PipelinesReadyPlugin};
use bevy_prototype_lyon::prelude::*;
use bevy_simple_prefs::PrefsStatus;

pub struct LoadingPlugin;

#[cfg(not(target_arch = "wasm32"))]
const EXPECTED_PIPELINES: usize = 10;
#[cfg(target_arch = "wasm32")]
const EXPECTED_PIPELINES: usize = 6;

pub const NUM_LEVELS: u32 = 12;

impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(PipelinesReadyPlugin);
app.init_resource::<Handles>();
app.add_systems(OnEnter(GameState::Loading), loading_setup);
app.add_systems(Update, loading_update.run_if(in_state(GameState::Loading)));
app.add_systems(
Update,
print_pipelines.run_if(resource_changed::<PipelinesReady>),
);
}
}

Expand All @@ -26,6 +38,13 @@ fn loading_setup(
MainCamera,
));

commands.spawn((
ShapeBuilder::with(&shapes::RegularPolygon::default())
.fill(Color::BLACK)
.build(),
StateScoped(GameState::Loading),
));

for i in 1..=NUM_LEVELS {
handles
.levels
Expand All @@ -35,14 +54,34 @@ fn loading_setup(
handles
.fonts
.push(asset_server.load("fonts/ChakraPetch-Regular-PixieWrangler.ttf"));

commands.spawn((
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
Children::spawn(Spawn(Text::new("Loading..."))),
StateScoped(GameState::Loading),
));

handles.music = asset_server.load("music/galactic_odyssey_by_alkakrab.ogg");
}

fn loading_update(
handles: Res<Handles>,
asset_server: Res<AssetServer>,
mut next_state: ResMut<NextState<GameState>>,
prefs: Res<PrefsStatus<SaveFile>>,
ready: Res<PipelinesReady>,
mut frames_since_pipelines_ready: Local<u32>,
) {
if ready.get() >= EXPECTED_PIPELINES {
*frames_since_pipelines_ready += 1;
}

if handles
.fonts
.iter()
Expand All @@ -59,9 +98,26 @@ fn loading_update(
return;
}

if !matches!(
asset_server.get_load_state(&handles.music),
Some(LoadState::Loaded),
) {
return;
}

// Firefox's FPS seems to take a few frames to recover after pipelines are
// compiled, resulting in weird audio artifacts.
if *frames_since_pipelines_ready < 10 {
return;
}

if !prefs.loaded {
return;
}

next_state.set(GameState::LevelSelect);
}

fn print_pipelines(ready: Res<PipelinesReady>) {
info!("Pipelines Ready: {}/{}", ready.get(), EXPECTED_PIPELINES);
}
Loading
Loading