audio: check after queue again if sound can be skipped

This should avoid more "No source available" errors
This commit is contained in:
Bixilon 2023-02-05 21:17:00 +01:00
parent 610494473d
commit d4f065b356
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -180,18 +180,24 @@ class AudioPlayer(
return source
}
private fun shouldPlay(sound: Sound, position: Vec3?): Boolean {
if (position == null) return true
val distance = (this.listener.position - position).length2()
if (distance >= sound.attenuationDistance * sound.attenuationDistance) {
return false
}
return true
}
private fun playSound(sound: Sound, position: Vec3? = null, volume: Float = 1.0f, pitch: Float = 1.0f) {
if (!profile.enabled) {
return
}
position?.let {
val distance = (this.listener.position - it).length()
if (distance >= sound.attenuationDistance) {
return
}
}
position?.let { if (!shouldPlay(sound, position)) return }
queue += add@{
sound.load(connection.assetsManager)
position?.let { if (!shouldPlay(sound, position)) return@add }
val source = getAvailableSource()
if (source == null) {
Log.log(LogMessageType.AUDIO, LogLevels.WARN) { "No source available: $sound" }