fixed a bug where UI of the storagechest won't properly initialise after a load

This commit is contained in:
minjaesong
2021-10-14 11:21:10 +09:00
parent f9cff90496
commit 4b67121e70
7 changed files with 48 additions and 35 deletions

View File

@@ -1,8 +1,15 @@
package net.torvald.terrarum.gameactors
import net.torvald.random.HQRNG
import net.torvald.spriteanimation.HasAssembledSprite
import net.torvald.spriteanimation.SpriteAnimation
import net.torvald.spriteassembler.ADProperties
import net.torvald.terrarum.ReferencingRanges
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.modulebasegame.gameactors.IngamePlayer
import net.torvald.terrarum.modulebasegame.gameactors.Pocketed
import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.tvda.ByteArray64Reader
import net.torvald.terrarum.tvda.toBigEndian
import net.torvald.terrarum.utils.PasswordBase32
@@ -73,6 +80,15 @@ abstract class Actor : Comparable<Actor>, Runnable {
fun Int.sign(): Int = if (this > 0) 1 else if (this < 0) -1 else 0
open fun reload() {
/* called when the actor is loaded from the save; one use of this function is to "re-sync" the
* Transient variables such as `mainUI` of FixtureBase
*/
actorValue.actor = this
if (this is Pocketed)
inventory.actor = this
}
/**
* ActorValue change event handler

View File

@@ -897,7 +897,8 @@ open class ActorWithBody : Actor {
bounceY = false
// this will slow down the player, but its main purpose is to hide a bug
// where when player happens to be "walled" (which zeroes the x velo) and keeps moving left/right
// that when player happens to be "walled" (which zeroes the x velo) they can keep
// move left/right as long as "buried depth" <= stairheight
// so we also zero the same exact value here for perfect hiding
if (controllerV != null) {
val stairRatio = stairHeight / hitbox.height

View File

@@ -30,8 +30,8 @@ open class FixtureBase : ActorWithBody, CuedByTerrainChange {
lateinit var blockBox: BlockBox // something like TapestryObject will want to redefine this
fun blockBoxIndexToPoint2i(it: BlockBoxIndex): Point2i = this.blockBox.width.let { w -> Point2i(it % w, it / w) }
var blockBoxProps: BlockBoxProps = BlockBoxProps(0)
var nameFun: () -> String = { "" }
var mainUI: UICanvas? = null
@Transient var nameFun: () -> String = { "" }
@Transient var mainUI: UICanvas? = null
var inventory: FixtureInventory? = null
protected constructor() : super(RenderOrder.BEHIND, PhysProperties.IMMOBILE, null)

View File

@@ -45,6 +45,19 @@ internal class FixtureStorageChest : FixtureBase {
sprite!!.setRowsAndFrames(1, 1)
actorValue[AVKey.BASEMASS] = MASS
println("FixtureStorageChest constructor call")
printStackTrace(this)
}
override fun reload() {
super.reload()
// doing this is required as when things are deserialised, constructor is called, THEN the fields are
// filled in, thus the initialised mainUI has a stale reference;
// we fix it by simply giving a new reference to the mainUI
(mainUI as UIStorageChest).chestInventory = this.inventory!!
(mainUI as UIStorageChest).chestNameFun = this.nameFun
}
companion object {
@@ -202,12 +215,13 @@ internal class UIStorageChest : UICanvas(
// encumbrance meter
val encumbranceText = Lang["GAME_INVENTORY_ENCUMBRANCE"]
val chestName = chestNameFun()
val chestNameXpos = itemListChest.posX + 6f
val encumbBarXPos = itemListPlayer.posX + itemListPlayer.width - weightBarWidth
val encumbBarTextXPos = encumbBarXPos - 6 - App.fontGame.getWidth(encumbranceText)
val encumbBarYPos = UIInventoryCells.encumbBarYPos
val encumbBarYPos = (App.scr.height + internalHeight).div(2) - 20 + 3f
val encumbCol = UIItemInventoryCellCommonRes.getHealthMeterColour(1f - encumbrancePerc, 0f, 1f)
val encumbBack = encumbCol mul UIItemInventoryCellCommonRes.meterBackDarkening
val chestName = chestNameFun()
// encumbrance bar background
batch.color = encumbBack
@@ -228,10 +242,14 @@ internal class UIStorageChest : UICanvas(
// chest name text
batch.color = Color.WHITE
App.fontGame.draw(batch, chestName, itemListChest.posX + 6f, encumbBarYPos - 3f)
App.fontGame.draw(batch, chestName, chestNameXpos, encumbBarYPos - 3f)
// encumb text
batch.color = Color.WHITE
App.fontGame.draw(batch, encumbranceText, encumbBarTextXPos, encumbBarYPos - 3f)
App.fontGame.draw(batch, encumbranceText, encumbBarTextXPos, encumbBarYPos - 3f +
if (App.fontGame.getWidth(chestName) + 2 + chestNameXpos >= encumbBarTextXPos)
App.fontGame.lineHeight
else 0f
)
}
override fun doOpening(delta: Float) {
@@ -253,6 +271,6 @@ internal class UIStorageChest : UICanvas(
override fun dispose() {
shapeRenderer.dispose()
try { shapeRenderer.dispose() } catch (e: IllegalArgumentException) {}
}
}

View File

@@ -12,6 +12,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.INVEN_DEBUG_MODE
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.controlHelpHeight
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalHeight
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalWidth
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericKeyDownFun
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericTouchDownFun
@@ -28,7 +29,7 @@ internal class UIInventoryCells(
companion object {
val weightBarWidth = UIItemInventoryElemSimple.height * 2f + UIItemInventoryItemGrid.listGap
var encumbBarYPos = 0f
// var encumbBarYPos = (App.scr.height + internalHeight).div(2) - 20 + 3f
}
internal var encumbrancePerc = 0f
@@ -106,7 +107,7 @@ internal class UIInventoryCells(
if (App.fontGame.getWidth(full.listControlHelp) + 2 + controlHintXPos >= encumbBarTextXPos)
App.fontGame.lineHeight
else 0f
Companion.encumbBarYPos = encumbBarYPos // q&d hack to share some numbers
// Companion.encumbBarYPos = encumbBarYPos // q&d hack to share some numbers
App.fontGame.draw(batch,
encumbranceText,

View File

@@ -43,7 +43,7 @@ object Common {
// install custom (de)serialiser
init {
jsoner.ignoreUnknownFields = true
// jsoner.ignoreUnknownFields = true
jsoner.setUsePrototypes(false)
jsoner.setIgnoreDeprecated(false)

View File

@@ -126,14 +126,9 @@ object ReadActor {
operator fun invoke(disk: SimpleFileSystem, dataStream: Reader): Actor =
fillInDetails(disk, Common.jsoner.fromJson(null, dataStream))
fun readActorBare(worldDataStream: Reader): Actor =
Common.jsoner.fromJson(null, worldDataStream)
private fun fillInDetails(disk: SimpleFileSystem, actor: Actor): Actor {
actor.actorValue.actor = actor
actor.reload()
if (actor is Pocketed)
actor.inventory.actor = actor
if (actor is ActorWithBody && actor is IngamePlayer) {
val animFile = disk.getFile(-2L)
@@ -163,22 +158,4 @@ object ReadActor {
return actor
}
fun readActorAndAddToWorld(ingame: TerrarumIngame, disk: SimpleFileSystem, dataStream: Reader): Actor {
val actor = invoke(disk, dataStream)
// replace existing player
val oldPlayerID = ingame.actorNowPlaying?.referenceID
try {
ingame.forceRemoveActor(ingame.getActorByID(actor.referenceID))
}
catch (e: NoSuchActorWithIDException) { /* no actor to delete, you may proceed */ }
ingame.addNewActor(actor)
if (actor.referenceID == oldPlayerID)
ingame.actorNowPlaying = actor as ActorHumanoid
return actor
}
}