First draft at a procedural More options

This commit is contained in:
gregf36665 2024-12-22 20:05:42 +00:00 committed by Syndromeramo
parent 655d5f0e06
commit 932f1770ff

View file

@ -24,7 +24,7 @@ class ShelfListener : InteractionListener {
openDialogue(player, ShelfDialogue(shelfId, 0)) openDialogue(player, ShelfDialogue(shelfId, 0))
} }
class ShelfDialogue(val shelfId: Int, val offset: Int) : DialogueFile() { class ShelfDialogue(shelfId: Int, val offset: Int) : DialogueFile() {
companion object{ companion object{
@ -46,10 +46,7 @@ class ShelfListener : InteractionListener {
) )
private val oakshelf_1_items = woodshelf_2_items + listOf( private val oakshelf_1_items = woodshelf_2_items + listOf(
"More Options" to NEXT,
"Cake tin" to Items.CAKE_TIN_1887,
"Bowl" to Items.BOWL_1923, "Bowl" to Items.BOWL_1923,
"More Options" to PREV
) )
val shelves = mapOf( val shelves = mapOf(
@ -60,17 +57,11 @@ class ShelfListener : InteractionListener {
) )
} }
val end = min(shelves[shelfId]!!.size, 5) private val shelfItemsNames = shelves[shelfId]!!.map { it.first }
val shelfItems = shelves[shelfId]!!.slice(offset until end) private val shelfItemsIds = shelves[shelfId]!!.map { it.second }
val shelfItemsNames = shelfItems.map { it.first }
val shelfItemsIds = shelfItems.map { it.second }
fun checkItem(player: Player, shelfId: Int, offset: Int, button : Int){ private fun checkItem(player: Player, idx : Int){
val item = shelfItemsIds[button] val item = shelfItemsIds[idx]
if (item < 1000){
openDialogue(player, ShelfDialogue(shelfId, offset+item))
}
else{
if (hasSpaceFor(player, item, 1)){ if (hasSpaceFor(player, item, 1)){
addItem(player, item) addItem(player, item)
end() end()
@ -80,15 +71,56 @@ class ShelfListener : InteractionListener {
} }
} }
private fun createTopics(page: Int, itemsPerPage: Int): MutableList<Topic<Int>> {
val startIndex = (page - 1) * itemsPerPage
val endIndex = (startIndex + itemsPerPage).coerceAtMost(shelfItemsNames.size)
val topics = shelfItemsNames.subList(startIndex, endIndex)
.mapIndexed { index, item -> Topic(item, startIndex + index + 1, skipPlayer = true) }
.toMutableList()
// Add "More" button if there's another page or to loop back
if (endIndex < shelfItemsNames.size || page > 1) {
topics.add(Topic("More", endIndex + 1, skipPlayer = true))
} }
return topics
}
override fun handle(componentID: Int, buttonID: Int) { override fun handle(componentID: Int, buttonID: Int) {
println("Stage: $stage") if (player!!.inventory.freeSlot() < 1) {
when(stage){ sendDialogue(player!!, "You need at least one free inventory space to take from the shelves.").also { stage = END_DIALOGUE }
0 -> showTopics( return
*shelfItemsNames.toTypedArray().mapIndexed { index, item -> Topic(item, index+1, skipPlayer = true) }.toTypedArray() }
) val itemsPerPage = 4
in 1 .. 5 -> checkItem(player!!, shelfId, offset, stage - 1).also { stage = 6 } val totalPages = (shelfItemsNames.size + itemsPerPage - 1) / itemsPerPage // Total number of pages
when (stage) {
0, shelfItemsNames.size + 1 -> { // Initial stage or final more button clicked
val topics = createTopics(1, itemsPerPage)
showTopics(*topics.toTypedArray()).also { stage = 1 }
}
in 1..shelfItemsNames.size -> { // Specific stages for each page
val currentPage = stage / (itemsPerPage + 1)
val startIndex = currentPage * itemsPerPage
if (buttonID == itemsPerPage + 1) { // "More" button clicked
if (currentPage < totalPages) {
val topics = createTopics(currentPage + 1, itemsPerPage)
showTopics(*topics.toTypedArray()).also { stage = currentPage*4 + 1 }
} else { // Last page, loop back to the first page
val topics = createTopics(1, itemsPerPage)
showTopics(*topics.toTypedArray()).also { stage = 1 }
}
} else { // Handle item selection
val itemIndex = startIndex + (buttonID - 1)
if (itemIndex < shelfItemsNames.size) {
checkItem(player!!, itemIndex)
}
}
}
else -> end() else -> end()
} }
} }