diff --git a/Cargo.toml b/Cargo.toml index d9724da4537257601cb99c384d3a00cdc7f9167d..0047fb33a2f30430f30120569b157a196d76f8dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "micro_musicbox" -version = "0.6.2" +version = "0.7.0" edition = "2021" license = "Apache-2.0" authors = ["Louis Capitanchik <louis@microhacks.co.uk>"] @@ -10,6 +10,16 @@ description = "Opinionated service interface for bevy_kira_audio" 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] default = [] serde = ["dep:serde"] @@ -19,10 +29,10 @@ wav = ["bevy_kira_audio/wav"] ogg = ["bevy_kira_audio/ogg"] [dependencies] -bevy = { version = "0.10.0", default-features = false } -bevy_kira_audio = { version = "0.15.0", default-features = false } +bevy = { version = "0.11.0", default-features = false } +bevy_kira_audio = { version = "0.16.0", default-features = false } serde = { version = "1", optional = true } [dev_dependencies] -bevy = "0.10.0" +bevy = "0.11.0" log = "0.4" diff --git a/README.md b/README.md index 9a23019fe9337bac31a114c2fa5bcfb908faa89e..5a7feac8950a179c5ffdd787e5fceac930a24612 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ however, recommended that you create your own impl for your own resource type. ```rust /// An example storage resource that allows assets to be retrieved by name, /// rather than by file path +#[derive(Resource)] pub struct AssetHandles { // ...Other types... pub sounds: HashMap<String, Handle<AudioSource>>, @@ -139,6 +140,7 @@ The examples in this repository use assets available under the following license | musicbox version | bevy version | bka version | |------------------|--------------|-------------| +| 0.7 | 0.11 | 0.16 | | 0.6 | 0.10 | 0.15 | | 0.5 | 0.9 | 0.13 | -| 0.4 | 0.8.0 | 0.12 | \ No newline at end of file +| 0.4 | 0.8 | 0.12 | \ No newline at end of file diff --git a/examples/basic.rs b/examples/basic.rs index 22b0886bd5a298fe81f33f655b0e7c143915dcc5..850ff4a22694a772d58876b918d0879441be406d 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -8,13 +8,13 @@ fn main() { .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { resolution: WindowResolution::new(800.0, 600.0), - title: String::from("Kitchen Sink Example"), + title: String::from("Basic Example"), ..Default::default() }), ..Default::default() })) .add_plugins(CombinedAudioPlugins::<AssetServer>::new()) - .add_startup_system(play_audio) + .add_systems(Startup, play_audio) .run(); } diff --git a/examples/kitchen_sink.rs b/examples/kitchen_sink.rs index 6199a44a61fb4b6a150f71109cfc36026fee06bc..0f76aee02b69c34aa68901a3a5360c7bc6cb8c38 100644 --- a/examples/kitchen_sink.rs +++ b/examples/kitchen_sink.rs @@ -16,7 +16,7 @@ mod utilities; pub fn main() { App::new() - .add_plugin(utilities::SetupPlugin) // Loads resources + .add_plugins(utilities::SetupPlugin) // Loads resources .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { resolution: WindowResolution::new(800.0, 600.0), @@ -25,13 +25,15 @@ pub fn main() { }), ..Default::default() })) .add_plugins(CombinedAudioPlugins::<AssetServer>::new()) - .add_system( - setup_audio.in_schedule(OnEnter(AppState::Running)) + .add_systems( + OnEnter(AppState::Running), + setup_audio ) - .add_system( - set_instructions.in_schedule(OnEnter(AppState::Running)) + .add_systems( + 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(); } @@ -42,10 +44,6 @@ pub struct MusicState { pub playing_first: bool, } -// pub fn has_music_state(state: Option<Res<MusicState>>) -> ShouldRun { -// state.is_some().into() -// } - pub fn set_instructions( mut instructions: Query<&mut Text, (With<TextMarker>, Without<DetailsMarker>)>, mut details: Query<&mut Text, (With<DetailsMarker>, Without<TextMarker>)>, @@ -65,14 +63,12 @@ pub fn setup_audio(mut commands: Commands, mut musicbox: MusicBox<AssetServer>) }); } -// pub fn - pub fn cross_fade_tracks( input: Res<Input<KeyCode>>, mut music_box: MusicBox<AssetServer>, mut music_state: ResMut<MusicState>, ) { - if input.just_released(KeyCode::RShift) { + if input.just_released(KeyCode::ShiftRight) { if music_state.playing_first { music_box.cross_fade_music( "The-Great-Madeja.mp3", diff --git a/examples/utilities/mod.rs b/examples/utilities/mod.rs index 3f82ecc083fe1edf39840bcadf383635f3078b6f..03cacb46ce96835d856947f247c4d6660e9df8c5 100644 --- a/examples/utilities/mod.rs +++ b/examples/utilities/mod.rs @@ -1,5 +1,4 @@ -use bevy::app::Plugin; -use bevy::asset::{Handle, LoadState}; +use bevy::asset::LoadState; use bevy::prelude::*; use bevy_kira_audio::AudioSource; @@ -42,7 +41,7 @@ pub fn check_load_state( match load_state { LoadState::Loaded => { log::info!("STATE {:?}", appstate); - *next_state = NextState(Some(AppState::Running)); // appstate.set(AppState::Running); + next_state.set(AppState::Running); } LoadState::Loading => {} _ => { @@ -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 #[derive(Component)] pub struct TextMarker; @@ -73,7 +62,8 @@ pub fn create_ui(mut commands: Commands, assets: Res<AssetServer>) { commands .spawn(NodeBundle { 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, align_items: AlignItems::Center, flex_direction: FlexDirection::Column, @@ -115,9 +105,9 @@ pub struct SetupPlugin; impl Plugin for SetupPlugin { fn build(&self, app: &mut App) { app.add_state::<AppState>() - .add_startup_system(load_resources) - .add_startup_system(create_ui) - .add_system( + .add_systems(Startup, (load_resources, create_ui)) + .add_systems( + Update, check_load_state .run_if(resource_exists::<AudioResources>()) .run_if(in_state(AppState::Loading)), diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 271800cb2f3791b3adc24328e71c9e2550b439db..31578d3bfd615e5977c8e2a53810f548769cf460 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel = "nightly" \ No newline at end of file +channel = "stable" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index f703a0fd581e04517ff76b7a9bef3901c7c7278e..e20b0091c0b2cbbf29eb6381dc887ac3f027e626 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ //! fn main() { //! App::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"); //! }); //! } @@ -25,8 +25,8 @@ use std::marker::PhantomData; -use bevy::app::{App, CoreSet, Plugin, PluginGroup, PluginGroupBuilder}; -use bevy::prelude::{IntoSystemConfig, SystemSet}; +use bevy::app::PluginGroupBuilder; +use bevy::prelude::*; use bevy_kira_audio::{AudioApp, AudioPlugin}; use crate::channels::{ @@ -61,9 +61,7 @@ pub struct MusicBoxPlugin<T: SuppliesAudio> { impl<T: SuppliesAudio> Default for MusicBoxPlugin<T> { fn default() -> Self { - Self { - _t: PhantomData::default(), - } + Self { _t: PhantomData } } } @@ -82,7 +80,7 @@ impl<T: SuppliesAudio> Plugin for MusicBoxPlugin<T> { .add_audio_channel::<UiSfxAudioChannel>() .insert_resource(AudioSettings::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> { impl<T: SuppliesAudio> Default for CombinedAudioPlugins<T> { fn default() -> Self { - Self { - _t: PhantomData::default(), - } + Self { _t: PhantomData } } } diff --git a/src/music_box.rs b/src/music_box.rs index 3c27cf40ab9ccaffd1febed5d80a99eac6d647d7..3d81edb92be8d8be7d6d30e59c5e8435b060bdff 100644 --- a/src/music_box.rs +++ b/src/music_box.rs @@ -1,7 +1,7 @@ use std::marker::PhantomData; 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 crate::utilities::{AudioSettings, SuppliesAudio, TrackType}; @@ -141,6 +141,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { } match self.map_tracks(&name) { + #[allow(deprecated)] TrackType::WithIntro(_, track) | TrackType::Single(track) => { let next = self .channels @@ -150,8 +151,8 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { .looped() .handle(); - self.state.active_music_name = Some(name.clone()); - self.state.active_music = Some(next.clone()); + self.state.active_music_name = Some(name); + self.state.active_music = Some(next.clone_weak()); Some(next) } @@ -233,6 +234,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { fade: AudioTween, ) -> Option<Handle<AudioInstance>> { match self.map_tracks(name) { + #[allow(deprecated)] TrackType::WithIntro(_, track) | TrackType::Single(track) => { let next = self .channels @@ -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 pub fn play_sfx<Name: ToString>(&mut self, name: Name) -> Option<Handle<AudioInstance>> { match self.map_tracks(name) { + #[allow(deprecated)] TrackType::WithIntro(_, track) | TrackType::Single(track) => { let instance = self.channels.sfx_channel.play(track).handle(); Some(instance) @@ -275,6 +278,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { name: Name, ) -> Option<Handle<AudioInstance>> { match self.map_tracks(name) { + #[allow(deprecated)] TrackType::WithIntro(_, track) | TrackType::Single(track) => { let instance = self.channels.sfx_channel.play(track).looped().handle(); Some(instance) @@ -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 pub fn play_ui_sfx<Name: ToString>(&mut self, name: Name) -> Option<Handle<AudioInstance>> { match self.map_tracks(name) { + #[allow(deprecated)] TrackType::WithIntro(_, track) | TrackType::Single(track) => { let instance = self.channels.ui_sfx_channel.play(track).handle(); Some(instance) @@ -395,6 +400,7 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> { Some(handle) => TrackType::Single(handle), None => TrackType::Missing, }, + #[allow(deprecated)] TrackType::WithIntro(intro, looper) => match ( self.handles.get_audio_track(intro), self.handles.get_audio_track(looper), diff --git a/src/utilities.rs b/src/utilities.rs index 5c6cc40335d31dc199b8c47de2ff89aee8e9955b..be41dfe9f6503f795e68cfa756bedd9af385f71a 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -1,4 +1,3 @@ -use bevy::ecs::system::Resource; use bevy::prelude::*; use bevy_kira_audio::AudioSource; @@ -18,13 +17,13 @@ use crate::music_box::MusicBox; /// # use bevy::prelude::*; /// /// impl SuppliesAudio for AssetServer { -/// fn resolve_track_name<T: ToString>(&self, name: T) -> TrackType<String> { -/// TrackType::Single(name.to_string()) -/// } +/// fn resolve_track_name<T: ToString>(&self, name: T) -> TrackType<String> { /// +/// TrackType::Single(name.to_string()) +/// } /// -/// fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> { -/// Some(self.load(&name.to_string())) -/// } +/// fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> { +/// Some(self.load(&name.to_string())) +/// } /// } /// ``` pub trait SuppliesAudio: Resource { @@ -71,7 +70,7 @@ impl SuppliesAudio for AssetServer { } fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> { - Some(self.load(&name.to_string())) + Some(self.load(name.to_string())) } }