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>"]
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 }
bevy = { version = "0.9", default-features = true }

View File

@ -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<HandleUntyped>,
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()
});
},
));
}
}

View File

@ -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<syz::Context>,
context: ResMut<Context>,
listener: Query<Option<&GlobalTransform>, With<Listener>>,
) {
if let Ok(transform) = listener.get_single() {
@ -192,7 +195,7 @@ fn update_listener(
}
fn add_source_handle(
context: Res<syz::Context>,
context: Res<Context>,
mut query: Query<(
&mut Source,
Option<&PannerStrategy>,
@ -241,7 +244,7 @@ fn add_source_handle(
}
fn add_generator(
context: Res<syz::Context>,
context: Res<Context>,
buffers: Res<Assets<Buffer>>,
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<Entity, Handle<Buffer>>);
fn swap_buffers(
@ -354,7 +357,7 @@ fn change_panner_strategy(
}
fn update_source_properties(
context: Res<syz::Context>,
context: Res<Context>,
mut query: Query<(
&mut Source,
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 default_panner_strategy: Option<syz::PannerStrategy>,
pub default_distance_model: Option<syz::DistanceModel>,
@ -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<syz::Context>,
context: Res<Context>,
config: Res<SynthizerConfig>,
defaults: Res<SynthizerDefaults>,
) {
@ -639,7 +642,7 @@ fn sync_config(
}
fn events(
context: Res<syz::Context>,
context: Res<Context>,
sounds: Query<(Entity, &Sound)>,
mut output: EventWriter<SynthizerEvent>,
) {
@ -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::<Buffer>()
.init_asset_loader::<BufferAssetLoader>()
.register_type::<Listener>()