working storage chest impl

This commit is contained in:
minjaesong
2021-03-16 15:32:22 +09:00
parent 0fa889bc55
commit 953e44c8d7
12 changed files with 59 additions and 44 deletions

View File

@@ -738,7 +738,7 @@ public class AppLoader implements ApplicationListener {
textureWhiteCircle.dispose();
logo.getTexture().dispose();
disposableSingletonsPool.forEach((it) -> {try { it.dispose(); } catch (GdxRuntimeException e) {}});
disposableSingletonsPool.forEach((it) -> {try { it.dispose(); } catch (IllegalArgumentException e) {}});
ModMgr.INSTANCE.disposeMods();

View File

@@ -41,8 +41,8 @@ class UIItemInventoryElem(
override var quickslot: Int? = null,
override var equippedSlot: Int? = null,
val drawBackOnNull: Boolean = true,
keyDownFun: (GameItem?, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
keyDownFun: (GameItem?, Int, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int) -> Unit
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun) {
companion object {

View File

@@ -38,8 +38,8 @@ class UIItemInventoryElemSimple(
override var quickslot: Int? = null,
override var equippedSlot: Int? = null,
val drawBackOnNull: Boolean = true,
keyDownFun: (GameItem?, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
keyDownFun: (GameItem?, Int, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int) -> Unit
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun) {
companion object {

View File

@@ -1071,7 +1071,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
it.handler.dispose()
it.dispose()
}
catch (e: GdxRuntimeException) {}
catch (e: IllegalArgumentException) {}
}
super.dispose()

View File

@@ -32,16 +32,13 @@ class ActorInventory(@Transient val actor: Pocketed, maxCapacity: Int, capacityM
val quickSlot = Array<ItemID?>(UIQuickslotBar.SLOT_COUNT) { null } // 0: Slot 1, 9: Slot 10
fun remove(itemID: ItemID, count: Int) = remove(ItemCodex[itemID]!!, count)
override fun remove(itemID: ItemID, count: Int) = remove(ItemCodex[itemID]!!, count)
/** Will check existence of the item using its Dynamic ID; careful with command order!
* e.g. re-assign after this operation */
fun remove(item: GameItem, count: Int = 1) {
override fun remove(item: GameItem, count: Int) {
super.remove(item, count) { existingItem ->
// unequip, if applicable
actor.unequipItem(existingItem.item)
// depleted item; remove entry from inventory
itemList.remove(existingItem)
// also unequip on the quickslot
actor.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
setQuickBar(it, null)

View File

@@ -69,6 +69,9 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
insertionSortLastElem(itemList)
}
open fun remove(itemID: ItemID, count: Int) = remove(ItemCodex[itemID]!!, count) {}
open fun remove(item: GameItem, count: Int = 1) = remove(item, count) {}
open fun remove(itemID: ItemID, count: Int, unequipFun: (InventoryPair) -> Unit) =
remove(ItemCodex[itemID]!!, count, unequipFun)
/** Will check existence of the item using its Dynamic ID; careful with command order!
@@ -97,6 +100,9 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
existingItem.amount = newCount
}
else {
// depleted item; remove entry from inventory
itemList.remove(existingItem)
// do additional removal job (e.g. unequipping)
unequipFun(existingItem)
}
}

View File

@@ -8,6 +8,7 @@ import net.torvald.terrarum.*
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory.Companion.CAPACITY_MODE_COUNT
import net.torvald.terrarum.modulebasegame.ui.HasInventory
import net.torvald.terrarum.modulebasegame.ui.InventoryNegotiator
@@ -64,24 +65,20 @@ internal class UIStorageChest : UICanvas(), HasInventory {
private val shapeRenderer = ShapeRenderer()
private val negotiator = object : InventoryNegotiator() {
override fun accept(item: GameItem, amount: Int) {
printdbg(this, "Accept")
override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Int) {
player.remove(item, amount)
fixture.add(item, amount)
}
override fun reject(item: GameItem, amount: Int) {
printdbg(this, "Reject")
override fun reject(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Int) {
fixture.remove(item, amount)
player.add(item, amount)
}
}
override fun getNegotiator() = negotiator
override fun getFixtureInventory() {
TODO("Not yet implemented")
}
override fun getPlayerInventory() {
TODO("Not yet implemented")
}
override fun getFixtureInventory(): FixtureInventory = chest
override fun getPlayerInventory(): FixtureInventory = Terrarum.ingame!!.actorNowPlaying!!.inventory
private lateinit var catBar: UIItemInventoryCatBar
private lateinit var itemListChest: UIItemInventoryItemGrid
@@ -118,8 +115,13 @@ internal class UIStorageChest : UICanvas(), HasInventory {
6, CELLS_VRT,
drawScrollOnRightside = false,
drawWallet = false,
keyDownFun = { _, _ -> Unit },
touchDownFun = { _, _, _, _, _ -> itemListUpdate() }
keyDownFun = { _, _, _ -> Unit },
touchDownFun = { gameItem, amount, _ ->
if (gameItem != null) {
negotiator.reject(chest, getPlayerInventory(), gameItem, amount)
}
itemListUpdate()
}
)
itemListPlayer = UIItemInventoryItemGrid(
this,
@@ -130,8 +132,13 @@ internal class UIStorageChest : UICanvas(), HasInventory {
6, CELLS_VRT,
drawScrollOnRightside = true,
drawWallet = false,
keyDownFun = { _, _ -> Unit },
touchDownFun = { _, _, _, _, _ -> itemListUpdate() }
keyDownFun = { _, _, _ -> Unit },
touchDownFun = { gameItem, amount, _ ->
if (gameItem != null) {
negotiator.accept(getPlayerInventory(), chest, gameItem, amount)
}
itemListUpdate()
}
)
handler.allowESCtoClose = true
@@ -142,7 +149,7 @@ internal class UIStorageChest : UICanvas(), HasInventory {
}
catBar.update(delta)
itemListChest.update(delta)
itemListPlayer.update(delta)
@@ -180,16 +187,19 @@ internal class UIStorageChest : UICanvas(), HasInventory {
override fun doOpening(delta: Float) {
Terrarum.ingame?.paused = true
(Terrarum.ingame as? TerrarumIngame)?.setTooltipMessage(null)
}
override fun doClosing(delta: Float) {
Terrarum.ingame?.paused = false
(Terrarum.ingame as? TerrarumIngame)?.setTooltipMessage(null)
}
override fun endOpening(delta: Float) {
}
override fun endClosing(delta: Float) {
(Terrarum.ingame as? TerrarumIngame)?.setTooltipMessage(null) // required!
}

View File

@@ -1,9 +1,11 @@
package net.torvald.terrarum.modulebasegame.ui
import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
interface HasInventory {
fun getNegotiator(): InventoryNegotiator
fun getFixtureInventory()
fun getPlayerInventory()
fun getFixtureInventory(): FixtureInventory
fun getPlayerInventory(): FixtureInventory
}

View File

@@ -2,6 +2,7 @@ package net.torvald.terrarum.modulebasegame.ui
import net.torvald.terrarum.UIItemInventoryCatBar.Companion.CAT_ALL
import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
/**
* Created by minjaesong on 2021-03-12.
@@ -10,7 +11,7 @@ abstract class InventoryNegotiator {
/** Retrieve item filter to be used to show only the acceptable items when player's own inventory is being displayed */
open fun getItemFilter(): List<String> = listOf(CAT_ALL) // GameItem.Category
/** Accepts item from the player and pass it to right inventory (object), slot (UI), etc... */
abstract fun accept(item: GameItem, amount: Int = 1)
abstract fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Int = 1)
/** Rejects item and perhaps returns it back to the player, or make explosion, etc... */
abstract fun reject(item: GameItem, amount: Int = 1)
abstract fun reject(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Int = 1)
}

View File

@@ -30,7 +30,7 @@ class UIInventoryFull(
override var openCloseTime: Second = 0.0f
companion object {
const val INVEN_DEBUG_MODE = true
const val INVEN_DEBUG_MODE = false
const val REQUIRED_MARGIN: Int = 138 // hard-coded value. Don't know the details. Range: [91-146]. I chose MAX-8 because cell gap is 8
const val CELLS_HOR = 10
@@ -247,7 +247,6 @@ class UIInventoryFull(
override fun endClosing(delta: Float) {
(Terrarum.ingame as? TerrarumIngame)?.setTooltipMessage(null) // required!
MinimapComposer.revalidateAll()
}

View File

@@ -26,20 +26,20 @@ abstract class UIItemInventoryCellBase(
open var itemImage: TextureRegion?,
open var quickslot: Int? = null,
open var equippedSlot: Int? = null,
val keyDownFun: (GameItem?, Int) -> Unit,
val touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
val keyDownFun: (GameItem?, Int, Int) -> Unit,
val touchDownFun: (GameItem?, Int, Int) -> Unit
) : UIItem(parentUI, initialX, initialY) {
abstract override fun update(delta: Float)
abstract override fun render(batch: SpriteBatch, camera: Camera)
override fun keyDown(keycode: Int): Boolean {
keyDownFun(item, keycode)
keyDownFun(item, amount, keycode)
super.keyDown(keycode)
return true
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
touchDownFun(item, screenX, screenY, pointer, button)
touchDownFun(item, amount, button)
super.touchDown(screenX, screenY, pointer, button)
return true
}

View File

@@ -46,8 +46,8 @@ class UIItemInventoryItemGrid(
val verticalCells: Int,
val drawScrollOnRightside: Boolean = false,
val drawWallet: Boolean = true,
keyDownFun: (GameItem?, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
keyDownFun: (GameItem?, Int, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int) -> Unit
) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position
@@ -108,8 +108,8 @@ class UIItemInventoryItemGrid(
fun getEstimatedW(horizontalCells: Int) = horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 1) * listGap
fun getEstimatedH(verticalCells: Int) = verticalCells * UIItemInventoryElemSimple.height + (verticalCells - 1) * listGap
fun createInvCellGenericKeyDownFun(): (GameItem?, Int) -> Unit {
return { item: GameItem?, keycode: Int ->
fun createInvCellGenericKeyDownFun(): (GameItem?, Int, Int) -> Unit {
return { item: GameItem?, amount: Int, keycode: Int ->
if (item != null && Terrarum.ingame != null && keycode in Input.Keys.NUM_0..Input.Keys.NUM_9) {
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
if (player != null) {
@@ -138,8 +138,8 @@ class UIItemInventoryItemGrid(
}
}
fun createInvCellGenericTouchDownFun(listRebuildFun: () -> Unit): (GameItem?, Int, Int, Int, Int) -> Unit {
return { item: GameItem?, screenX: Int, screenY: Int, pointer: Int, button: Int ->
fun createInvCellGenericTouchDownFun(listRebuildFun: () -> Unit): (GameItem?, Int, Int) -> Unit {
return { item: GameItem?, amount: Int, button: Int ->
if (item != null && Terrarum.ingame != null) {
// equip da shit
val itemEquipSlot = item.equipPosition