Skip to content

Commit

Permalink
Fix music looping
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 9, 2023
1 parent efd0dd8 commit 70abbe9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
27 changes: 24 additions & 3 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use bevy_kira_audio::prelude::*;
use bevy_kira_audio::AudioPlugin as KiraAudioPlugin;
use bevy_kira_audio::AudioSource;
use rand::thread_rng;
use rand::Rng;

pub struct AudioPlugin;

impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(KiraAudioPlugin)
.init_collection::<AudioAssets>();
app.register_type::<BackgroundMusic>()
.add_plugins(KiraAudioPlugin)
.init_collection::<AudioAssets>()
.init_resource::<BackgroundMusic>()
.add_systems(Startup, spawn_background_music);
}
}

Expand All @@ -36,3 +39,21 @@ impl AudioAssets {
}
}
}

#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct BackgroundMusic(pub Handle<AudioInstance>);

fn spawn_background_music(
mut commands: Commands,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
) {
let handle = audio
.play(audio_assets.music.clone())
.with_volume(0.8)
.looped()
.paused()
.handle();
commands.insert_resource(BackgroundMusic(handle));
}
6 changes: 0 additions & 6 deletions src/state/editor_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ mod upgrade_panel;
use bevy::prelude::*;
use bevy::ui::Val::*;
use bevy_asset_loader::prelude::*;
use bevy_kira_audio::prelude::*;
use serde::Deserialize;
use serde::Serialize;

use crate::audio::AudioAssets;
use crate::config::Config;
pub use crate::state::editor_screen::code_panel::spawn_code_panel;
use crate::state::editor_screen::code_panel::spawn_light_code_panel;
Expand Down Expand Up @@ -116,17 +114,13 @@ fn enter_editor_screen(
root: Res<AppRoot>,
config: Res<Config>,
time: Res<Time>,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
) {
let config = &config.editor_screen;
commands.insert_resource(ClearColor(config.scene_view_background_color));
commands.insert_resource(EditorScreenStartTime(time.elapsed_seconds_f64()));

let screen = spawn_editor_screen(&mut commands, config.light_theme.clone(), true);
commands.entity(screen).set_parent(root.ui);

audio.play(audio_assets.music.clone()).looped();
}

pub fn spawn_editor_screen(
Expand Down
2 changes: 2 additions & 0 deletions src/state/loading_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use iyes_progress::prelude::*;
use serde::Deserialize;
use serde::Serialize;

use crate::audio::AudioAssets;
use crate::config::Config;
use crate::simulation::SpritePackAssets;
use crate::state::editor_screen::EditorScreenAssets;
Expand All @@ -22,6 +23,7 @@ impl Plugin for LoadingScreenStatePlugin {
.add_loading_state(LoadingState::new(LoadingScreen))
.add_collection_to_loading_state::<_, EditorScreenAssets>(LoadingScreen)
.add_collection_to_loading_state::<_, SpritePackAssets>(LoadingScreen)
.add_collection_to_loading_state::<_, AudioAssets>(LoadingScreen)
.add_plugins(ProgressPlugin::new(LoadingScreen).continue_to(EditorScreen))
.add_systems(OnEnter(LoadingScreen), enter_loading)
.add_systems(OnExit(LoadingScreen), exit_loading)
Expand Down
2 changes: 2 additions & 0 deletions src/state/title_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use iyes_progress::prelude::*;
use serde::Deserialize;
use serde::Serialize;

use crate::audio::AudioAssets;
use crate::config::Config;
use crate::simulation::SpritePackAssets;
use crate::state::editor_screen::EditorScreenAssets;
Expand All @@ -26,6 +27,7 @@ impl Plugin for TitleScreenStatePlugin {
.add_loading_state(LoadingState::new(TitleScreen))
.add_collection_to_loading_state::<_, EditorScreenAssets>(TitleScreen)
.add_collection_to_loading_state::<_, SpritePackAssets>(TitleScreen)
.add_collection_to_loading_state::<_, AudioAssets>(TitleScreen)
.add_plugins(ProgressPlugin::new(TitleScreen))
.add_systems(OnEnter(TitleScreen), enter_title_screen)
.add_systems(OnExit(TitleScreen), exit_title_screen);
Expand Down
21 changes: 21 additions & 0 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ops::Index;
use std::ops::IndexMut;
use std::time::Duration;

use bevy::ecs::event::ManualEventReader;
use bevy::ecs::system::SystemId;
Expand All @@ -10,6 +11,7 @@ use rand::thread_rng;
use strum::EnumCount;

use crate::audio::AudioAssets;
use crate::audio::BackgroundMusic;
use crate::config::Config;
use crate::physics::PhysicsSettings;
use crate::physics::UNIT_SPEED;
Expand Down Expand Up @@ -632,6 +634,7 @@ generate_upgrade_list!(
..default()
},

// TODO: These would be better implemented by sending e.g. a ChangeEditorTheme event
// Editor themes

DarkModeDracula: Upgrade {
Expand All @@ -641,6 +644,8 @@ generate_upgrade_list!(
mut commands: Commands,
root: Res<AppRoot>,
config: Res<Config>,
music: Res<BackgroundMusic>,
mut audio_instances: ResMut<Assets<AudioInstance>>,
| {
commands.entity(root.ui).despawn_descendants();
let editor_screen = spawn_editor_screen(
Expand All @@ -649,6 +654,13 @@ generate_upgrade_list!(
false,
);
commands.entity(editor_screen).set_parent(root.ui);

// Start background music
if let Some(instance) = audio_instances.get_mut(&music.0) {
instance.resume(AudioTween::new(Duration::from_secs_f32(0.15), AudioEasing::OutPowi(3)));
} else {
error!("Background music has not loaded yet");
}
})),
..default()
},
Expand All @@ -660,6 +672,8 @@ generate_upgrade_list!(
mut commands: Commands,
root: Res<AppRoot>,
config: Res<Config>,
music: Res<BackgroundMusic>,
mut audio_instances: ResMut<Assets<AudioInstance>>,
| {
commands.entity(root.ui).despawn_descendants();
let editor_screen = spawn_editor_screen(
Expand All @@ -668,6 +682,13 @@ generate_upgrade_list!(
false,
);
commands.entity(editor_screen).set_parent(root.ui);

// Start background music
if let Some(instance) = audio_instances.get_mut(&music.0) {
instance.resume(AudioTween::new(Duration::from_secs_f32(0.15), AudioEasing::OutPowi(3)));
} else {
error!("Background music has not loaded yet");
}
})),
..default()
},
Expand Down

0 comments on commit 70abbe9

Please sign in to comment.