From 726a0337c5d1bb423d36d03c9fa9a6895fe9062e Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 30 Jul 2023 10:17:21 +0800 Subject: [PATCH] feat: drop-out for drum-like instruments --- .../quaedam/projection/music/Composer.kt | 22 ++++++++++++++++--- .../quaedam/projection/music/MusicPlayer.kt | 8 +++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/common/src/main/kotlin/quaedam/projection/music/Composer.kt b/common/src/main/kotlin/quaedam/projection/music/Composer.kt index 0fb2881..5432a2f 100644 --- a/common/src/main/kotlin/quaedam/projection/music/Composer.kt +++ b/common/src/main/kotlin/quaedam/projection/music/Composer.kt @@ -1,5 +1,6 @@ package quaedam.projection.music +import net.minecraft.world.level.block.state.properties.NoteBlockInstrument import kotlin.math.abs import kotlin.random.Random import kotlin.random.nextInt @@ -8,17 +9,32 @@ import kotlin.random.nextInt * The composer for music. * rhythmRandom is used for a better rhythm sync between different instruments. */ -class Composer(val noteRandom: Random, val rhythmRandom: Random) { +class Composer(val noteRandom: Random, val rhythmRandom: Random, val instrument: NoteBlockInstrument) { data class Note(val note: Int, val volume: Float, val time: Int) val baseTime = arrayOf(5, 5, 3, 3, 4, 4, 2, 2, 8).random(rhythmRandom) val baseNote = noteRandom.nextInt(5..19) - fun composeMusic() = decorate( - (0..rhythmRandom.nextInt(5)).flatMap { composeSection() } + val mayDropOut = instrument in arrayOf( + NoteBlockInstrument.BASEDRUM, + NoteBlockInstrument.HAT, + NoteBlockInstrument.SNARE, ) + fun composeMusic(): List { + var note = (0..rhythmRandom.nextInt(4)).flatMap { composeSection() } + note = decorate(note) + if (mayDropOut && rhythmRandom.nextInt(3) != 0) { + val dropRate = rhythmRandom.nextInt(2..4) + note = note.chunked(dropRate).map { + val first = it.first() + Note(first.note, first.volume, it.sumOf { note -> note.time }) + } + } + return note + } + fun decorate(notes: List) = notes.map { if (noteRandom.nextInt(4) == 0) { doDecorate(it) diff --git a/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt b/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt index 04a6f55..13d8748 100644 --- a/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt +++ b/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt @@ -31,14 +31,18 @@ class MusicPlayer(val seed: Long, val level: Level, val pos: BlockPos, val start tag.getLong(TAG_STARTED_AT) ) - var notes = Composer(Random(seed), Random(startedAt / 20 * 15)).composeMusic().toMutableList() + var notes = Composer( + noteRandom = Random(seed), + rhythmRandom = Random(startedAt / 20 * 15), + instrument = level.getBlockState(pos).getValue(BlockStateProperties.NOTEBLOCK_INSTRUMENT) + ).composeMusic().toMutableList() val totalTime = notes.sumOf { it.time }.toLong() var remainingTime = totalTime val isEnd get() = remainingTime <= 0 || notes.isEmpty() var noteTime = 0 init { - var currentRemaining = totalTime - (level.gameTime - startedAt) + val currentRemaining = totalTime - (level.gameTime - startedAt) while (remainingTime > currentRemaining) { // seek to current position remainingTime -= fetchNote().time