storage chest ui to display its name and player encumbrance meter

This commit is contained in:
minjaesong
2021-03-16 17:37:52 +09:00
parent 5f7db4cf19
commit bae438c2df
11 changed files with 89 additions and 28 deletions

View File

@@ -6,6 +6,10 @@ import net.torvald.terrarum.utils.JsonFetcher
import java.io.File
import java.util.*
class LangObject(val key: String, val fromLang: Boolean) {
fun get() = if (fromLang) Lang[key] else key
}
/**
* Created by minjaesong on 2016-01-22.
*/

View File

@@ -3,6 +3,7 @@ package net.torvald.terrarum.modulebasegame.console
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.console.ConsoleCommand
import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.gameactors.FixtureTikiTorch
/**
@@ -10,7 +11,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureTikiTorch
*/
internal object SpawnTikiTorch : ConsoleCommand {
override fun execute(args: Array<String>) {
val torch = FixtureTikiTorch()
val torch = FixtureTikiTorch { "Tiki Torch" }
torch.setPosition(Terrarum.mouseX, Terrarum.mouseY)
Terrarum.ingame!!.addNewActor(torch)

View File

@@ -2,6 +2,9 @@ package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Pixmap
import net.torvald.EMDASH
import net.torvald.terrarum.ccC
import net.torvald.terrarum.ccW
import net.torvald.terrarum.gameworld.toUint
import java.io.File
import java.nio.charset.Charset
@@ -171,6 +174,6 @@ object DecodeTapestry {
readCounter++
}
return TapestryObject(outImageData, artName, authorName)
return TapestryObject(outImageData, artName, authorName) { "$ccW$authorName, $ccC$artName" }
}
}

View File

