mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 20:31:51 +09:00
Quickbar: all working except for equip/unequip
This commit is contained in:
@@ -42,7 +42,6 @@ class StateUITest : BasicGameState() {
|
||||
// these are the test codes.
|
||||
// Item properties must be pre-composed using CSV/JSON, and read and made into the item instance
|
||||
// using factory/builder pattern. @see ItemCodex
|
||||
actor.actorValue[AVKey.__PLAYER_QSPREFIX + "3"] = Block.STONE
|
||||
|
||||
actor.inventory.add(object : InventoryItem() {
|
||||
init {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
import net.torvald.colourutil.CIELabUtil.darkerLab
|
||||
import net.torvald.terrarum.gamecontroller.Key
|
||||
import net.torvald.terrarum.itemproperties.InventoryItem
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
import net.torvald.terrarum.ui.UIInventory
|
||||
@@ -125,6 +126,28 @@ class UIItemInventoryElem(
|
||||
}
|
||||
|
||||
override fun keyPressed(key: Int, c: Char) {
|
||||
if (item != null && Terrarum.ingame != null && key in Key.NUM_1..Key.NUM_0) {
|
||||
val inventory = Terrarum.ingame!!.player?.inventory
|
||||
val slot = key - Key.NUM_1
|
||||
val currentSlotItem = inventory?.getQuickBar(slot)
|
||||
|
||||
|
||||
inventory?.setQuickBar(
|
||||
slot,
|
||||
if (currentSlotItem?.item != item)
|
||||
item?.dynamicID // register
|
||||
else
|
||||
null // drop registration
|
||||
)
|
||||
|
||||
// search for duplicates in the quickbar, except mine
|
||||
// if there is, unregister the other
|
||||
(0..9).minus(slot).forEach {
|
||||
if (inventory?.getQuickBar(it)?.item == item) {
|
||||
inventory?.setQuickBar(it, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun keyReleased(key: Int, c: Char) {
|
||||
|
||||
@@ -93,9 +93,7 @@ object AVKey {
|
||||
|
||||
|
||||
const val __PLAYER_QUICKSLOTSEL = "__quickslotselection"
|
||||
/** SYNOPSIS: __qsitem1 .. __qsitem10
|
||||
* contains tem ID; they are supposed to be unique. Indices must be ONE-BASED! */
|
||||
const val __PLAYER_QSPREFIX = "__qsitem" // __qsitem1 .. __qsitem10 (NOT ZERO BASED!)
|
||||
|
||||
/** Double
|
||||
* When using tool/arm/etc. how long action button is held, in milliseconds (Int)
|
||||
* Or for NPCs, how long it has been waiting for next move
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.torvald.terrarum.gameactors
|
||||
import com.jme3.math.FastMath
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.gameactors.faction.Faction
|
||||
import net.torvald.terrarum.gamecontroller.Key
|
||||
import net.torvald.terrarum.itemproperties.InventoryItem
|
||||
import net.torvald.terrarum.itemproperties.Material
|
||||
import net.torvald.terrarum.realestate.LandUtil
|
||||
@@ -333,7 +334,9 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
|
||||
}
|
||||
|
||||
override fun keyPressed(key: Int, c: Char) {
|
||||
|
||||
if (key in Key.NUM_1..Key.NUM_0) {
|
||||
actorValue[AVKey.__PLAYER_QUICKSLOTSEL] = key - Key.NUM_1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,7 +33,7 @@ class ActorInventory(val actor: Pocketed, var maxCapacity: Int, var capacityMode
|
||||
* Sorted by referenceID.
|
||||
*/
|
||||
private val itemList = ArrayList<InventoryPair>()
|
||||
private val quickBar = Array<ItemID?>(10, { null })
|
||||
private val quickBar = Array<ItemID?>(10, { null }) // 0: Slot 1, 9: Slot 10
|
||||
|
||||
init {
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ class UIInventory(
|
||||
|
||||
val inventory: ActorInventory?
|
||||
get() = actor?.inventory
|
||||
val actorValue: ActorValue
|
||||
get() = (actor as Actor).actorValue
|
||||
//val actorValue: ActorValue
|
||||
// get() = (actor as Actor).actorValue
|
||||
|
||||
override var handler: UIHandler? = null
|
||||
override var openCloseTime: Int = 120
|
||||
@@ -146,67 +146,7 @@ class UIInventory(
|
||||
|
||||
|
||||
if (rebuildList) {
|
||||
val filter = catButtonsToCatIdent[catButtons.selectedButton.labelText]
|
||||
|
||||
// encumbrance
|
||||
encumbrancePerc = inventory!!.capacity.toFloat() / inventory!!.maxCapacity
|
||||
isEncumbered = inventory!!.isEncumbered
|
||||
|
||||
|
||||
|
||||
inventorySortList = ArrayList<InventoryPair>()
|
||||
|
||||
// filter items
|
||||
inventory?.forEach {
|
||||
if (it.item.inventoryCategory == filter || filter == "__all__")
|
||||
inventorySortList.add(it)
|
||||
}
|
||||
|
||||
rebuildList = false
|
||||
|
||||
// sort if needed
|
||||
// test sort by name
|
||||
inventorySortList.sortBy { it.item.name }
|
||||
|
||||
// map sortList to item list
|
||||
for (k in 0..items.size - 1) {
|
||||
// we have an item
|
||||
try {
|
||||
val sortListItem = inventorySortList[k + itemsScrollOffset]
|
||||
items[k].item = sortListItem.item
|
||||
items[k].amount = sortListItem.amount
|
||||
items[k].itemImage = ItemCodex.getItemImage(sortListItem.item)
|
||||
|
||||
// set quickslot number
|
||||
for (qs in 1..QUICKSLOT_MAX) {
|
||||
if (-sortListItem.item.dynamicID == actorValue.getAsInt(AVKey.__PLAYER_QSPREFIX + qs)) {
|
||||
items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9
|
||||
break
|
||||
}
|
||||
else
|
||||
items[k].quickslot = null
|
||||
}
|
||||
|
||||
// set equippedslot number
|
||||
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
|
||||
}
|
||||
else
|
||||
items[k].equippedSlot = null
|
||||
}
|
||||
}
|
||||
}
|
||||
// we do not have an item, empty the slot
|
||||
catch (e: IndexOutOfBoundsException) {
|
||||
items[k].item = null
|
||||
items[k].amount = 0
|
||||
items[k].itemImage = null
|
||||
items[k].quickslot = null
|
||||
}
|
||||
}
|
||||
shutUpAndRebuild()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,13 +218,76 @@ class UIInventory(
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** Persuade the UI to rebuild its item list */
|
||||
fun rebuildList() {
|
||||
rebuildList = true
|
||||
}
|
||||
|
||||
|
||||
fun shutUpAndRebuild() {
|
||||
val filter = catButtonsToCatIdent[catButtons.selectedButton.labelText]
|
||||
|
||||
// encumbrance
|
||||
encumbrancePerc = inventory!!.capacity.toFloat() / inventory!!.maxCapacity
|
||||
isEncumbered = inventory!!.isEncumbered
|
||||
|
||||
|
||||
|
||||
inventorySortList = ArrayList<InventoryPair>()
|
||||
|
||||
// filter items
|
||||
inventory?.forEach {
|
||||
if (it.item.inventoryCategory == filter || filter == "__all__")
|
||||
inventorySortList.add(it)
|
||||
}
|
||||
|
||||
rebuildList = false
|
||||
|
||||
// sort if needed
|
||||
// test sort by name
|
||||
inventorySortList.sortBy { it.item.name }
|
||||
|
||||
// map sortList to item list
|
||||
for (k in 0..items.size - 1) {
|
||||
// we have an item
|
||||
try {
|
||||
val sortListItem = inventorySortList[k + itemsScrollOffset]
|
||||
items[k].item = sortListItem.item
|
||||
items[k].amount = sortListItem.amount
|
||||
items[k].itemImage = ItemCodex.getItemImage(sortListItem.item)
|
||||
|
||||
// set quickslot number
|
||||
for (qs in 1..QUICKSLOT_MAX) {
|
||||
if (sortListItem.item == actor?.inventory?.getQuickBar(qs - 1)?.item) {
|
||||
items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9
|
||||
break
|
||||
}
|
||||
else
|
||||
items[k].quickslot = null
|
||||
}
|
||||
|
||||
// set equippedslot number
|
||||
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
|
||||
}
|
||||
else
|
||||
items[k].equippedSlot = null
|
||||
}
|
||||
}
|
||||
}
|
||||
// we do not have an item, empty the slot
|
||||
catch (e: IndexOutOfBoundsException) {
|
||||
items[k].item = null
|
||||
items[k].amount = 0
|
||||
items[k].itemImage = null
|
||||
items[k].quickslot = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////
|
||||
@@ -312,19 +315,24 @@ class UIInventory(
|
||||
}
|
||||
|
||||
override fun keyPressed(key: Int, c: Char) {
|
||||
items.forEach { it.keyPressed(key, c) }
|
||||
items.forEach { if (it.mouseUp) it.keyPressed(key, c) }
|
||||
shutUpAndRebuild()
|
||||
}
|
||||
|
||||
override fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
|
||||
}
|
||||
|
||||
override fun keyReleased(key: Int, c: Char) {
|
||||
items.forEach { if (it.mouseUp) it.keyReleased(key, c) }
|
||||
shutUpAndRebuild()
|
||||
}
|
||||
|
||||
override fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
|
||||
}
|
||||
|
||||
override fun controllerButtonPressed(controller: Int, button: Int) {
|
||||
items.forEach { if (it.mouseUp) it.controllerButtonPressed(controller, button) }
|
||||
shutUpAndRebuild()
|
||||
}
|
||||
|
||||
override fun mousePressed(button: Int, x: Int, y: Int) {
|
||||
@@ -332,6 +340,8 @@ class UIInventory(
|
||||
}
|
||||
|
||||
override fun controllerButtonReleased(controller: Int, button: Int) {
|
||||
items.forEach { if (it.mouseUp) it.controllerButtonReleased(controller, button) }
|
||||
shutUpAndRebuild()
|
||||
}
|
||||
|
||||
override fun mouseReleased(button: Int, x: Int, y: Int) {
|
||||
|
||||
@@ -87,7 +87,7 @@ class UIPieMenu : UICanvas {
|
||||
itemImage, // using fixed CELL_SIZE for reasons
|
||||
slotX + (CELL_SIZE - itemW) / 2f,
|
||||
slotY + (CELL_SIZE - itemH) / 2f,
|
||||
Color(1f, 1f, 1f, handler!!.opacity * UIQuickBar.finalOpacity)
|
||||
Color(1f, 1f, 1f, handler!!.opacity)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ class UIQuickBar : UICanvas, MouseControlled {
|
||||
itemImage, // using fixed CELL_SIZE for reasons
|
||||
slotX + (CELL_SIZE - itemW) / 2f,
|
||||
slotY + (CELL_SIZE - itemH) / 2f,
|
||||
Color(1f, 1f, 1f, handler!!.opacity * UIQuickBar.finalOpacity)
|
||||
Color(1f, 1f, 1f, handler!!.opacity)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user