mirror of
https://gitlab.com/2009scape/2009scape.git
synced 2025-12-09 16:45:44 -07:00
Create AbstractContainer so that Shelf and Larder can use the same design
This commit is contained in:
parent
af02250f88
commit
90be6cff96
2 changed files with 149 additions and 123 deletions
|
|
@ -0,0 +1,79 @@
|
||||||
|
package content.global.skill.construction.decoration.kitchen
|
||||||
|
|
||||||
|
import core.api.addItem
|
||||||
|
import core.api.hasSpaceFor
|
||||||
|
import core.api.sendDialogue
|
||||||
|
import core.game.dialogue.DialogueFile
|
||||||
|
import core.game.dialogue.Topic
|
||||||
|
import core.game.node.entity.player.Player
|
||||||
|
import core.tools.END_DIALOGUE
|
||||||
|
|
||||||
|
abstract class AbstractContainer(containerId: Int, containers: Map<Int,List<Pair<String, Int>>>, private val containerName: String) : DialogueFile() {
|
||||||
|
|
||||||
|
private val container = containers[containerId]!!
|
||||||
|
private val containerItemsNames = container.map { it.first }
|
||||||
|
private val containerItemsIds = container.map { it.second }
|
||||||
|
private val size = container.size
|
||||||
|
private val itemsPerPage = 4
|
||||||
|
|
||||||
|
private fun checkItem(player: Player, idx : Int){
|
||||||
|
val item = containerItemsIds[idx]
|
||||||
|
if (hasSpaceFor(player, item, 1)){
|
||||||
|
addItem(player, item)
|
||||||
|
end()
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
sendDialogue(player, "You need at least one free inventory space to take from the shelves.").also { stage = END_DIALOGUE }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createTopics(page: Int): MutableList<Topic<Int>> {
|
||||||
|
val startIndex = (page - 1) * itemsPerPage
|
||||||
|
val endIndex = (startIndex + itemsPerPage).coerceAtMost(size)
|
||||||
|
|
||||||
|
val topics = containerItemsNames.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 < size || page > 1) {
|
||||||
|
topics.add(Topic("More", endIndex + 1, skipPlayer = true))
|
||||||
|
}
|
||||||
|
|
||||||
|
return topics
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
override fun handle(componentID: Int, buttonID: Int) {
|
||||||
|
if (player!!.inventory.freeSlot() < 1) {
|
||||||
|
sendDialogue(player!!, "You need at least one free inventory space to take from the $containerName.").also { stage = END_DIALOGUE }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val itemsPerPage = 4
|
||||||
|
|
||||||
|
when (stage) {
|
||||||
|
0, size + 1 -> { // Initial stage or final more button clicked
|
||||||
|
val topics = createTopics(1)
|
||||||
|
showTopics(*topics.toTypedArray()).also { stage = 1 }
|
||||||
|
}
|
||||||
|
in 1..size -> {
|
||||||
|
val currentPage = (stage-1) / itemsPerPage
|
||||||
|
val startIndex = currentPage * itemsPerPage
|
||||||
|
|
||||||
|
if (buttonID == itemsPerPage + 1) { // "More" button clicked (but not the last one)
|
||||||
|
val topics = createTopics(currentPage + 1)
|
||||||
|
showTopics(*topics.toTypedArray()).also { stage = currentPage*itemsPerPage + 1 }
|
||||||
|
} else { // Handle item selection
|
||||||
|
val itemIndex = startIndex + (buttonID - 1)
|
||||||
|
if (itemIndex < size) {
|
||||||
|
checkItem(player!!, itemIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -12,25 +12,7 @@ import kotlin.math.min
|
||||||
|
|
||||||
|
|
||||||
class ShelfListener : InteractionListener {
|
class ShelfListener : InteractionListener {
|
||||||
|
companion object {
|
||||||
override fun defineListeners() {
|
|
||||||
on(ShelfDialogue.shelves.keys.toIntArray(), IntType.SCENERY, "Search") {player, node ->
|
|
||||||
searchShelf(player, node.id)
|
|
||||||
return@on true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun searchShelf(player : Player, shelfId: Int){
|
|
||||||
openDialogue(player, ShelfDialogue(shelfId))
|
|
||||||
}
|
|
||||||
|
|
||||||
class ShelfDialogue(shelfId: Int) : DialogueFile() {
|
|
||||||
|
|
||||||
companion object{
|
|
||||||
|
|
||||||
const val NEXT = 5
|
|
||||||
const val PREV = -5
|
|
||||||
|
|
||||||
// Source for text https://www.youtube.com/watch?v=SNuqelEft3Y
|
// Source for text https://www.youtube.com/watch?v=SNuqelEft3Y
|
||||||
|
|
||||||
private val woodshelf_1_items = listOf(
|
private val woodshelf_1_items = listOf(
|
||||||
|
|
@ -53,11 +35,19 @@ class ShelfListener : InteractionListener {
|
||||||
|
|
||||||
private val oakshelf_2_items = oakshelf_1_items.map { (name, itemId) ->
|
private val oakshelf_2_items = oakshelf_1_items.map { (name, itemId) ->
|
||||||
when (name) {
|
when (name) {
|
||||||
"Cup" -> { "Porcelain cup" to Items.PORCELAIN_CUP_7732 }
|
"Cup" -> {
|
||||||
"Teapot" -> { "Teapot" to Items.TEAPOT_7714 }
|
"Porcelain cup" to Items.PORCELAIN_CUP_7732
|
||||||
else -> { name to itemId}
|
|
||||||
}
|
}
|
||||||
} + listOf( "Pie dish" to Items.PIE_DISH_2313 )
|
|
||||||
|
"Teapot" -> {
|
||||||
|
"Teapot" to Items.TEAPOT_7714
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
name to itemId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} + listOf("Pie dish" to Items.PIE_DISH_2313)
|
||||||
|
|
||||||
private val teakshelf_1_items = oakshelf_2_items + listOf(
|
private val teakshelf_1_items = oakshelf_2_items + listOf(
|
||||||
"Pot" to Items.EMPTY_POT_1931
|
"Pot" to Items.EMPTY_POT_1931
|
||||||
|
|
@ -65,11 +55,19 @@ class ShelfListener : InteractionListener {
|
||||||
|
|
||||||
private val teakshelf_2_items = teakshelf_1_items.map { (name, itemId) ->
|
private val teakshelf_2_items = teakshelf_1_items.map { (name, itemId) ->
|
||||||
when (name) {
|
when (name) {
|
||||||
"Porcelain cup" -> { "Porcelain cup" to Items.PORCELAIN_CUP_7735 }
|
"Porcelain cup" -> {
|
||||||
"Teapot" -> { "Teapot" to Items.TEAPOT_7726 }
|
"Porcelain cup" to Items.PORCELAIN_CUP_7735
|
||||||
else -> { name to itemId }
|
|
||||||
}
|
}
|
||||||
} + listOf( "Chef's Hat" to Items.CHEFS_HAT_1949 )
|
|
||||||
|
"Teapot" -> {
|
||||||
|
"Teapot" to Items.TEAPOT_7726
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
name to itemId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} + listOf("Chef's Hat" to Items.CHEFS_HAT_1949)
|
||||||
|
|
||||||
val shelves = mapOf(
|
val shelves = mapOf(
|
||||||
Scenery.SHELVES_13545 to woodshelf_1_items,
|
Scenery.SHELVES_13545 to woodshelf_1_items,
|
||||||
|
|
@ -82,68 +80,17 @@ class ShelfListener : InteractionListener {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val shelf = shelves[shelfId]!!
|
override fun defineListeners() {
|
||||||
private val shelfItemsNames = shelf.map { it.first }
|
on(shelves.keys.toIntArray(), IntType.SCENERY, "Search") { player, node ->
|
||||||
private val shelfItemsIds = shelf.map { it.second }
|
searchShelf(player, node.id)
|
||||||
private val size = shelf.size
|
return@on true
|
||||||
|
|
||||||
private fun checkItem(player: Player, idx : Int){
|
|
||||||
val item = shelfItemsIds[idx]
|
|
||||||
if (hasSpaceFor(player, item, 1)){
|
|
||||||
addItem(player, item)
|
|
||||||
end()
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
sendDialogue(player, "You need at least one free inventory space to take from the shelves.").also { stage = END_DIALOGUE }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createTopics(page: Int, itemsPerPage: Int): MutableList<Topic<Int>> {
|
private fun searchShelf(player : Player, shelfId: Int){
|
||||||
val startIndex = (page - 1) * itemsPerPage
|
openDialogue(player, ShelfDialogue(shelfId, shelves))
|
||||||
val endIndex = (startIndex + itemsPerPage).coerceAtMost(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 < size || page > 1) {
|
|
||||||
topics.add(Topic("More", endIndex + 1, skipPlayer = true))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return topics
|
class ShelfDialogue(shelfId: Int, shelves : Map<Int,List<Pair<String,Int>>>) : AbstractContainer(shelfId, shelves, "shelf")
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
override fun handle(componentID: Int, buttonID: Int) {
|
|
||||||
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
|
|
||||||
|
|
||||||
when (stage) {
|
|
||||||
0, size + 1 -> { // Initial stage or final more button clicked
|
|
||||||
val topics = createTopics(1, itemsPerPage)
|
|
||||||
showTopics(*topics.toTypedArray()).also { stage = 1 }
|
|
||||||
}
|
|
||||||
in 1..size -> {
|
|
||||||
val currentPage = (stage-1) / itemsPerPage
|
|
||||||
val startIndex = currentPage * itemsPerPage
|
|
||||||
|
|
||||||
if (buttonID == itemsPerPage + 1) { // "More" button clicked (but not the last one)
|
|
||||||
val topics = createTopics(currentPage + 1, itemsPerPage)
|
|
||||||
showTopics(*topics.toTypedArray()).also { stage = currentPage*itemsPerPage + 1 }
|
|
||||||
} else { // Handle item selection
|
|
||||||
val itemIndex = startIndex + (buttonID - 1)
|
|
||||||
if (itemIndex < size) {
|
|
||||||
checkItem(player!!, itemIndex)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> end()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue