Non-Intrusive refactor of play_queued_audio_system() (#10910)

# Objective

- Improve readability.
- Somewhat relates to #10896.

## Solution

- Use early returns to minimize nesting.
- Change `emitter_translation` to use `if let` instead of `map`.
This commit is contained in:
Mateusz Wachowiak 2023-12-09 15:27:39 +01:00 committed by GitHub
parent 24060213b5
commit 1523e8c409
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -122,7 +122,9 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
}; };
for (entity, source_handle, settings, maybe_emitter_transform) in &query_nonplaying { for (entity, source_handle, settings, maybe_emitter_transform) in &query_nonplaying {
if let Some(audio_source) = audio_sources.get(source_handle) { let Some(audio_source) = audio_sources.get(source_handle) else {
continue;
};
// audio data is available (has loaded), begin playback and insert sink component // audio data is available (has loaded), begin playback and insert sink component
if settings.spatial { if settings.spatial {
let (left_ear, right_ear) = ear_positions.get(); let (left_ear, right_ear) = ear_positions.get();
@ -136,30 +138,36 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
); );
} }
let emitter_translation = maybe_emitter_transform let emitter_translation = if let Some(emitter_transform) = maybe_emitter_transform {
.map(|t| (t.translation() * ear_positions.scale.0).into()) (emitter_transform.translation() * ear_positions.scale.0).into()
.unwrap_or_else(|| { } else {
warn!("Spatial AudioBundle with no GlobalTransform component. Using zero."); warn!("Spatial AudioBundle with no GlobalTransform component. Using zero.");
Vec3::ZERO.into() Vec3::ZERO.into()
}); };
match SpatialSink::try_new( let sink = match SpatialSink::try_new(
stream_handle, stream_handle,
emitter_translation, emitter_translation,
left_ear.into(), left_ear.into(),
right_ear.into(), right_ear.into(),
) { ) {
Ok(sink) => { Ok(sink) => sink,
Err(err) => {
warn!("Error creating spatial sink: {err:?}");
continue;
}
};
sink.set_speed(settings.speed); sink.set_speed(settings.speed);
match settings.volume { match settings.volume {
Volume::Relative(vol) => { Volume::Relative(vol) => sink.set_volume(vol.0 * global_volume.volume.0),
sink.set_volume(vol.0 * global_volume.volume.0);
}
Volume::Absolute(vol) => sink.set_volume(vol.0), Volume::Absolute(vol) => sink.set_volume(vol.0),
} }
if settings.paused { if settings.paused {
sink.pause(); sink.pause();
} }
match settings.mode { match settings.mode {
PlaybackMode::Loop => { PlaybackMode::Loop => {
sink.append(audio_source.decoder().repeat_infinite()); sink.append(audio_source.decoder().repeat_infinite());
@ -184,24 +192,25 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
.insert((SpatialAudioSink { sink }, PlaybackRemoveMarker)); .insert((SpatialAudioSink { sink }, PlaybackRemoveMarker));
} }
}; };
}
Err(err) => {
warn!("Error playing spatial sound: {err:?}");
}
}
} else { } else {
match Sink::try_new(stream_handle) { let sink = match Sink::try_new(stream_handle) {
Ok(sink) => { Ok(sink) => sink,
Err(err) => {
warn!("Error creating sink: {err:?}");
continue;
}
};
sink.set_speed(settings.speed); sink.set_speed(settings.speed);
match settings.volume { match settings.volume {
Volume::Relative(vol) => { Volume::Relative(vol) => sink.set_volume(vol.0 * global_volume.volume.0),
sink.set_volume(vol.0 * global_volume.volume.0);
}
Volume::Absolute(vol) => sink.set_volume(vol.0), Volume::Absolute(vol) => sink.set_volume(vol.0),
} }
if settings.paused { if settings.paused {
sink.pause(); sink.pause();
} }
match settings.mode { match settings.mode {
PlaybackMode::Loop => { PlaybackMode::Loop => {
sink.append(audio_source.decoder().repeat_infinite()); sink.append(audio_source.decoder().repeat_infinite());
@ -227,12 +236,6 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
} }
}; };
} }
Err(err) => {
warn!("Error playing sound: {err:?}");
}
}
}
}
} }
} }