Create AbstractContainer so that Shelf and Larder can use the same design

This commit is contained in:
gregf36665 2024-12-22 23:06:26 +00:00 committed by Syndromeramo
parent af02250f88
commit 90be6cff96
2 changed files with 149 additions and 123 deletions

View file

@ -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()
}
}
}

View file

@ -12,25 +12,7 @@ import kotlin.math.min
class ShelfListener : InteractionListener { class ShelfListener : InteractionListener {
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 { 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,9 +35,17 @@ 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} }
"Teapot" -> {
"Teapot" to Items.TEAPOT_7714
}
else -> {
name to itemId
}
} }
} + listOf("Pie dish" to Items.PIE_DISH_2313) } + listOf("Pie dish" to Items.PIE_DISH_2313)
@ -65,9 +55,17 @@ 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 } }
"Teapot" -> {
"Teapot" to Items.TEAPOT_7726
}
else -> {
name to itemId
}
} }
} + listOf("Chef's Hat" to Items.CHEFS_HAT_1949) } + listOf("Chef's Hat" to Items.CHEFS_HAT_1949)
@ -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()
}
}
}
} }