From 52aec0fd9338c77ec67f2cdfe897f622c3f1a9d1 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 4 Jan 2024 19:21:04 +0900 Subject: [PATCH] queueing prev/specific song will keep the internal playlist shuffled --- .../modulebasegame/TerrarumMusicGovernor.kt | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt b/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt index a45e1c235..2ad280149 100644 --- a/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt +++ b/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt @@ -228,8 +228,16 @@ class TerrarumMusicGovernor : MusicGovernor() { if (indexAtMusicBin < 0) throw IllegalArgumentException("The music does not exist on the internal songs list ($music)") // rewrite musicBin - // TODO take shuffing into account - val newMusicBin = Array(songs.size - indexAtMusicBin) { offset -> + val newMusicBin = if (shuffled) songs.shuffled().toTypedArray().also { + // if shuffled, + // 1. create a shuffled version of songlist + // 2. swap two songs such that the songs[indexAtMusicBin] comes first + val swapTo = it.indexOf(songs[indexAtMusicBin]) + val tmp = it[swapTo] + it[swapTo] = it[0] + it[0] = tmp + } + else Array(songs.size - indexAtMusicBin) { offset -> val k = offset + indexAtMusicBin songs[k] } @@ -241,8 +249,16 @@ class TerrarumMusicGovernor : MusicGovernor() { if (indexAtMusicBin !in songs.indices) throw IndexOutOfBoundsException("The index is outside of the internal songs list ($indexAtMusicBin/${songs.size})") // rewrite musicBin - // TODO take shuffing into account - val newMusicBin = Array(songs.size - indexAtMusicBin) { offset -> + val newMusicBin = if (shuffled) songs.shuffled().toTypedArray().also { + // if shuffled, + // 1. create a shuffled version of songlist + // 2. swap two songs such that the songs[indexAtMusicBin] comes first + val swapTo = it.indexOf(songs[indexAtMusicBin]) + val tmp = it[swapTo] + it[swapTo] = it[0] + it[0] = tmp + } + else Array(songs.size - indexAtMusicBin) { offset -> val k = offset + indexAtMusicBin songs[k] }