From 3fecb314f60b91dec674c3a7eeb0f0f5269750aa Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 6 Dec 2022 16:43:37 -0600 Subject: [PATCH] Upgrade to Bevy 0.9. --- Cargo.toml | 5 +++-- examples/game.rs | 26 +++++++++++++------------- src/lib.rs | 28 ++++++++++++++++++---------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0dcf6b8..74088f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,13 +4,14 @@ version = "0.1.0" authors = ["Nolan Darilek "] license = "MIT OR Apache-2.0" edition = "2021" +repository = "https://labs.lightsout.games/projects/bevy_synthizer" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] anyhow = "1" -bevy = { version = "0.8", default-features = false, features = ["bevy_asset"] } +bevy = { version = "0.9", default-features = false, features = ["bevy_asset"] } synthizer = "0.5" [dev-dependencies] -bevy = { version = "0.8", default-features = true } \ No newline at end of file +bevy = { version = "0.9", default-features = true } \ No newline at end of file diff --git a/examples/game.rs b/examples/game.rs index 8b6258d..115e5b2 100644 --- a/examples/game.rs +++ b/examples/game.rs @@ -8,11 +8,11 @@ struct RotationTimer(Timer); impl Default for RotationTimer { fn default() -> Self { - Self(Timer::from_seconds(30., true)) + Self(Timer::from_seconds(30., TimerMode::Repeating)) } } -#[derive(Default)] +#[derive(Resource, Default)] struct AssetHandles { sounds: Vec, loaded: bool, @@ -34,22 +34,22 @@ fn load_and_create( .get_group_load_state(handles.sounds.iter().map(|handle| handle.id)) == LoadState::Loaded; if handles.loaded { - commands - .spawn_bundle(TransformBundle::default()) - .insert(Listener) - .insert(RotationTimer::default()); + commands.spawn(( + TransformBundle::default(), + Listener, + RotationTimer::default(), + )); let handle = handles.sounds[0].clone(); let buffer = asset_server.get_handle(handle); - commands - .spawn_bundle(TransformBundle::from(Transform::from_translation( - Vec3::new(10., 0., 0.), - ))) - .insert(Source::default()) - .insert(Sound { + commands.spawn(( + TransformBundle::from(Transform::from_translation(Vec3::new(10., 0., 0.))), + Source::default(), + Sound { buffer, looping: true, ..default() - }); + }, + )); } } diff --git a/src/lib.rs b/src/lib.rs index e127077..debcc2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ use bevy::{ }; pub use synthizer as syz; -#[derive(Clone, Debug, Deref, DerefMut, PartialEq, TypeUuid)] +#[derive(Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypeUuid)] #[uuid = "6b6b533a-bb1f-11ec-bda2-00155d8fdde9"] pub struct Buffer(syz::Buffer); @@ -43,6 +43,9 @@ impl AssetLoader for BufferAssetLoader { } } +#[derive(Resource, Clone, Debug, Deref, DerefMut)] +pub struct Context(syz::Context); + #[derive(Component, Clone, Debug, Reflect)] #[reflect(Component)] pub struct Source { @@ -157,7 +160,7 @@ pub enum SynthizerEvent { pub struct Listener; fn update_listener( - context: ResMut, + context: ResMut, listener: Query, With>, ) { if let Ok(transform) = listener.get_single() { @@ -192,7 +195,7 @@ fn update_listener( } fn add_source_handle( - context: Res, + context: Res, mut query: Query<( &mut Source, Option<&PannerStrategy>, @@ -241,7 +244,7 @@ fn add_source_handle( } fn add_generator( - context: Res, + context: Res, buffers: Res>, mut query: Query<(Entity, Option<&Parent>, &mut Sound)>, mut sources: Query<&mut Source>, @@ -313,7 +316,7 @@ fn add_sound_without_source( } } -#[derive(Default, Deref, DerefMut)] +#[derive(Resource, Default, Deref, DerefMut)] struct LastBuffer(HashMap>); fn swap_buffers( @@ -354,7 +357,7 @@ fn change_panner_strategy( } fn update_source_properties( - context: Res, + context: Res, mut query: Query<( &mut Source, Option<&DistanceModel>, @@ -561,7 +564,7 @@ fn remove_sound(mut last_buffer: ResMut, removed: RemovedComponents< } } -#[derive(Clone, Default, Debug)] +#[derive(Resource, Clone, Default, Debug)] pub struct SynthizerConfig { pub default_panner_strategy: Option, pub default_distance_model: Option, @@ -574,7 +577,7 @@ pub struct SynthizerConfig { pub log_to_stderr: bool, } -#[derive(Debug)] +#[derive(Resource, Debug)] pub struct SynthizerDefaults { pub panner_strategy: syz::PannerStrategy, pub distance_model: syz::DistanceModel, @@ -586,7 +589,7 @@ pub struct SynthizerDefaults { } fn sync_config( - context: Res, + context: Res, config: Res, defaults: Res, ) { @@ -639,7 +642,7 @@ fn sync_config( } fn events( - context: Res, + context: Res, sounds: Query<(Entity, &Sound)>, mut output: EventWriter, ) { @@ -677,6 +680,9 @@ pub enum SynthizerSystems { UpdateState, } +#[derive(Resource)] +struct InitializationGuard(syz::InitializationGuard); + pub struct SynthizerPlugin; impl Plugin for SynthizerPlugin { @@ -693,6 +699,7 @@ impl Plugin for SynthizerPlugin { let guard = syz_config .initialize() .expect("Failed to initialize Synthizer"); + let guard = InitializationGuard(guard); let context = syz::Context::new().expect("Failed to create Synthizer context"); let defaults = SynthizerDefaults { panner_strategy: context.default_panner_strategy().get().unwrap(), @@ -704,6 +711,7 @@ impl Plugin for SynthizerPlugin { closeness_boost_distance: context.default_closeness_boost_distance().get().unwrap(), }; context.enable_events().expect("Failed to enable events"); + let context = Context(context); app.add_asset::() .init_asset_loader::() .register_type::()