diff --git a/src/net/torvald/terrarum/gameactors/Actor.kt b/src/net/torvald/terrarum/gameactors/Actor.kt index 5fa9f1a7a..693e2b86a 100644 --- a/src/net/torvald/terrarum/gameactors/Actor.kt +++ b/src/net/torvald/terrarum/gameactors/Actor.kt @@ -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, 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 diff --git a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt index 2572169ba..c06e0a4e7 100644 --- a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt +++ b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt @@ -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 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt index febdb59b4..098f09b70 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt @@ -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) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt index d20f361f4..6c0b5670e 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt @@ -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) {} } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt index f101e2677..3d9e911bf 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt @@ -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, diff --git a/src/net/torvald/terrarum/serialise/Common.kt b/src/net/torvald/terrarum/serialise/Common.kt index 9af644ad4..20b4d42c0 100644 --- a/src/net/torvald/terrarum/serialise/Common.kt +++ b/src/net/torvald/terrarum/serialise/Common.kt @@ -43,7 +43,7 @@ object Common { // install custom (de)serialiser init { - jsoner.ignoreUnknownFields = true +// jsoner.ignoreUnknownFields = true jsoner.setUsePrototypes(false) jsoner.setIgnoreDeprecated(false) diff --git a/src/net/torvald/terrarum/serialise/WriteActor.kt b/src/net/torvald/terrarum/serialise/WriteActor.kt index 6d8c560ca..19d9eac89 100644 --- a/src/net/torvald/terrarum/serialise/WriteActor.kt +++ b/src/net/torvald/terrarum/serialise/WriteActor.kt @@ -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 - } - } \ No newline at end of file