Skip to content
Snippets Groups Projects
Verified Commit 564e0b8f authored by Louis's avatar Louis :fire:
Browse files

Update to Bevy 0.11, BevyKiraAudio 0.16

parent 7190c6d8
No related branches found
No related tags found
No related merge requests found
[package] [package]
name = "micro_musicbox" name = "micro_musicbox"
version = "0.6.2" version = "0.7.0"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
authors = ["Louis Capitanchik <louis@microhacks.co.uk>"] authors = ["Louis Capitanchik <louis@microhacks.co.uk>"]
...@@ -10,6 +10,16 @@ description = "Opinionated service interface for bevy_kira_audio" ...@@ -10,6 +10,16 @@ description = "Opinionated service interface for bevy_kira_audio"
exclude = ["assets"] exclude = ["assets"]
[[example]]
name = "kitchen-sink"
path = "examples/kitchen_sink.rs"
required-features = ["mp3"]
[[example]]
name = "basic"
path = "examples/basic.rs"
required-features = ["mp3"]
[features] [features]
default = [] default = []
serde = ["dep:serde"] serde = ["dep:serde"]
...@@ -19,10 +29,10 @@ wav = ["bevy_kira_audio/wav"] ...@@ -19,10 +29,10 @@ wav = ["bevy_kira_audio/wav"]
ogg = ["bevy_kira_audio/ogg"] ogg = ["bevy_kira_audio/ogg"]
[dependencies] [dependencies]
bevy = { version = "0.10.0", default-features = false } bevy = { version = "0.11.0", default-features = false }
bevy_kira_audio = { version = "0.15.0", default-features = false } bevy_kira_audio = { version = "0.16.0", default-features = false }
serde = { version = "1", optional = true } serde = { version = "1", optional = true }
[dev_dependencies] [dev_dependencies]
bevy = "0.10.0" bevy = "0.11.0"
log = "0.4" log = "0.4"
...@@ -90,6 +90,7 @@ however, recommended that you create your own impl for your own resource type. ...@@ -90,6 +90,7 @@ however, recommended that you create your own impl for your own resource type.
```rust ```rust
/// An example storage resource that allows assets to be retrieved by name, /// An example storage resource that allows assets to be retrieved by name,
/// rather than by file path /// rather than by file path
#[derive(Resource)]
pub struct AssetHandles { pub struct AssetHandles {
// ...Other types... // ...Other types...
pub sounds: HashMap<String, Handle<AudioSource>>, pub sounds: HashMap<String, Handle<AudioSource>>,
...@@ -139,6 +140,7 @@ The examples in this repository use assets available under the following license ...@@ -139,6 +140,7 @@ The examples in this repository use assets available under the following license
| musicbox version | bevy version | bka version | | musicbox version | bevy version | bka version |
|------------------|--------------|-------------| |------------------|--------------|-------------|
| 0.7 | 0.11 | 0.16 |
| 0.6 | 0.10 | 0.15 | | 0.6 | 0.10 | 0.15 |
| 0.5 | 0.9 | 0.13 | | 0.5 | 0.9 | 0.13 |
| 0.4 | 0.8.0 | 0.12 | | 0.4 | 0.8 | 0.12 |
\ No newline at end of file \ No newline at end of file
...@@ -8,13 +8,13 @@ fn main() { ...@@ -8,13 +8,13 @@ fn main() {
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
resolution: WindowResolution::new(800.0, 600.0), resolution: WindowResolution::new(800.0, 600.0),
title: String::from("Kitchen Sink Example"), title: String::from("Basic Example"),
..Default::default() ..Default::default()
}), }),
..Default::default() ..Default::default()
})) }))
.add_plugins(CombinedAudioPlugins::<AssetServer>::new()) .add_plugins(CombinedAudioPlugins::<AssetServer>::new())
.add_startup_system(play_audio) .add_systems(Startup, play_audio)
.run(); .run();
} }
......
...@@ -16,7 +16,7 @@ mod utilities; ...@@ -16,7 +16,7 @@ mod utilities;
pub fn main() { pub fn main() {
App::new() App::new()
.add_plugin(utilities::SetupPlugin) // Loads resources .add_plugins(utilities::SetupPlugin) // Loads resources
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
resolution: WindowResolution::new(800.0, 600.0), resolution: WindowResolution::new(800.0, 600.0),
...@@ -25,13 +25,15 @@ pub fn main() { ...@@ -25,13 +25,15 @@ pub fn main() {
}), }),
..Default::default() ..Default::default()
})) .add_plugins(CombinedAudioPlugins::<AssetServer>::new()) })) .add_plugins(CombinedAudioPlugins::<AssetServer>::new())
.add_system( .add_systems(
setup_audio.in_schedule(OnEnter(AppState::Running)) OnEnter(AppState::Running),
setup_audio
) )
.add_system( .add_systems(
set_instructions.in_schedule(OnEnter(AppState::Running)) OnEnter(AppState::Running),
set_instructions
) )
.add_system(cross_fade_tracks.run_if(resource_exists::<MusicState>())) .add_systems(Update, cross_fade_tracks.run_if(resource_exists::<MusicState>()))
.run(); .run();
} }
...@@ -42,10 +44,6 @@ pub struct MusicState { ...@@ -42,10 +44,6 @@ pub struct MusicState {
pub playing_first: bool, pub playing_first: bool,
} }
// pub fn has_music_state(state: Option<Res<MusicState>>) -> ShouldRun {
// state.is_some().into()
// }
pub fn set_instructions( pub fn set_instructions(
mut instructions: Query<&mut Text, (With<TextMarker>, Without<DetailsMarker>)>, mut instructions: Query<&mut Text, (With<TextMarker>, Without<DetailsMarker>)>,
mut details: Query<&mut Text, (With<DetailsMarker>, Without<TextMarker>)>, mut details: Query<&mut Text, (With<DetailsMarker>, Without<TextMarker>)>,
...@@ -65,14 +63,12 @@ pub fn setup_audio(mut commands: Commands, mut musicbox: MusicBox<AssetServer>) ...@@ -65,14 +63,12 @@ pub fn setup_audio(mut commands: Commands, mut musicbox: MusicBox<AssetServer>)
}); });
} }
// pub fn
pub fn cross_fade_tracks( pub fn cross_fade_tracks(
input: Res<Input<KeyCode>>, input: Res<Input<KeyCode>>,
mut music_box: MusicBox<AssetServer>, mut music_box: MusicBox<AssetServer>,
mut music_state: ResMut<MusicState>, mut music_state: ResMut<MusicState>,
) { ) {
if input.just_released(KeyCode::RShift) { if input.just_released(KeyCode::ShiftRight) {
if music_state.playing_first { if music_state.playing_first {
music_box.cross_fade_music( music_box.cross_fade_music(
"The-Great-Madeja.mp3", "The-Great-Madeja.mp3",
......
use bevy::app::Plugin; use bevy::asset::LoadState;
use bevy::asset::{Handle, LoadState};
use bevy::prelude::*; use bevy::prelude::*;
use bevy_kira_audio::AudioSource; use bevy_kira_audio::AudioSource;
...@@ -42,7 +41,7 @@ pub fn check_load_state( ...@@ -42,7 +41,7 @@ pub fn check_load_state(
match load_state { match load_state {
LoadState::Loaded => { LoadState::Loaded => {
log::info!("STATE {:?}", appstate); log::info!("STATE {:?}", appstate);
*next_state = NextState(Some(AppState::Running)); // appstate.set(AppState::Running); next_state.set(AppState::Running);
} }
LoadState::Loading => {} LoadState::Loading => {}
_ => { _ => {
...@@ -51,16 +50,6 @@ pub fn check_load_state( ...@@ -51,16 +50,6 @@ pub fn check_load_state(
} }
} }
// pub fn has_audio_resources(res: Option<Res<AudioResources>>) -> ShouldRun {
// res.is_some().into()
// }
// pub fn is_state_loading(state: Res<AppState>) -> ShouldRun {
// (*state == AppState::Loading).into()
// }
// pub fn is_state_running(state: Res<AppState>) -> ShouldRun {
// (*state == AppState::Running).into()
// }
/// This component allows us to easily grab the on screen text /// This component allows us to easily grab the on screen text
#[derive(Component)] #[derive(Component)]
pub struct TextMarker; pub struct TextMarker;
...@@ -73,7 +62,8 @@ pub fn create_ui(mut commands: Commands, assets: Res<AssetServer>) { ...@@ -73,7 +62,8 @@ pub fn create_ui(mut commands: Commands, assets: Res<AssetServer>) {
commands commands
.spawn(NodeBundle { .spawn(NodeBundle {
style: Style { style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center, justify_content: JustifyContent::Center,
align_items: AlignItems::Center, align_items: AlignItems::Center,
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
...@@ -115,9 +105,9 @@ pub struct SetupPlugin; ...@@ -115,9 +105,9 @@ pub struct SetupPlugin;
impl Plugin for SetupPlugin { impl Plugin for SetupPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_state::<AppState>() app.add_state::<AppState>()
.add_startup_system(load_resources) .add_systems(Startup, (load_resources, create_ui))
.add_startup_system(create_ui) .add_systems(
.add_system( Update,
check_load_state check_load_state
.run_if(resource_exists::<AudioResources>()) .run_if(resource_exists::<AudioResources>())
.run_if(in_state(AppState::Loading)), .run_if(in_state(AppState::Loading)),
......
[toolchain] [toolchain]
channel = "nightly" channel = "stable"
\ No newline at end of file \ No newline at end of file
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
//! fn main() { //! fn main() {
//! App::new() //! App::new()
//! .add_plugins(CombinedAudioPlugins::<AssetServer>::new()) //! .add_plugins(CombinedAudioPlugins::<AssetServer>::new())
//! .add_startup_system(|mut music_box: MusicBox<AssetServer>| { //! .add_systems(Startup, |mut music_box: MusicBox<AssetServer>| {
//! music_box.play_music("music/bing_bong.mp3"); //! music_box.play_music("music/bing_bong.mp3");
//! }); //! });
//! } //! }
...@@ -25,8 +25,8 @@ ...@@ -25,8 +25,8 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use bevy::app::{App, CoreSet, Plugin, PluginGroup, PluginGroupBuilder}; use bevy::app::PluginGroupBuilder;
use bevy::prelude::{IntoSystemConfig, SystemSet}; use bevy::prelude::*;
use bevy_kira_audio::{AudioApp, AudioPlugin}; use bevy_kira_audio::{AudioApp, AudioPlugin};
use crate::channels::{ use crate::channels::{
...@@ -61,9 +61,7 @@ pub struct MusicBoxPlugin<T: SuppliesAudio> { ...@@ -61,9 +61,7 @@ pub struct MusicBoxPlugin<T: SuppliesAudio> {
impl<T: SuppliesAudio> Default for MusicBoxPlugin<T> { impl<T: SuppliesAudio> Default for MusicBoxPlugin<T> {
fn default() -> Self { fn default() -> Self {
Self { Self { _t: PhantomData }
_t: PhantomData::default(),
}
} }
} }
...@@ -82,7 +80,7 @@ impl<T: SuppliesAudio> Plugin for MusicBoxPlugin<T> { ...@@ -82,7 +80,7 @@ impl<T: SuppliesAudio> Plugin for MusicBoxPlugin<T> {
.add_audio_channel::<UiSfxAudioChannel>() .add_audio_channel::<UiSfxAudioChannel>()
.insert_resource(AudioSettings::default()) .insert_resource(AudioSettings::default())
.insert_resource(MusicBoxState::default()) .insert_resource(MusicBoxState::default())
.add_system(utilities::sync_music_volume::<T>.in_base_set(CoreSet::Last)); .add_systems(Last, utilities::sync_music_volume::<T>);
} }
} }
...@@ -94,9 +92,7 @@ pub struct CombinedAudioPlugins<T: SuppliesAudio> { ...@@ -94,9 +92,7 @@ pub struct CombinedAudioPlugins<T: SuppliesAudio> {
impl<T: SuppliesAudio> Default for CombinedAudioPlugins<T> { impl<T: SuppliesAudio> Default for CombinedAudioPlugins<T> {
fn default() -> Self { fn default() -> Self {
Self { Self { _t: PhantomData }
_t: PhantomData::default(),
}
} }
} }
......
use std::marker::PhantomData; use std::marker::PhantomData;
use bevy::ecs::system::SystemParam; use bevy::ecs::system::SystemParam;
use bevy::prelude::{Assets, Commands, DetectChanges, Handle, Res, ResMut, Resource}; use bevy::prelude::{Assets, DetectChanges, Handle, Res, ResMut, Resource};
use bevy_kira_audio::{AudioChannel, AudioControl, AudioInstance, AudioSource, AudioTween}; use bevy_kira_audio::{AudioChannel, AudioControl, AudioInstance, AudioSource, AudioTween};
use crate::utilities::{AudioSettings, SuppliesAudio, TrackType}; use crate::utilities::{AudioSettings, SuppliesAudio, TrackType};
...@@ -141,6 +141,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { ...@@ -141,6 +141,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
} }
match self.map_tracks(&name) { match self.map_tracks(&name) {
#[allow(deprecated)]
TrackType::WithIntro(_, track) | TrackType::Single(track) => { TrackType::WithIntro(_, track) | TrackType::Single(track) => {
let next = self let next = self
.channels .channels
...@@ -150,8 +151,8 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { ...@@ -150,8 +151,8 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
.looped() .looped()
.handle(); .handle();
self.state.active_music_name = Some(name.clone()); self.state.active_music_name = Some(name);
self.state.active_music = Some(next.clone()); self.state.active_music = Some(next.clone_weak());
Some(next) Some(next)
} }
...@@ -233,6 +234,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { ...@@ -233,6 +234,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
fade: AudioTween, fade: AudioTween,
) -> Option<Handle<AudioInstance>> { ) -> Option<Handle<AudioInstance>> {
match self.map_tracks(name) { match self.map_tracks(name) {
#[allow(deprecated)]
TrackType::WithIntro(_, track) | TrackType::Single(track) => { TrackType::WithIntro(_, track) | TrackType::Single(track) => {
let next = self let next = self
.channels .channels
...@@ -256,6 +258,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { ...@@ -256,6 +258,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
/// A handle for the newly started audio instance, or `None` if the track was not found /// A handle for the newly started audio instance, or `None` if the track was not found
pub fn play_sfx<Name: ToString>(&mut self, name: Name) -> Option<Handle<AudioInstance>> { pub fn play_sfx<Name: ToString>(&mut self, name: Name) -> Option<Handle<AudioInstance>> {
match self.map_tracks(name) { match self.map_tracks(name) {
#[allow(deprecated)]
TrackType::WithIntro(_, track) | TrackType::Single(track) => { TrackType::WithIntro(_, track) | TrackType::Single(track) => {
let instance = self.channels.sfx_channel.play(track).handle(); let instance = self.channels.sfx_channel.play(track).handle();
Some(instance) Some(instance)
...@@ -275,6 +278,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { ...@@ -275,6 +278,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
name: Name, name: Name,
) -> Option<Handle<AudioInstance>> { ) -> Option<Handle<AudioInstance>> {
match self.map_tracks(name) { match self.map_tracks(name) {
#[allow(deprecated)]
TrackType::WithIntro(_, track) | TrackType::Single(track) => { TrackType::WithIntro(_, track) | TrackType::Single(track) => {
let instance = self.channels.sfx_channel.play(track).looped().handle(); let instance = self.channels.sfx_channel.play(track).looped().handle();
Some(instance) Some(instance)
...@@ -289,6 +293,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { ...@@ -289,6 +293,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
/// A handle for the newly started audio instance, or `None` if the track was not found /// A handle for the newly started audio instance, or `None` if the track was not found
pub fn play_ui_sfx<Name: ToString>(&mut self, name: Name) -> Option<Handle<AudioInstance>> { pub fn play_ui_sfx<Name: ToString>(&mut self, name: Name) -> Option<Handle<AudioInstance>> {
match self.map_tracks(name) { match self.map_tracks(name) {
#[allow(deprecated)]
TrackType::WithIntro(_, track) | TrackType::Single(track) => { TrackType::WithIntro(_, track) | TrackType::Single(track) => {
let instance = self.channels.ui_sfx_channel.play(track).handle(); let instance = self.channels.ui_sfx_channel.play(track).handle();
Some(instance) Some(instance)
...@@ -395,6 +400,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { ...@@ -395,6 +400,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
Some(handle) => TrackType::Single(handle), Some(handle) => TrackType::Single(handle),
None => TrackType::Missing, None => TrackType::Missing,
}, },
#[allow(deprecated)]
TrackType::WithIntro(intro, looper) => match ( TrackType::WithIntro(intro, looper) => match (
self.handles.get_audio_track(intro), self.handles.get_audio_track(intro),
self.handles.get_audio_track(looper), self.handles.get_audio_track(looper),
......
use bevy::ecs::system::Resource;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_kira_audio::AudioSource; use bevy_kira_audio::AudioSource;
...@@ -18,13 +17,13 @@ use crate::music_box::MusicBox; ...@@ -18,13 +17,13 @@ use crate::music_box::MusicBox;
/// # use bevy::prelude::*; /// # use bevy::prelude::*;
/// ///
/// impl SuppliesAudio for AssetServer { /// impl SuppliesAudio for AssetServer {
/// fn resolve_track_name<T: ToString>(&self, name: T) -> TrackType<String> { /// fn resolve_track_name<T: ToString>(&self, name: T) -> TrackType<String> { ///
/// TrackType::Single(name.to_string()) /// TrackType::Single(name.to_string())
/// } /// }
/// ///
/// fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> { /// fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> {
/// Some(self.load(&name.to_string())) /// Some(self.load(&name.to_string()))
/// } /// }
/// } /// }
/// ``` /// ```
pub trait SuppliesAudio: Resource { pub trait SuppliesAudio: Resource {
...@@ -71,7 +70,7 @@ impl SuppliesAudio for AssetServer { ...@@ -71,7 +70,7 @@ impl SuppliesAudio for AssetServer {
} }
fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> { fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> {
Some(self.load(&name.to_string())) Some(self.load(name.to_string()))
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment