Upgrade to Bevy 0.9.

This commit is contained in:
Nolan Darilek 2022-12-06 16:43:37 -06:00
parent 9cb1d1d447
commit 3fecb314f6
3 changed files with 34 additions and 25 deletions

View File

@ -4,13 +4,14 @@ version = "0.1.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"] authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
edition = "2021" 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "1" 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" synthizer = "0.5"
[dev-dependencies] [dev-dependencies]
bevy = { version = "0.8", default-features = true } bevy = { version = "0.9", default-features = true }

View File

@ -8,11 +8,11 @@ struct RotationTimer(Timer);
impl Default for RotationTimer { impl Default for RotationTimer {
fn default() -> Self { fn default() -> Self {
Self(Timer::from_seconds(30., true)) Self(Timer::from_seconds(30., TimerMode::Repeating))
} }
} }
#[derive(Default)] #[derive(Resource, Default)]
struct AssetHandles { struct AssetHandles {
sounds: Vec<HandleUntyped>, sounds: Vec<HandleUntyped>,
loaded: bool, loaded: bool,
@ -34,22 +34,22 @@ fn load_and_create(
.get_group_load_state(handles.sounds.iter().map(|handle| handle.id)) .get_group_load_state(handles.sounds.iter().map(|handle| handle.id))
== LoadState::Loaded; == LoadState::Loaded;
if handles.loaded { if handles.loaded {
commands commands.spawn((
.spawn_bundle(TransformBundle::default()) TransformBundle::default(),
.insert(Listener) Listener,
.insert(RotationTimer::default()); RotationTimer::default(),
));
let handle = handles.sounds[0].clone(); let handle = handles.sounds[0].clone();
let buffer = asset_server.get_handle(handle); let buffer = asset_server.get_handle(handle);
commands commands.spawn((
.spawn_bundle(TransformBundle::from(Transform::from_translation( TransformBundle::from(Transform::from_translation(Vec3::new(10., 0., 0.))),
Vec3::new(10., 0., 0.), Source::default(),
))) Sound {
.insert(Source::default())
.insert(Sound {
buffer, buffer,
looping: true, looping: true,
..default() ..default()
}); },
));
} }
} }

View File

@ -10,7 +10,7 @@ use bevy::{
}; };
pub use synthizer as syz; 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"] #[uuid = "6b6b533a-bb1f-11ec-bda2-00155d8fdde9"]
pub struct Buffer(syz::Buffer); 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)] #[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)] #[reflect(Component)]
pub struct Source { pub struct Source {
@ -157,7 +160,7 @@ pub enum SynthizerEvent {
pub struct Listener; pub struct Listener;
fn update_listener( fn update_listener(
context: ResMut<syz::Context>, context: ResMut<Context>,
listener: Query<Option<&GlobalTransform>, With<Listener>>, listener: Query<Option<&GlobalTransform>, With<Listener>>,
) { ) {
if let Ok(transform) = listener.get_single() { if let Ok(transform) = listener.get_single() {
@ -192,7 +195,7 @@ fn update_listener(
} }
fn add_source_handle( fn add_source_handle(
context: Res<syz::Context>, context: Res<Context>,
mut query: Query<( mut query: Query<(
&mut Source, &mut Source,
Option<&PannerStrategy>, Option<&PannerStrategy>,
@ -241,7 +244,7 @@ fn add_source_handle(
} }
fn add_generator( fn add_generator(
context: Res<syz::Context>, context: Res<Context>,
buffers: Res<Assets<Buffer>>, buffers: Res<Assets<Buffer>>,
mut query: Query<(Entity, Option<&Parent>, &mut Sound)>, mut query: Query<(Entity, Option<&Parent>, &mut Sound)>,
mut sources: Query<&mut Source>, 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<Entity, Handle<Buffer>>); struct LastBuffer(HashMap<Entity, Handle<Buffer>>);
fn swap_buffers( fn swap_buffers(
@ -354,7 +357,7 @@ fn change_panner_strategy(
} }
fn update_source_properties( fn update_source_properties(
context: Res<syz::Context>, context: Res<Context>,
mut query: Query<( mut query: Query<(
&mut Source, &mut Source,
Option<&DistanceModel>, Option<&DistanceModel>,
@ -561,7 +564,7 @@ fn remove_sound(mut last_buffer: ResMut<LastBuffer>, removed: RemovedComponents<
} }
} }
#[derive(Clone, Default, Debug)] #[derive(Resource, Clone, Default, Debug)]
pub struct SynthizerConfig { pub struct SynthizerConfig {
pub default_panner_strategy: Option<syz::PannerStrategy>, pub default_panner_strategy: Option<syz::PannerStrategy>,
pub default_distance_model: Option<syz::DistanceModel>, pub default_distance_model: Option<syz::DistanceModel>,
@ -574,7 +577,7 @@ pub struct SynthizerConfig {
pub log_to_stderr: bool, pub log_to_stderr: bool,
} }
#[derive(Debug)] #[derive(Resource, Debug)]
pub struct SynthizerDefaults { pub struct SynthizerDefaults {
pub panner_strategy: syz::PannerStrategy, pub panner_strategy: syz::PannerStrategy,
pub distance_model: syz::DistanceModel, pub distance_model: syz::DistanceModel,
@ -586,7 +589,7 @@ pub struct SynthizerDefaults {
} }
fn sync_config( fn sync_config(
context: Res<syz::Context>, context: Res<Context>,
config: Res<SynthizerConfig>, config: Res<SynthizerConfig>,
defaults: Res<SynthizerDefaults>, defaults: Res<SynthizerDefaults>,
) { ) {
@ -639,7 +642,7 @@ fn sync_config(
} }
fn events( fn events(
context: Res<syz::Context>, context: Res<Context>,
sounds: Query<(Entity, &Sound)>, sounds: Query<(Entity, &Sound)>,
mut output: EventWriter<SynthizerEvent>, mut output: EventWriter<SynthizerEvent>,
) { ) {
@ -677,6 +680,9 @@ pub enum SynthizerSystems {
UpdateState, UpdateState,
} }
#[derive(Resource)]
struct InitializationGuard(syz::InitializationGuard);
pub struct SynthizerPlugin; pub struct SynthizerPlugin;
impl Plugin for SynthizerPlugin { impl Plugin for SynthizerPlugin {
@ -693,6 +699,7 @@ impl Plugin for SynthizerPlugin {
let guard = syz_config let guard = syz_config
.initialize() .initialize()
.expect("Failed to initialize Synthizer"); .expect("Failed to initialize Synthizer");
let guard = InitializationGuard(guard);
let context = syz::Context::new().expect("Failed to create Synthizer context"); let context = syz::Context::new().expect("Failed to create Synthizer context");
let defaults = SynthizerDefaults { let defaults = SynthizerDefaults {
panner_strategy: context.default_panner_strategy().get().unwrap(), 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(), closeness_boost_distance: context.default_closeness_boost_distance().get().unwrap(),
}; };
context.enable_events().expect("Failed to enable events"); context.enable_events().expect("Failed to enable events");
let context = Context(context);
app.add_asset::<Buffer>() app.add_asset::<Buffer>()
.init_asset_loader::<BufferAssetLoader>() .init_asset_loader::<BufferAssetLoader>()
.register_type::<Listener>() .register_type::<Listener>()