Skip to content

Commit

Permalink
Disable Bevy Music completely on Web
Browse files Browse the repository at this point in the history
  • Loading branch information
necrashter committed Dec 11, 2023
1 parent 6cb16bd commit 28f8fce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ pub struct AudioPlugin;

impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(KiraAudioPlugin)
.init_collection::<AudioAssets>();

// Music will be played by Web Audio API on web.
#[cfg(not(feature = "web"))]
app.register_type::<BackgroundMusic>()
.add_plugins(KiraAudioPlugin)
.init_collection::<AudioAssets>()
.init_resource::<BackgroundMusic>()
.add_systems(Startup, spawn_background_music);
}
Expand Down Expand Up @@ -45,6 +48,7 @@ pub struct AudioAssets {
pub guitar_sounds: Vec<Handle<AudioSource>>,
#[asset(paths("audio/unicorn0.ogg", "audio/unicorn1.ogg"), collection(typed))]
pub unicorn_sounds: Vec<Handle<AudioSource>>,
#[cfg(not(feature = "web"))]
#[asset(path = "music/ingame.ogg")]
pub music: Handle<AudioSource>,
}
Expand All @@ -67,10 +71,12 @@ impl AudioAssets {
}
}

#[cfg(not(feature = "web"))]
#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct BackgroundMusic(pub Handle<AudioInstance>);

#[cfg(not(feature = "web"))]
fn spawn_background_music(
mut commands: Commands,
audio: Res<Audio>,
Expand Down
7 changes: 7 additions & 0 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rand::thread_rng;
use strum::EnumCount;

use crate::audio::AudioAssets;
#[cfg(not(feature = "web"))]
use crate::audio::BackgroundMusic;
use crate::audio::SoundEffectKind;
use crate::config::Config;
Expand Down Expand Up @@ -867,7 +868,9 @@ generate_upgrade_list!(
mut commands: Commands,
root: Res<AppRoot>,
config: Res<Config>,
#[cfg(not(feature = "web"))]
music: Res<BackgroundMusic>,
#[cfg(not(feature = "web"))]
mut audio_instances: ResMut<Assets<AudioInstance>>,
| {
commands.entity(root.ui).despawn_descendants();
Expand Down Expand Up @@ -898,7 +901,9 @@ generate_upgrade_list!(
mut commands: Commands,
root: Res<AppRoot>,
config: Res<Config>,
#[cfg(not(feature = "web"))]
music: Res<BackgroundMusic>,
#[cfg(not(feature = "web"))]
mut audio_instances: ResMut<Assets<AudioInstance>>,
| {
commands.entity(root.ui).despawn_descendants();
Expand Down Expand Up @@ -929,7 +934,9 @@ generate_upgrade_list!(
mut commands: Commands,
root: Res<AppRoot>,
config: Res<Config>,
#[cfg(not(feature = "web"))]
music: Res<BackgroundMusic>,
#[cfg(not(feature = "web"))]
mut audio_instances: ResMut<Assets<AudioInstance>>,
| {
commands.entity(root.ui).despawn_descendants();
Expand Down

0 comments on commit 28f8fce

Please sign in to comment.