From 58a91ed10b2ac3c3e89d468f2b202fac3c8a1587 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 12 Mar 2019 01:15:57 +0900 Subject: [PATCH] can retrieve item image for dynamic items --- .../terrarum/itemproperties/ItemCodex.kt | 10 +++++++++- .../gameactors/ActorInventory.kt | 18 +++++++++--------- .../modulebasegame/ui/AmmoMeterProxy.kt | 2 +- .../ui/UIItemInventoryEquippedView.kt | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/net/torvald/terrarum/itemproperties/ItemCodex.kt b/src/net/torvald/terrarum/itemproperties/ItemCodex.kt index 9033befb2..4afcd320c 100644 --- a/src/net/torvald/terrarum/itemproperties/ItemCodex.kt +++ b/src/net/torvald/terrarum/itemproperties/ItemCodex.kt @@ -22,6 +22,7 @@ object ItemCodex { */ val itemCodex = HashMap() val dynamicItemDescription = HashMap() + val dynamicToStaticTable = HashMap() val ITEM_TILES = 0..GameWorld.TILES_SUPPORTED - 1 val ITEM_WALLS = GameWorld.TILES_SUPPORTED..GameWorld.TILES_SUPPORTED * 2 - 1 @@ -242,6 +243,7 @@ object ItemCodex { printdbg(this, "Registering new dynamic item $dynamicID (from ${item.originalID})") } dynamicItemDescription[dynamicID] = item + dynamicToStaticTable[dynamicID] = item.originalID } /** @@ -265,6 +267,8 @@ object ItemCodex { } } + fun dynamicToStaticID(dynamicID: ItemID) = dynamicToStaticTable[dynamicID]!! + /** * Mainly used by GameItemLoader */ @@ -279,8 +283,12 @@ object ItemCodex { } fun getItemImage(itemOriginalID: Int): TextureRegion { + // dynamic item + if (itemOriginalID in ITEM_DYNAMIC) { + return getItemImage(dynamicToStaticID(itemOriginalID)) + } // terrain - if (itemOriginalID in ITEM_TILES) { + else if (itemOriginalID in ITEM_TILES) { return BlocksDrawer.tileItemTerrain.get( itemOriginalID % 16, itemOriginalID / 16 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt index 526aedc7a..19262e5a9 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt @@ -77,7 +77,7 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c // If we already have the item, increment the amount // If not, add item with specified amount - val existingItem = getByDynamicID(item.dynamicID) + val existingItem = invSearchByDynamicID(item.dynamicID) // if the item already exists if (existingItem != null) { @@ -106,7 +106,7 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c - val existingItem = getByDynamicID(item.dynamicID) + val existingItem = invSearchByDynamicID(item.dynamicID) if (existingItem != null) { // if the item already exists val newCount = existingItem.amount - count @@ -133,7 +133,7 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c quickSlot[slot] = dynamicID } - fun getQuickslot(slot: Int): InventoryPair? = getByDynamicID(quickSlot[slot]) + fun getQuickslot(slot: Int): InventoryPair? = invSearchByDynamicID(quickSlot[slot]) /** * HashMap @@ -198,7 +198,7 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c newItem.stackable = false add(newItem) - itemEquipped[newItem.equipPosition] = getByDynamicID(newItem.dynamicID)!!.item // will test if some sketchy code is written. Test fail: kotlinNullpointerException + itemEquipped[newItem.equipPosition] = newItem.dynamicID //invSearchByDynamicID(newItem.dynamicID)!!.item // will test if some sketchy code is written. Test fail: kotlinNullpointerException // FIXME now damage meter (vital) is broken } @@ -236,7 +236,7 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c false else itemList.binarySearch(id, DYNAMIC_ID) >= 0 - fun getByDynamicID(id: ItemID?): InventoryPair? { + fun invSearchByDynamicID(id: ItemID?): InventoryPair? { if (itemList.size == 0 || id == null) return null @@ -246,8 +246,8 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c else return itemList[index] } - private fun getByStaticID(id: ItemID): InventoryPair? { - if (itemList.size == 0) + private fun invSearchByStaticID(id: ItemID?): InventoryPair? { + if (itemList.size == 0 || id == null) return null val index = itemList.binarySearch(id, STATIC_ID) @@ -269,7 +269,7 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c } @Transient private val STATIC_ID = 41324534 @Transient private val DYNAMIC_ID = 181643953 - private fun ArrayList.binarySearch(ID: ItemID, searchBy: Int): Int { + private fun ArrayList.binarySearch(ID: ItemID, searchMode: Int): Int { // code from collections/Collections.kt var low = 0 var high = this.size - 1 @@ -277,7 +277,7 @@ class ActorInventory(@Transient val actor: Pocketed, var maxCapacity: Int, var c while (low <= high) { val mid = (low + high).ushr(1) // safe from overflows - val midVal = if (searchBy == STATIC_ID) + val midVal = if (searchMode == STATIC_ID) ItemCodex[this[mid].item]!!.originalID else ItemCodex[this[mid].item]!!.dynamicID diff --git a/src/net/torvald/terrarum/modulebasegame/ui/AmmoMeterProxy.kt b/src/net/torvald/terrarum/modulebasegame/ui/AmmoMeterProxy.kt index e4b0df027..16c700daf 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/AmmoMeterProxy.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/AmmoMeterProxy.kt @@ -19,7 +19,7 @@ object AmmoMeterProxy { else { meter.vitalGetterVal = { if (currentItem.stackable && currentItem.maxDurability == GameItem.DURABILITY_NA) { - actor.inventory.getByDynamicID(currentItem.dynamicID)!!.amount.toFloat() + actor.inventory.invSearchByDynamicID(currentItem.dynamicID)!!.amount.toFloat() } else currentItem.durability diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt index 7de0a9970..319d3f53c 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt @@ -132,7 +132,7 @@ class UIItemInventoryEquippedView( itemGrid[k].equippedSlot = null } else { - val itemRecord = inventory.getByDynamicID(item)!! + val itemRecord = inventory.invSearchByDynamicID(item)!! itemGrid[k].item = ItemCodex[item] itemGrid[k].amount = itemRecord.amount