@@ -19,6 +19,7 @@ open class FixtureBase(
blockBox0: BlockBox,
val blockBoxProps: BlockBoxProps = BlockBoxProps(0),
renderOrder: RenderOrder = RenderOrder.MIDDLE,
val nameFun: () -> String,
val mainUI: UICanvas? = null,
val inventory: FixtureInventory? = null
// disabling physics (not allowing the fixture to move) WILL make things easier in many ways

View File

@@ -8,22 +8,22 @@ 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.langpack.Lang
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
import net.torvald.terrarum.modulebasegame.ui.*
import net.torvald.terrarum.modulebasegame.ui.UIInventoryCells.Companion.weightBarWidth
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.CELLS_HOR
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.CELLS_VRT
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVENTORY_CELLS_OFFSET_X
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVENTORY_CELLS_OFFSET_Y
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.catBarWidth
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.controlHelpHeight
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.gradEndCol
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.gradHeight
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.gradStartCol
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalHeight
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalWidth
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryEquippedView
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.listGap
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
@@ -31,14 +31,16 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2019-07-08.
*/
internal class FixtureStorageChest : FixtureBase(
internal class FixtureStorageChest(nameFun: () -> String) : FixtureBase(
BlockBox(BlockBox.ALLOW_MOVE_DOWN, 1, 1),
inventory = FixtureInventory(40, CAPACITY_MODE_COUNT),
mainUI = UIStorageChest()
mainUI = UIStorageChest(),
nameFun = nameFun
) {
init {
(mainUI as UIStorageChest).chest = this.inventory!!
(mainUI as UIStorageChest).chestInventory = this.inventory!!
(mainUI as UIStorageChest).chestNameFun = this.nameFun
setHitboxDimension(16, 16, 0, 0)
@@ -56,7 +58,8 @@ internal class FixtureStorageChest : FixtureBase(
internal class UIStorageChest : UICanvas(), HasInventory {
lateinit var chest: FixtureInventory
lateinit var chestInventory: FixtureInventory
lateinit var chestNameFun: () -> String
override var width = AppLoader.screenW
override var height = AppLoader.screenH
@@ -77,13 +80,16 @@ internal class UIStorageChest : UICanvas(), HasInventory {
}
override fun getNegotiator() = negotiator
override fun getFixtureInventory(): FixtureInventory = chest
override fun getFixtureInventory(): FixtureInventory = chestInventory
override fun getPlayerInventory(): FixtureInventory = Terrarum.ingame!!.actorNowPlaying!!.inventory
private lateinit var catBar: UIItemInventoryCatBar
private lateinit var itemListChest: UIItemInventoryItemGrid
private lateinit var itemListPlayer: UIItemInventoryItemGrid
private var encumbrancePerc = 0f
private var isEncumbered = false
private var halfSlotOffset = (UIItemInventoryElemSimple.height + listGap) / 2
private var initialised = false
@@ -91,6 +97,9 @@ internal class UIStorageChest : UICanvas(), HasInventory {
private fun itemListUpdate() {
itemListChest.rebuild(catBar.catIconsMeaning[catBar.selectedIcon])
itemListPlayer.rebuild(catBar.catIconsMeaning[catBar.selectedIcon])
encumbrancePerc = getPlayerInventory().capacity.toFloat() / getPlayerInventory().maxCapacity
isEncumbered = getPlayerInventory().isEncumbered
}
private fun setCompact(yes: Boolean) {
@@ -105,6 +114,8 @@ internal class UIStorageChest : UICanvas(), HasInventory {
itemListPlayer.gridModeButtons[1].highlighted = yes
itemListPlayer.itemPage = 0
itemListPlayer.rebuild(catBar.catIconsMeaning[catBar.selectedIcon])
itemListUpdate()
}
override fun updateUI(delta: Float) {
@@ -123,7 +134,7 @@ internal class UIStorageChest : UICanvas(), HasInventory {
itemListChest = UIItemInventoryItemGrid(
this,
catBar,
chest,
getFixtureInventory(),
INVENTORY_CELLS_OFFSET_X - halfSlotOffset,
INVENTORY_CELLS_OFFSET_Y,
6, CELLS_VRT,
@@ -132,7 +143,7 @@ internal class UIStorageChest : UICanvas(), HasInventory {
keyDownFun = { _, _, _ -> Unit },
touchDownFun = { gameItem, amount, _ ->
if (gameItem != null) {
negotiator.reject(chest, getPlayerInventory(), gameItem, amount)
negotiator.reject(getFixtureInventory(), getPlayerInventory(), gameItem, amount)
}
itemListUpdate()
}
@@ -149,7 +160,7 @@ internal class UIStorageChest : UICanvas(), HasInventory {
keyDownFun = { _, _, _ -> Unit },
touchDownFun = { gameItem, amount, _ ->
if (gameItem != null) {
negotiator.accept(getPlayerInventory(), chest, gameItem, amount)
negotiator.accept(getPlayerInventory(), getFixtureInventory(), gameItem, amount)
}
itemListUpdate()
}
@@ -195,6 +206,7 @@ internal class UIStorageChest : UICanvas(), HasInventory {
}
batch.begin()
// UI items
@@ -203,6 +215,40 @@ internal class UIStorageChest : UICanvas(), HasInventory {
catBar.render(batch, camera)
itemListChest.render(batch, camera)
itemListPlayer.render(batch, camera)
blendNormal(batch)
// encumbrance meter
val encumbranceText = Lang["GAME_INVENTORY_ENCUMBRANCE"]
// encumbrance bar will go one row down if control help message is too long
val encumbBarXPos = itemListPlayer.posX + itemListPlayer.width - weightBarWidth
val encumbBarTextXPos = encumbBarXPos - 6 - AppLoader.fontGame.getWidth(encumbranceText)
val encumbBarYPos = UIInventoryCells.encumbBarYPos
// encumbrance bar background
val encumbCol = UIItemInventoryCellCommonRes.getHealthMeterColour(1f - encumbrancePerc, 0f, 1f)
val encumbBack = encumbCol mul UIItemInventoryCellCommonRes.meterBackDarkening
batch.color = Color.WHITE
// encumb text
AppLoader.fontGame.draw(batch, encumbranceText, encumbBarTextXPos, encumbBarYPos - 3f)
// chest name text
AppLoader.fontGame.draw(batch, chestNameFun(), itemListChest.posX + 6f, encumbBarYPos - 3f)
batch.color = encumbBack
batch.fillRect(
encumbBarXPos, encumbBarYPos,
weightBarWidth, controlHelpHeight - 6f
)
// encumbrance bar
batch.color = encumbCol
batch.fillRect(
encumbBarXPos, encumbBarYPos,
if (getPlayerInventory().capacityMode == FixtureInventory.CAPACITY_MODE_NO_ENCUMBER)
1f
else // make sure 1px is always be seen
minOf(weightBarWidth, maxOf(1f, weightBarWidth * encumbrancePerc)),
controlHelpHeight - 6f
)
}
override fun doOpening(delta: Float) {

View File

@@ -14,7 +14,7 @@ import java.util.*
/**
* Created by minjaesong on 2016-06-17.
*/
internal class FixtureTikiTorch : FixtureBase(BlockBox(BlockBox.NO_COLLISION, 1, 2)), Luminous {
internal class FixtureTikiTorch(nameFun: () -> String) : FixtureBase(BlockBox(BlockBox.NO_COLLISION, 1, 2), nameFun = nameFun), Luminous {
private val rndHash1: Int
private val rndHash2: Int

View File

@@ -10,8 +10,8 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2017-01-07.
*/
class TapestryObject(pixmap: Pixmap, val artName: String, val artAuthor: String) :
FixtureBase(BlockBox(BlockBox.NO_COLLISION, 1, 1), renderOrder = RenderOrder.BEHIND) // placeholder blockbox
class TapestryObject(pixmap: Pixmap, val artName: String, val artAuthor: String, nameFun: () -> String) :
FixtureBase(BlockBox(BlockBox.NO_COLLISION, 1, 1), renderOrder = RenderOrder.BEHIND, nameFun = nameFun) // placeholder blockbox
{
// physics = false only speeds up for ~2 frames with 50 tapestries

View File

@@ -6,6 +6,7 @@ import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.gameitem.ItemID
import net.torvald.terrarum.itemproperties.Material
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.gameactors.FixtureStorageChest
import net.torvald.terrarum.modulebasegame.gameactors.FixtureTikiTorch
@@ -31,7 +32,7 @@ class ItemStorageChest(originalID: ItemID) : GameItem(originalID) {
}
override fun startPrimaryUse(delta: Float): Boolean {
val item = FixtureStorageChest()
val item = FixtureStorageChest { Lang[originalName] }
return item.spawn(Terrarum.mouseTileX, Terrarum.mouseTileY - item.blockBox.height + 1)
// return true when placed, false when cannot be placed

View File

@@ -6,6 +6,7 @@ import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.gameitem.ItemID
import net.torvald.terrarum.itemproperties.Material
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.gameactors.FixtureTikiTorch
/**
@@ -14,14 +15,14 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureTikiTorch
class TikiTorchTester(originalID: ItemID) : GameItem(originalID) {
override var dynamicID: ItemID = originalID
override val originalName = "Tiki Torch"
override val originalName = "ITEM_TIKI_TORCH"
override var baseMass = FixtureTikiTorch.MASS
override var stackable = true
override var inventoryCategory = Category.FIXTURE
override val isUnique = false
override val isDynamic = false
override val material = Material()
override val itemImage: TextureRegion?
override val itemImage: TextureRegion
get() = CommonResourcePool.getAsTextureRegion("itemplaceholder_48")
override var baseToolSize: Double? = baseMass
@@ -30,7 +31,7 @@ class TikiTorchTester(originalID: ItemID) : GameItem(originalID) {
}
override fun startPrimaryUse(delta: Float): Boolean {
val item = FixtureTikiTorch()
val item = FixtureTikiTorch { Lang[originalName] }
return item.spawn(Terrarum.mouseTileX, Terrarum.mouseTileY - item.blockBox.height + 1)
// return true when placed, false when cannot be placed

View File

@@ -14,6 +14,7 @@ import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVENTOR
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVENTORY_CELLS_OFFSET_Y
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalWidth
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVEN_DEBUG_MODE
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.controlHelpHeight
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericKeyDownFun
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericTouchDownFun
import net.torvald.terrarum.ui.UICanvas
@@ -26,8 +27,10 @@ internal class UIInventoryCells(
override var height: Int = AppLoader.screenH
override var openCloseTime: Second = 0.0f
private val weightBarWidth = UIItemInventoryElemSimple.height * 2f + UIItemInventoryItemGrid.listGap
companion object {
val weightBarWidth = UIItemInventoryElemSimple.height * 2f + UIItemInventoryItemGrid.listGap
var encumbBarYPos = 0f
}
internal var encumbrancePerc = 0f
private set
@@ -106,6 +109,7 @@ internal class UIInventoryCells(
if (AppLoader.fontGame.getWidth(full.listControlHelp) + 2 + controlHintXPos >= encumbBarTextXPos)
AppLoader.fontGame.lineHeight
else 0f
Companion.encumbBarYPos = encumbBarYPos // q&d hack to share some numbers
AppLoader.fontGame.draw(batch,
encumbranceText,
@@ -120,7 +124,7 @@ internal class UIInventoryCells(
batch.color = encumbBack
batch.fillRect(
encumbBarXPos, encumbBarYPos,
weightBarWidth, full.controlHelpHeight - 6f
weightBarWidth, controlHelpHeight - 6f
)
// encumbrance bar
batch.color = encumbCol
@@ -130,7 +134,7 @@ internal class UIInventoryCells(
1f
else // make sure 1px is always be seen
minOf(weightBarWidth, maxOf(1f, weightBarWidth * encumbrancePerc)),
full.controlHelpHeight - 6f
controlHelpHeight - 6f
)
// debug text
batch.color = Color.LIGHT_GRAY
@@ -138,7 +142,7 @@ internal class UIInventoryCells(
AppLoader.fontSmallNumbers.draw(batch,
"${full.actor.inventory.capacity}/${full.actor.inventory.maxCapacity}",
encumbBarTextXPos,
encumbBarYPos + full.controlHelpHeight - 4f
encumbBarYPos + controlHelpHeight - 4f
)
}
}

View File

@@ -53,6 +53,8 @@ class UIInventoryFull(
val gradStartCol = Color(0x404040_60)
val gradEndCol = Color(0x000000_70)
val gradHeight = 48f
val controlHelpHeight = AppLoader.fontGame.lineHeight
}
//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
@@ -108,9 +110,7 @@ class UIInventoryFull(
else
"$gamepadLabelStart ${Lang["GAME_ACTION_CLOSE"]}$SP" +
"$gamepadLabelLT ${Lang["GAME_INVENTORY"]}"
val controlHelpHeight = AppLoader.fontGame.lineHeight
//val catBarWidth = 330
val catBar = UIItemInventoryCatBar(
this,
(AppLoader.screenW - catBarWidth) / 2,