Skip to content

Commit fe9a45d

Browse files
authored
Add Music (#20)
1 parent 7d2a77e commit fe9a45d

8 files changed

Lines changed: 532 additions & 139 deletions

File tree

Cargo.lock

Lines changed: 256 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ debugdump = ["bevy_mod_debugdump"]
1212
[dependencies]
1313
bevy = { version = "0.16", default-features = false, features = [
1414
"bevy_asset",
15+
"bevy_audio",
1516
"bevy_core_pipeline",
1617
"bevy_render",
1718
"bevy_sprite",
@@ -20,7 +21,9 @@ bevy = { version = "0.16", default-features = false, features = [
2021
"bevy_ui",
2122
"bevy_winit",
2223
"bevy_window",
24+
"default_font",
2325
"multi_threaded",
26+
"vorbis",
2427
"webgl2",
2528
"x11",
2629
] }
@@ -42,6 +45,7 @@ log = { version = "0.4", features = [
4245
"max_level_debug",
4346
"release_max_level_warn",
4447
] }
48+
bevy_pipelines_ready = "0.6.0"
4549

4650
[dev-dependencies]
4751
approx = "0.5.1"
3.06 MB
Binary file not shown.

src/loading.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
use crate::{save::SaveFile, GameState, Handles, MainCamera};
22
use bevy::{asset::LoadState, prelude::*};
3+
use bevy_pipelines_ready::{PipelinesReady, PipelinesReadyPlugin};
4+
use bevy_prototype_lyon::prelude::*;
35
use bevy_simple_prefs::PrefsStatus;
46

57
pub struct LoadingPlugin;
68

9+
#[cfg(not(target_arch = "wasm32"))]
10+
const EXPECTED_PIPELINES: usize = 10;
11+
#[cfg(target_arch = "wasm32")]
12+
const EXPECTED_PIPELINES: usize = 6;
13+
714
pub const NUM_LEVELS: u32 = 12;
815

916
impl Plugin for LoadingPlugin {
1017
fn build(&self, app: &mut App) {
18+
app.add_plugins(PipelinesReadyPlugin);
1119
app.init_resource::<Handles>();
1220
app.add_systems(OnEnter(GameState::Loading), loading_setup);
1321
app.add_systems(Update, loading_update.run_if(in_state(GameState::Loading)));
22+
app.add_systems(
23+
Update,
24+
print_pipelines.run_if(resource_changed::<PipelinesReady>),
25+
);
1426
}
1527
}
1628

@@ -26,6 +38,13 @@ fn loading_setup(
2638
MainCamera,
2739
));
2840

41+
commands.spawn((
42+
ShapeBuilder::with(&shapes::RegularPolygon::default())
43+
.fill(Color::BLACK)
44+
.build(),
45+
StateScoped(GameState::Loading),
46+
));
47+
2948
for i in 1..=NUM_LEVELS {
3049
handles
3150
.levels
@@ -35,14 +54,34 @@ fn loading_setup(
3554
handles
3655
.fonts
3756
.push(asset_server.load("fonts/ChakraPetch-Regular-PixieWrangler.ttf"));
57+
58+
commands.spawn((
59+
Node {
60+
width: Val::Percent(100.0),
61+
height: Val::Percent(100.0),
62+
justify_content: JustifyContent::Center,
63+
align_items: AlignItems::Center,
64+
..default()
65+
},
66+
Children::spawn(Spawn(Text::new("Loading..."))),
67+
StateScoped(GameState::Loading),
68+
));
69+
70+
handles.music = asset_server.load("music/galactic_odyssey_by_alkakrab.ogg");
3871
}
3972

4073
fn loading_update(
4174
handles: Res<Handles>,
4275
asset_server: Res<AssetServer>,
4376
mut next_state: ResMut<NextState<GameState>>,
4477
prefs: Res<PrefsStatus<SaveFile>>,
78+
ready: Res<PipelinesReady>,
79+
mut frames_since_pipelines_ready: Local<u32>,
4580
) {
81+
if ready.get() >= EXPECTED_PIPELINES {
82+
*frames_since_pipelines_ready += 1;
83+
}
84+
4685
if handles
4786
.fonts
4887
.iter()
@@ -59,9 +98,26 @@ fn loading_update(
5998
return;
6099
}
61100

101+
if !matches!(
102+
asset_server.get_load_state(&handles.music),
103+
Some(LoadState::Loaded),
104+
) {
105+
return;
106+
}
107+
108+
// Firefox's FPS seems to take a few frames to recover after pipelines are
109+
// compiled, resulting in weird audio artifacts.
110+
if *frames_since_pipelines_ready < 10 {
111+
return;
112+
}
113+
62114
if !prefs.loaded {
63115
return;
64116
}
65117

66118
next_state.set(GameState::LevelSelect);
67119
}
120+
121+
fn print_pipelines(ready: Res<PipelinesReady>) {
122+
info!("Pipelines Ready: {}/{}", ready.get(), EXPECTED_PIPELINES);
123+
}

0 commit comments

Comments
 (0)