From 37c7793ad2b2ca25d050adb3d99ddfcadbe415ea Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Fri, 5 Aug 2022 17:53:48 -0500 Subject: [PATCH] If a sound doesn't have a source, create a default. --- src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index e6aa50f..ce56ae4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -282,6 +282,33 @@ fn add_generator( } } +fn add_sound_without_source( + mut commands: Commands, + query: Query<(Entity, Option<&Parent>), (Added, Without)>, + parents: Query<&Parent>, + sources: Query<&Source>, +) { + for (entity, parent) in &query { + let source = if let Some(parent) = parent { + let mut parent: Option<&Parent> = Some(parent); + let mut target = None; + while let Some(p) = parent { + if sources.get(**p).is_ok() { + target = Some(**p); + break; + } + parent = parents.get(**p).ok(); + } + target.map(|v| sources.get(v).unwrap()) + } else { + None + }; + if source.is_none() { + commands.entity(entity).insert(Source::default()); + } + } +} + #[derive(Default, Deref, DerefMut)] struct LastBuffer(HashMap>); @@ -661,6 +688,7 @@ impl Plugin for SynthizerPlugin { CoreStage::PostUpdate, swap_buffers.before(SynthizerSystems::UpdateHandles), ) + .add_system_to_stage(CoreStage::PostUpdate, add_sound_without_source) .add_system_to_stage( CoreStage::PostUpdate, change_panner_strategy.before(SynthizerSystems::UpdateHandles),