mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-16 05:24:06 +09:00
selecting and consuming item in inventory
This commit is contained in:
@@ -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?>(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
|
||||
|
||||
@@ -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?>(InventoryItem.EquipPosition.INDEX_MAX, { null })
|
||||
|
||||
/**
|
||||
* Sorted by referenceID.
|
||||
*/
|
||||
private val itemList = ArrayList<InventoryPair>()
|
||||
|
||||
/**
|
||||
* 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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
|
||||
@@ -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<InventoryItem?>
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user