mirror of
https://gitlab.com/2009scape/2009scape.git
synced 2025-12-09 16:45:44 -07:00
First draft at a procedural More options
This commit is contained in:
parent
655d5f0e06
commit
932f1770ff
1 changed files with 58 additions and 26 deletions
|
|
@ -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) {
|
||||||
|
sendDialogue(player!!, "You need at least one free inventory space to take from the shelves.").also { stage = END_DIALOGUE }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val itemsPerPage = 4
|
||||||
|
val totalPages = (shelfItemsNames.size + itemsPerPage - 1) / itemsPerPage // Total number of pages
|
||||||
|
|
||||||
when (stage) {
|
when (stage) {
|
||||||
0 -> showTopics(
|
0, shelfItemsNames.size + 1 -> { // Initial stage or final more button clicked
|
||||||
*shelfItemsNames.toTypedArray().mapIndexed { index, item -> Topic(item, index+1, skipPlayer = true) }.toTypedArray()
|
val topics = createTopics(1, itemsPerPage)
|
||||||
)
|
showTopics(*topics.toTypedArray()).also { stage = 1 }
|
||||||
in 1 .. 5 -> checkItem(player!!, shelfId, offset, stage - 1).also { stage = 6 }
|
}
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue