From d20190b2bdd9eaa9037e1576747a4f82583e17cb Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Tue, 11 Apr 2017 19:21:32 +0900 Subject: [PATCH] selecting and consuming item in inventory --- src/net/torvald/terrarum/StateInGame.kt | 16 +++- src/net/torvald/terrarum/StateUITest.kt | 5 +- .../torvald/terrarum/UIItemInventoryElem.kt | 10 +- src/net/torvald/terrarum/console/Inventory.kt | 2 +- .../terrarum/gameactors/ActorHumanoid.kt | 41 +-------- .../terrarum/gameactors/ActorInventory.kt | 91 ++++++++----------- .../terrarum/gameactors/HumanoidNPC.kt | 6 +- .../gameactors/PlayerBuilderSigrid.kt | 2 +- .../torvald/terrarum/gameactors/Pocketed.kt | 38 +++++++- .../terrarum/gameactors/ThreadActorUpdate.kt | 15 ++- .../terrarum/gamecontroller/GameController.kt | 18 +++- .../terrarum/gameitem/InventoryItem.kt | 19 ++-- .../terrarum/itemproperties/ItemCodex.kt | 28 +++++- src/net/torvald/terrarum/ui/UIInventory.kt | 60 +++++++++--- src/net/torvald/terrarum/ui/UIVitalMetre.kt | 10 +- 15 files changed, 223 insertions(+), 138 deletions(-) diff --git a/src/net/torvald/terrarum/StateInGame.kt b/src/net/torvald/terrarum/StateInGame.kt index 704c5c9f0..cd6dfa389 100644 --- a/src/net/torvald/terrarum/StateInGame.kt +++ b/src/net/torvald/terrarum/StateInGame.kt @@ -12,6 +12,7 @@ import net.torvald.terrarum.gameactors.physicssolver.CollisionSolver import net.torvald.terrarum.gamecontroller.GameController import net.torvald.terrarum.gamecontroller.Key import net.torvald.terrarum.gamecontroller.KeyToggler +import net.torvald.terrarum.gameitem.InventoryItem import net.torvald.terrarum.gameworld.GameWorld import net.torvald.terrarum.gameworld.WorldSimulator import net.torvald.terrarum.gameworld.WorldTime @@ -578,6 +579,8 @@ class StateInGame : BasicGameState() { GameController.controllerButtonPressed(controller, button) uiContainer.forEach { it.controllerButtonPressed(controller, button) } // for GamepadControlled UIcanvases + + // TODO use item on Player } override fun controllerButtonReleased(controller: Int, button: Int) { @@ -663,7 +666,18 @@ class StateInGame : BasicGameState() { ThreadParallel.startAll() } else { - actorContainer.forEach { it.update(gc, delta) } + actorContainer.forEach { + it.update(gc, delta) + + if (it is Pocketed) { + it.inventory.forEach { inventoryEntry -> + inventoryEntry.item.effectWhileInPocket(gc, delta) + if (it.isEquipped(inventoryEntry.item)) { + inventoryEntry.item.effectWhenEquipped(gc, delta) + } + } + } + } } } diff --git a/src/net/torvald/terrarum/StateUITest.kt b/src/net/torvald/terrarum/StateUITest.kt index 586a3f63b..a933e9517 100644 --- a/src/net/torvald/terrarum/StateUITest.kt +++ b/src/net/torvald/terrarum/StateUITest.kt @@ -26,8 +26,7 @@ class StateUITest : BasicGameState() { TODO("not implemented") } - override var inventory: ActorInventory = ActorInventory() - override val itemEquipped = Array(InventoryItem.EquipPosition.INDEX_MAX + 1, { null }) + override var inventory: ActorInventory = ActorInventory(this, 100, ActorInventory.CAPACITY_MODE_WEIGHT) } init { @@ -56,6 +55,7 @@ class StateUITest : BasicGameState() { override var category: String = InventoryItem.Category.TOOL override var maxDurability: Double = 10.0 override var durability: Double = 6.43 + override var consumable = false }) actor.inventory.getByID(5656)!!.item.name = "Test tool" @@ -69,6 +69,7 @@ class StateUITest : BasicGameState() { override var baseMass: Double = 1.4 override var baseToolSize: Double? = null override var category: String = InventoryItem.Category.MISC + override var consumable = false }) actor.inventory.add(ItemCodex[16], 543) diff --git a/src/net/torvald/terrarum/UIItemInventoryElem.kt b/src/net/torvald/terrarum/UIItemInventoryElem.kt index 02e12c2d4..d3b6ba4ff 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElem.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElem.kt @@ -66,10 +66,12 @@ class UIItemInventoryElem( // mouseover background if (item != null || drawBackOnNull) { - if (mouseUp) { + // do not highlight even if drawBackOnNull is true + if (mouseUp && item != null) { BlendMode.resolve(mouseoverBackBlendMode) g.color = mouseoverBackCol } + // if drawBackOnNull, just draw background else { BlendMode.resolve(backBlendMode) g.color = backCol @@ -136,11 +138,11 @@ class UIItemInventoryElem( val itemEquipSlot = item!!.equipPosition val player = Terrarum.ingame!!.player - if (item != player.itemEquipped[itemEquipSlot]) { // if this item is unequipped, equip it - player.itemEquipped[itemEquipSlot] = item + if (item != player.inventory.itemEquipped[itemEquipSlot]) { // if this item is unequipped, equip it + player.equipItem(item!!) } else { // if not, unequip it - player.itemEquipped[itemEquipSlot] = null + player.unequipItem(item!!) } } diff --git a/src/net/torvald/terrarum/console/Inventory.kt b/src/net/torvald/terrarum/console/Inventory.kt index 76d36a402..638e3ca86 100644 --- a/src/net/torvald/terrarum/console/Inventory.kt +++ b/src/net/torvald/terrarum/console/Inventory.kt @@ -65,7 +65,7 @@ internal object Inventory : ConsoleCommand { target.inventory.add(item) } - target.itemEquipped[item.equipPosition] = item + target.inventory.itemEquipped[item.equipPosition] = item } override fun printUsage() { diff --git a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt index a43014901..3584cdbfe 100644 --- a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt +++ b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt @@ -23,8 +23,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) /** Must be set by PlayerFactory */ - override var inventory: ActorInventory = ActorInventory() - override val itemEquipped = Array(InventoryItem.EquipPosition.INDEX_MAX + 1, { null }) + override var inventory: ActorInventory = ActorInventory(this, 2000, ActorInventory.CAPACITY_MODE_WEIGHT) // default constructor /** Must be set by PlayerFactory */ @@ -141,6 +140,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) override var baseToolSize: Double? = null override var category = "should_not_be_seen" override val originalName: String = actorValue.getAsString(AVKey.NAME) ?: "(no name)" + override var consumable = false } override fun update(gc: GameContainer, delta: Int) { @@ -177,7 +177,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) // update inventory items inventory.forEach { - if (!itemEquipped.contains(it.item)) { // unequipped + if (!inventory.itemEquipped.contains(it.item)) { // unequipped it.item.effectWhileInPocket(gc, delta) } else { // equipped @@ -186,23 +186,6 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) } } - fun unequipItem(item: InventoryItem) { - for (i in 0..itemEquipped.size - 1) { - val it = itemEquipped[i] - if (item == it) { - it.effectWhenUnEquipped(gameContainer, updateDelta) - itemEquipped[i] = null // remove from the array by nulling it - break - } - } - } - - fun equipItem(item: InventoryItem) { - if (item.equipPosition >= 0) { - itemEquipped[item.equipPosition] = item - } - } - private fun updateGamerControlBox(input: Input) { if (isGamer) { isUpDown = input.isKeyDown(Terrarum.getConfigInt("keyup")) @@ -234,22 +217,6 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) else true override fun processInput(gc: GameContainer, delta: Int, input: Input) { - /////////////////// - // MOUSE CONTROL // - /////////////////// - - /** - * Primary Use - */ - // Left mouse - if (isGamer && input.isKeyDown(Terrarum.getConfigInt("mouseprimary"))) { - (itemEquipped[InventoryItem.EquipPosition.HAND_GRIP] ?: nullItem).primaryUse(gc, delta) - } - - // Right mouse - if (isGamer && input.isKeyDown(Terrarum.getConfigInt("mouseprimary"))) { - (itemEquipped[InventoryItem.EquipPosition.HAND_GRIP] ?: nullItem).secondaryUse(gc, delta) - } /** * L-R stop @@ -511,7 +478,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) if (sprite != null) sprite!!.update(delta) if (spriteGlow != null) spriteGlow!!.update(delta) - println("$this\tsprite current frame: ${sprite!!.currentFrame}") + //println("$this\tsprite current frame: ${sprite!!.currentFrame}") if (grounded) { // set anim row diff --git a/src/net/torvald/terrarum/gameactors/ActorInventory.kt b/src/net/torvald/terrarum/gameactors/ActorInventory.kt index 1b036fb22..c18aabb89 100644 --- a/src/net/torvald/terrarum/gameactors/ActorInventory.kt +++ b/src/net/torvald/terrarum/gameactors/ActorInventory.kt @@ -12,46 +12,24 @@ import java.util.concurrent.locks.ReentrantLock * Created by minjaesong on 16-03-15. */ -class ActorInventory() { +class ActorInventory(val actor: Pocketed, var maxCapacity: Int, private var capacityMode: Int) { - @Transient val CAPACITY_MAX = 0x7FFFFFFF - @Transient val CAPACITY_MODE_NO_ENCUMBER = 0 - @Transient val CAPACITY_MODE_COUNT = 1 - @Transient val CAPACITY_MODE_WEIGHT = 2 + companion object { + @Transient val CAPACITY_MODE_NO_ENCUMBER = 0 + @Transient val CAPACITY_MODE_COUNT = 1 + @Transient val CAPACITY_MODE_WEIGHT = 2 + } - - private var capacityByCount: Int - private var capacityByWeight: Int - private var capacityMode: Int + /** + * List of all equipped items (tools, armours, rings, necklaces, etc.) + */ + val itemEquipped = Array(InventoryItem.EquipPosition.INDEX_MAX, { null }) /** * Sorted by referenceID. */ private val itemList = ArrayList() - - /** - * Default constructor with no encumbrance. - */ init { - capacityMode = CAPACITY_MODE_NO_ENCUMBER - capacityByCount = 0 - capacityByWeight = 0 - } - - /** - * Construct new inventory with specified capacity. - * @param capacity if is_weight is true, killogramme value is required, counts of items otherwise. - * * - * @param is_weight whether encumbrance should be calculated upon the weight of the inventory. False to use item counts. - */ - constructor(capacity: Int, is_weight: Boolean) : this() { - if (is_weight) { - capacityByWeight = capacity - capacityMode = CAPACITY_MODE_WEIGHT - } else { - capacityByCount = capacity - capacityMode = CAPACITY_MODE_COUNT - } } fun add(itemID: Int, count: Int = 1) = add(ItemCodex[itemID], count) @@ -85,10 +63,14 @@ class ActorInventory() { throw Error("Tried to remove $count of $item, but the inventory only contains ${getByID(item.id)!!.amount} of them.") } else if (newCount > 0) { + // decrement count add(item, -count) } else { + // depleted item; remove entry from inventory itemList.remove(existingItem) + // unequip, if applicable + actor.unequipItem(existingItem.item) } } else { @@ -105,21 +87,13 @@ class ActorInventory() { * Get capacity of inventory * @return */ - fun getCapacity(): Int { - if (capacityMode == CAPACITY_MODE_NO_ENCUMBER) { - return CAPACITY_MAX - } - else if (capacityMode == CAPACITY_MODE_WEIGHT) { - return capacityByWeight - } - else { - return capacityByCount - } - } - - fun getCapacityMode(): Int { - return capacityMode - } + val capacity: Double + get() = if (capacityMode == CAPACITY_MODE_NO_ENCUMBER) + maxCapacity.toDouble() + else if (capacityMode == CAPACITY_MODE_WEIGHT) + getTotalWeight() + else + getTotalCount().toDouble() fun getTotalWeight(): Double = itemList.map { it.item.mass * it.amount }.sum() @@ -137,13 +111,21 @@ class ActorInventory() { * Check whether the itemList contains too many items * @return */ - fun isEncumbered(): Boolean { - if (getCapacityMode() == CAPACITY_MODE_WEIGHT) { - return capacityByWeight < getTotalWeight() - } else if (getCapacityMode() == CAPACITY_MODE_COUNT) { - return capacityByCount < getTotalCount() - } else { - return false + val isEncumbered: Boolean + get() = if (capacityMode == CAPACITY_MODE_NO_ENCUMBER) + false + else if (capacityMode == CAPACITY_MODE_WEIGHT) + maxCapacity < capacity + else + false + + + fun consumeItem(item: InventoryItem) { + if (item.consumable) { + remove(item, 1) + } + else { + // TODO decrement durability } } @@ -152,6 +134,7 @@ class ActorInventory() { + fun hasItem(item: InventoryItem) = hasItem(item.id) fun hasItem(id: Int) = if (itemList.size == 0) diff --git a/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt b/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt index cf091d1bd..84c0bcdb4 100644 --- a/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt +++ b/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt @@ -41,7 +41,7 @@ open class HumanoidNPC( // we're having InventoryItem data so that this class could be somewhat universal override var itemData: InventoryItem = object : InventoryItem() { override var id = referenceID - override val isUnique: Boolean = true + override val isUnique = true override var baseMass: Double get() = actorValue.getAsDouble(AVKey.BASEMASS)!! set(value) { actorValue[AVKey.BASEMASS] = value } @@ -53,8 +53,10 @@ open class HumanoidNPC( } override var category = "npc" override val originalName: String = actorValue.getAsString(AVKey.NAME) ?: "NPC" + override var consumable = false - override fun secondaryUse(gc: GameContainer, delta: Int) { + override fun secondaryUse(gc: GameContainer, delta: Int): Boolean { + return false // TODO place this Actor to the world } } diff --git a/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt b/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt index 09ef2b402..dbc7a68f1 100644 --- a/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt +++ b/src/net/torvald/terrarum/gameactors/PlayerBuilderSigrid.kt @@ -66,7 +66,7 @@ object PlayerBuilderSigrid { p.setHitboxDimension(15, p.actorValue.getAsInt(AVKey.BASEHEIGHT)!!, 11, 0) // FIXME offsetY of -2: Have no idea about the error; it's just supposed to be zero - p.inventory = ActorInventory(0x7FFFFFFF, true) + p.inventory = ActorInventory(p, 0, ActorInventory.CAPACITY_MODE_NO_ENCUMBER) p.setPosition((4096 * FeaturesDrawer.TILE_SIZE).toDouble(), (300 * 16).toDouble()) diff --git a/src/net/torvald/terrarum/gameactors/Pocketed.kt b/src/net/torvald/terrarum/gameactors/Pocketed.kt index e5de89896..5e6d5db3f 100644 --- a/src/net/torvald/terrarum/gameactors/Pocketed.kt +++ b/src/net/torvald/terrarum/gameactors/Pocketed.kt @@ -1,5 +1,6 @@ package net.torvald.terrarum.gameactors +import net.torvald.terrarum.Terrarum import net.torvald.terrarum.gameitem.InventoryItem import java.util.* @@ -10,9 +11,38 @@ interface Pocketed { var inventory: ActorInventory - /** - * List of all equipped items (tools, armours, rings, necklaces, etc.) - */ - val itemEquipped: Array + fun unequipItem(item: InventoryItem) { + if (item.equipPosition == InventoryItem.EquipPosition.NULL) + throw Error("Unequipping the item that cannot be equipped") + + if (!inventory.hasItem(item)) + throw Error("Unequipping the item that does not exist in inventory") + + inventory.itemEquipped[item.equipPosition] = null + item.effectOnUnequip(Terrarum.appgc, Terrarum.UPDATE_DELTA) + } + + fun equipItem(item: InventoryItem) { + if (item.equipPosition >= 0) { + inventory.itemEquipped[item.equipPosition] = item + item.effectWhenEquipped(Terrarum.appgc, Terrarum.UPDATE_DELTA) + } + } + + fun isEquipped(item: InventoryItem): Boolean { + return inventory.itemEquipped[item.equipPosition] == item + } + + + + fun consumePrimary(item: InventoryItem) { + if (item.primaryUse(Terrarum.appgc, Terrarum.UPDATE_DELTA)) + inventory.consumeItem(item) // consume on successful + } + + fun consumeSecondary(item: InventoryItem) { + if (item.secondaryUse(Terrarum.appgc, Terrarum.UPDATE_DELTA)) + inventory.consumeItem(item) // consume on successful + } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameactors/ThreadActorUpdate.kt b/src/net/torvald/terrarum/gameactors/ThreadActorUpdate.kt index c2ccd8204..d2383b873 100644 --- a/src/net/torvald/terrarum/gameactors/ThreadActorUpdate.kt +++ b/src/net/torvald/terrarum/gameactors/ThreadActorUpdate.kt @@ -9,7 +9,18 @@ import org.newdawn.slick.GameContainer class ThreadActorUpdate(val startIndex: Int, val endIndex: Int, val gc: GameContainer, val delta: Int) : Runnable { override fun run() { - for (i in startIndex..endIndex) - Terrarum.ingame!!.actorContainer[i].update(gc, delta) + for (i in startIndex..endIndex) { + val it = Terrarum.ingame!!.actorContainer[i] + it.update(gc, delta) + + if (it is Pocketed) { + it.inventory.forEach { inventoryEntry -> + inventoryEntry.item.effectWhileInPocket(gc, delta) + if (it.isEquipped(inventoryEntry.item)) { + inventoryEntry.item.effectWhenEquipped(gc, delta) + } + } + } + } } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gamecontroller/GameController.kt b/src/net/torvald/terrarum/gamecontroller/GameController.kt index 5b67ef36f..2d839e114 100644 --- a/src/net/torvald/terrarum/gamecontroller/GameController.kt +++ b/src/net/torvald/terrarum/gamecontroller/GameController.kt @@ -4,6 +4,7 @@ import net.torvald.terrarum.mapdrawer.TilesDrawer import net.torvald.terrarum.mapdrawer.FeaturesDrawer import net.torvald.terrarum.Terrarum import net.torvald.terrarum.gameactors.* +import net.torvald.terrarum.gameitem.InventoryItem import net.torvald.terrarum.mapdrawer.MapCamera import net.torvald.terrarum.tileproperties.Tile import net.torvald.terrarum.tileproperties.TileCodex @@ -34,11 +35,11 @@ object GameController { get() = (mouseY / FeaturesDrawer.TILE_SIZE).floorInt() fun processInput(gc: GameContainer, delta: Int, input: Input) { - if (Terrarum.ingame != null) { val ingame = Terrarum.ingame!! + // actor process input if (!ingame.consoleHandler.isTakingControl) { if (ingame.canPlayerMove) { ingame.actorContainer.forEach { @@ -67,7 +68,20 @@ object GameController { /////////////////// // MOUSE CONTROL // /////////////////// - // PRIMARY/SECONDARY IS FIXED TO LEFT/RIGHT BUTTON // + + // Use item: assuming the player has only one effective grip (EquipPosition.HAND_GRIP) + if (input.isMouseButtonDown(Terrarum.getConfigInt("mouseprimary")) || input.isMouseButtonDown(Terrarum.getConfigInt("mousesecondary"))) { + val itemOnGrip = ingame.player.inventory.itemEquipped[InventoryItem.EquipPosition.HAND_GRIP] + + if (itemOnGrip != null) { + if (input.isMouseButtonDown(Terrarum.getConfigInt("mouseprimary"))) { + ingame.player.consumePrimary(itemOnGrip) + } + else if (input.isMouseButtonDown(Terrarum.getConfigInt("mousesecondary"))) { + ingame.player.consumeSecondary(itemOnGrip) + } + } + } ///////////////////// diff --git a/src/net/torvald/terrarum/gameitem/InventoryItem.kt b/src/net/torvald/terrarum/gameitem/InventoryItem.kt index 25e1a9ff8..948e60dee 100644 --- a/src/net/torvald/terrarum/gameitem/InventoryItem.kt +++ b/src/net/torvald/terrarum/gameitem/InventoryItem.kt @@ -51,6 +51,9 @@ abstract class InventoryItem : Comparable { var itemProperties = ItemValue() + /** Single-use then destroyed (e.g. Tiles) */ + abstract var consumable: Boolean + /** * Where to equip the item */ @@ -107,13 +110,17 @@ abstract class InventoryItem : Comparable { /** * Effects applied (continuously or not) while primary button (usually left mouse button) is down + * + * @return true when use successfully, false otherwise */ - open fun primaryUse(gc: GameContainer, delta: Int) { } + open fun primaryUse(gc: GameContainer, delta: Int): Boolean = false /** * Effects applied (continuously or not) while secondary button (usually right mouse button) is down + * + * @return true when use successfully, false otherwise */ - open fun secondaryUse(gc: GameContainer, delta: Int) { } + open fun secondaryUse(gc: GameContainer, delta: Int): Boolean = false /** * Effects applied immediately only once if thrown from pocket @@ -128,7 +135,7 @@ abstract class InventoryItem : Comparable { /** * Effects applied only once when unequipped */ - open fun effectWhenUnEquipped(gc: GameContainer, delta: Int) { } + open fun effectOnUnequip(gc: GameContainer, delta: Int) { } override fun toString(): String { @@ -158,11 +165,7 @@ abstract class InventoryItem : Comparable { if (equipPosition == EquipPosition.NULL) throw IllegalArgumentException("Item is not supposed to be equipped (equipPosition is NULL") - if (!actor.inventory.hasItem(this.id)) { - actor.inventory.add(this) - } - - actor.itemEquipped[this.equipPosition] = this + actor.equipItem(this) } object EquipPosition { diff --git a/src/net/torvald/terrarum/itemproperties/ItemCodex.kt b/src/net/torvald/terrarum/itemproperties/ItemCodex.kt index ef8ef4829..ea49d7857 100644 --- a/src/net/torvald/terrarum/itemproperties/ItemCodex.kt +++ b/src/net/torvald/terrarum/itemproperties/ItemCodex.kt @@ -10,8 +10,10 @@ import net.torvald.terrarum.gamecontroller.mouseTileX import net.torvald.terrarum.gamecontroller.mouseTileY import net.torvald.terrarum.gameitem.IVKey import net.torvald.terrarum.gameworld.GameWorld +import net.torvald.terrarum.mapdrawer.TilesDrawer import net.torvald.terrarum.tileproperties.TileCodex import org.newdawn.slick.GameContainer +import org.newdawn.slick.Image import java.util.* /** @@ -33,6 +35,9 @@ object ItemCodex { val ITEM_DYNAMIC_MIN = ITEM_STATIC_MAX + 1 val ITEM_STATIC_MIN = ITEM_TILE_MAX + 1 // 4096 + private val itemImagePlaceholder = Image("./assets/item_kari_24.tga") + + init { // tile items (blocks and walls are the same thing basically) for (i in 0..ITEM_TILE_MAX) { @@ -44,28 +49,38 @@ object ItemCodex { override var equipPosition = EquipPosition.HAND_GRIP override var category = "block" override val originalName = TileCodex[i].nameKey + override var consumable = true init { itemProperties[IVKey.ITEMTYPE] = IVKey.ItemType.BLOCK } - override fun primaryUse(gc: GameContainer, delta: Int) { + override fun primaryUse(gc: GameContainer, delta: Int): Boolean { + return false // TODO base punch attack } - override fun secondaryUse(gc: GameContainer, delta: Int) { + override fun secondaryUse(gc: GameContainer, delta: Int): Boolean { val mousePoint = Point2d(gc.mouseTileX.toDouble(), gc.mouseTileY.toDouble()) // linear search filter (check for intersection with tilewise mouse point and tilewise hitbox) Terrarum.ingame!!.actorContainer.forEach { if (it is ActorWithSprite && it.tilewiseHitbox.intersects(mousePoint)) - return + return false } + + // return false if the tile is already there + if (this.id == Terrarum.ingame!!.world.getTileFromTerrain(gc.mouseTileX, gc.mouseTileY)) + return false + // filter passed, do the job + // FIXME this is only useful for Player Terrarum.ingame!!.world.setTileTerrain( gc.mouseTileX, gc.mouseTileY, i ) + + return true } } } @@ -89,5 +104,12 @@ object ItemCodex { } } + fun getItemImage(code: Int): Image { + if (code <= ITEM_TILE_MAX) + return TilesDrawer.tilesTerrain.getSubImage((code % 16) * 16, code / 16) + else + return itemImagePlaceholder + } + fun hasItem(itemID: Int): Boolean = dynamicItemDescription.containsKey(itemID) } \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UIInventory.kt b/src/net/torvald/terrarum/ui/UIInventory.kt index c4d2b34f4..925394a63 100644 --- a/src/net/torvald/terrarum/ui/UIInventory.kt +++ b/src/net/torvald/terrarum/ui/UIInventory.kt @@ -6,6 +6,7 @@ import net.torvald.terrarum.Terrarum.joypadLabelNinA import net.torvald.terrarum.Terrarum.joypadLabelNinY import net.torvald.terrarum.gameactors.* import net.torvald.terrarum.gameitem.InventoryItem +import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.langpack.Lang import org.newdawn.slick.* import java.util.* @@ -28,11 +29,9 @@ class UIInventory( override var handler: UIHandler? = null override var openCloseTime: Int = 120 - val itemImagePlaceholder = Image("./assets/item_kari_24.tga") - val catButtonsToCatIdent = HashMap() - val backgroundColour = Color(0xA0242424.toInt()) + val backgroundColour = Color(0x80242424.toInt()) init { catButtonsToCatIdent.put("GAME_INVENTORY_WEAPONS", InventoryItem.Category.WEAPON) @@ -88,7 +87,7 @@ class UIInventory( val itemsStripWidth = ((width - catButtons.width) - (2 * itemStripGutterH + itemInterColGutter)) / 2 val items = Array( - 2 + height / (UIItemInventoryElem.height + itemStripGutterV - controlHelpHeight) * 2, { + ((height - controlHelpHeight) / (UIItemInventoryElem.height + itemStripGutterV)) * 2, { UIItemInventoryElem( parentUI = this, posX = catButtons.width + if (it % 2 == 0) itemStripGutterH else (itemStripGutterH + itemsStripWidth + itemInterColGutter), @@ -99,7 +98,9 @@ class UIInventory( itemImage = null, mouseoverBackCol = Color(0x282828), mouseoverBackBlendMode = BlendMode.SCREEN, - drawBackOnNull = false + backCol = Color(0xd4d4d4), + backBlendMode = BlendMode.MULTIPLY, + drawBackOnNull = true ) }) val itemsScrollOffset = 0 @@ -160,7 +161,7 @@ class UIInventory( val sortListItem = inventorySortList[k + itemsScrollOffset] items[k].item = sortListItem.item items[k].amount = sortListItem.amount - items[k].itemImage = itemImagePlaceholder + items[k].itemImage = ItemCodex.getItemImage(sortListItem.item.id) // set quickslot number for (qs in 1..QUICKSLOT_MAX) { @@ -173,9 +174,9 @@ class UIInventory( } // set equippedslot number - for (eq in 0..actor!!.itemEquipped.size - 1) { - if (eq < actor!!.itemEquipped.size) { - if (actor!!.itemEquipped[eq] == items[k].item) { + for (eq in 0..actor!!.inventory.itemEquipped.size - 1) { + if (eq < actor!!.inventory.itemEquipped.size) { + if (actor!!.inventory.itemEquipped[eq] == items[k].item) { items[k].equippedSlot = eq break } @@ -199,6 +200,8 @@ class UIInventory( oldCatSelect = catButtons.selectedIndex } + private val weightBarWidth = 60f + override fun render(gc: GameContainer, g: Graphics) { // background blendNormal() @@ -221,9 +224,42 @@ class UIInventory( // texts blendNormal() - g.color = Color(0xdddddd) - Typography.printCentered(g, listControlHelp, catButtons.width, height - controlHelpHeight, width - catButtons.width) - Typography.printCentered(g, listControlClose, 0, height - controlHelpHeight, catButtons.width) + g.color = Color(0xe8e8e8) + // W - close + g.drawString(listControlClose, 4f, height - controlHelpHeight.toFloat()) + // MouseL - Use ; 1.9 - Register ; T - Drop + g.drawString(listControlHelp, catButtons.width + 4f, height - controlHelpHeight.toFloat()) + // encumbrance + if (inventory != null) { + val encumbranceText = Lang["GAME_INVENTORY_ENCUMBRANCE"] + + g.drawString( + encumbranceText, + width - 9 - g.font.getWidth(encumbranceText) - weightBarWidth, + height - controlHelpHeight.toFloat() + ) + + // encumbrance bar background + blendMul() + g.color = Color(0xa0a0a0) + g.fillRect( + width - 3 - weightBarWidth, + height - controlHelpHeight + 3f, + weightBarWidth, + controlHelpHeight - 6f + ) + // encumbrance bar + blendNormal() + val encumbPerc = inventory!!.capacity.toFloat() / inventory!!.maxCapacity + g.color = if (inventory!!.isEncumbered) Color(0xccff0000.toInt()) else Color(0xcc00ff00.toInt()) + g.fillRect( + width - 3 - weightBarWidth, + height - controlHelpHeight + 3f, + minOf(weightBarWidth, maxOf(1f, weightBarWidth * encumbPerc)), // make sure 1px is always be seen + controlHelpHeight - 5f + ) + } + } diff --git a/src/net/torvald/terrarum/ui/UIVitalMetre.kt b/src/net/torvald/terrarum/ui/UIVitalMetre.kt index 5b123f76a..7927e99dd 100644 --- a/src/net/torvald/terrarum/ui/UIVitalMetre.kt +++ b/src/net/torvald/terrarum/ui/UIVitalMetre.kt @@ -1,5 +1,6 @@ package net.torvald.terrarum.ui +import com.jme3.math.FastMath import net.torvald.colourutil.CIELabUtil.darkerLab import net.torvald.terrarum.Terrarum import net.torvald.terrarum.gameactors.ActorHumanoid @@ -59,12 +60,9 @@ class UIVitalMetre( */ override fun render(gc: GameContainer, g: Graphics) { if (vitalGetterVal() != null && vitalGetterMax() != null && player != null) { - - // FIXME does not work well with screen zoom, because of my custom g.translate - g.translate( - -MapCamera.x + player!!.centrePosPoint.x.toFloat(), - -MapCamera.y + player!!.centrePosPoint.y.toFloat() + Terrarum.ingame!!.screenZoom * (player!!.centrePosPoint.x.toFloat() - (MapCamera.x)), + Terrarum.ingame!!.screenZoom * (player!!.centrePosPoint.y.toFloat() - (MapCamera.y)) ) @@ -118,6 +116,8 @@ class UIVitalMetre( } } +fun Float.abs() = FastMath.abs(this) + /* +-------------+ (84)