equip item by body parts

Former-commit-id: 34294de21d16c03da98427edae3b3c6ed94088aa
Former-commit-id: b37fe27635b1643e72e8f714bfea6ce214006931
This commit is contained in:
Song Minjae
2016-12-16 18:56:13 +09:00
parent 1d1769a2c3
commit 5dc99f5612
10 changed files with 90 additions and 56 deletions

View File

@@ -4,6 +4,7 @@ import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.ActorInventory import net.torvald.terrarum.gameactors.ActorInventory
import net.torvald.terrarum.gameactors.Player import net.torvald.terrarum.gameactors.Player
import net.torvald.terrarum.gameactors.Pocketed import net.torvald.terrarum.gameactors.Pocketed
import net.torvald.terrarum.gameitem.EquipPosition
import net.torvald.terrarum.itemproperties.ItemPropCodex import net.torvald.terrarum.itemproperties.ItemPropCodex
/** /**
@@ -22,7 +23,7 @@ internal object Inventory : ConsoleCommand {
"list" -> listInventory() "list" -> listInventory()
"add" -> addItem(args[2].toInt(), args[3].toInt()) "add" -> addItem(args[2].toInt(), args[3].toInt())
"target" -> setTarget(args[2].toInt()) "target" -> setTarget(args[2].toInt())
"hold" -> holdItem(args[2].toInt()) "equip" -> equipItem(args[2].toInt())
else -> printUsage() else -> printUsage()
} }
} }
@@ -56,19 +57,20 @@ internal object Inventory : ConsoleCommand {
target.inventory.add(ItemPropCodex.getProp(refId), amount) target.inventory.add(ItemPropCodex.getProp(refId), amount)
} }
private fun holdItem(refId: Int) { private fun equipItem(refId: Int) {
val item = ItemPropCodex.getProp(refId)
// if the item does not exist, add it first // if the item does not exist, add it first
if (!target.inventory.contains(refId)) { if (!target.inventory.contains(item)) {
target.inventory.add(refId) target.inventory.add(item)
} }
target.itemHolding = ItemPropCodex.getProp(refId) target.itemEquipped[item.equipPosition] = item
} }
override fun printUsage() { override fun printUsage() {
Echo("Usage: inventory command arguments") Echo("Usage: inventory command arguments")
Echo("Available commands:") Echo("Available commands:")
Echo("list | assign slot | add itemid [amount] | target [actorid] | hold itemid") Echo("list | assign slot | add itemid [amount] | target [actorid] | equip itemid")
Echo("equip itemid")
} }
} }

View File

@@ -6,6 +6,7 @@ import net.torvald.terrarum.gameactors.*
import net.torvald.terrarum.gameactors.faction.Faction import net.torvald.terrarum.gameactors.faction.Faction
import net.torvald.terrarum.gamecontroller.EnumKeyFunc import net.torvald.terrarum.gamecontroller.EnumKeyFunc
import net.torvald.terrarum.gamecontroller.KeyMap import net.torvald.terrarum.gamecontroller.KeyMap
import net.torvald.terrarum.gameitem.EquipPosition
import net.torvald.terrarum.gameitem.InventoryItem import net.torvald.terrarum.gameitem.InventoryItem
import net.torvald.terrarum.gameitem.InventoryItemAdapter import net.torvald.terrarum.gameitem.InventoryItemAdapter
import net.torvald.terrarum.realestate.RealEstateUtility import net.torvald.terrarum.realestate.RealEstateUtility
@@ -27,8 +28,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
/** Must be set by PlayerFactory */ /** Must be set by PlayerFactory */
override var inventory: ActorInventory = ActorInventory() override var inventory: ActorInventory = ActorInventory()
override var itemHolding: InventoryItem? = null override val itemEquipped = Array<InventoryItem?>(EquipPosition.INDEX_MAX + 1, { null })
override val itemEquipped = ArrayList<InventoryItem>()
/** Must be set by PlayerFactory */ /** Must be set by PlayerFactory */
override var faction: HashSet<Faction> = HashSet() override var faction: HashSet<Faction> = HashSet()
@@ -136,6 +136,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
private val nullItem = object : InventoryItemAdapter() { private val nullItem = object : InventoryItemAdapter() {
override val itemID: Int = 0 override val itemID: Int = 0
override val equipPosition: Int = EquipPosition.NULL
override var mass: Double = 0.0 override var mass: Double = 0.0
override var scale: Double = 1.0 override var scale: Double = 1.0
} }
@@ -211,12 +212,12 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
*/ */
// Left mouse // Left mouse
if (isGamer && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) { if (isGamer && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
(itemHolding ?: nullItem).primaryUse(gc, delta) (itemEquipped[EquipPosition.HAND_GRIP] ?: nullItem).primaryUse(gc, delta)
} }
// Right mouse // Right mouse
if (isGamer && input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)) { if (isGamer && input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)) {
(itemHolding ?: nullItem).secondaryUse(gc, delta) (itemEquipped[EquipPosition.HAND_GRIP] ?: nullItem).secondaryUse(gc, delta)
} }
/** /**

View File

@@ -26,7 +26,7 @@ class DroppedItem(private val item: InventoryItem) : ActorWithBody() {
} }
override fun update(gc: GameContainer, delta: Int) { override fun update(gc: GameContainer, delta: Int) {
item.effectWhenTakenOut(gc, delta) item.effectWhenEquipped(gc, delta)
} }
override fun drawBody(gc: GameContainer, g: Graphics) { override fun drawBody(gc: GameContainer, g: Graphics) {

View File

@@ -1,6 +1,7 @@
package net.torvald.terrarum.gameactors package net.torvald.terrarum.gameactors
import net.torvald.terrarum.gameactors.ActorHumanoid import net.torvald.terrarum.gameactors.ActorHumanoid
import net.torvald.terrarum.gameitem.EquipPosition
import net.torvald.terrarum.gameitem.InventoryItem import net.torvald.terrarum.gameitem.InventoryItem
import net.torvald.terrarum.gameitem.InventoryItemAdapter import net.torvald.terrarum.gameitem.InventoryItemAdapter
import org.luaj.vm2.Globals import org.luaj.vm2.Globals
@@ -42,6 +43,7 @@ open class HumanoidNPC(aiFile: String, born: GameDate) : ActorHumanoid(born), AI
// we're having InventoryItem data so that this class could be somewhat universal // we're having InventoryItem data so that this class could be somewhat universal
override var itemData: InventoryItem = object : InventoryItemAdapter() { override var itemData: InventoryItem = object : InventoryItemAdapter() {
override var itemID = referenceID override var itemID = referenceID
override val equipPosition: Int = EquipPosition.HAND_GRIP
override var mass: Double override var mass: Double
get() = actorValue.getAsDouble(AVKey.BASEMASS)!! get() = actorValue.getAsDouble(AVKey.BASEMASS)!!

View File

@@ -6,6 +6,7 @@ import net.torvald.spriteanimation.SpriteAnimation
import com.google.gson.JsonObject import com.google.gson.JsonObject
import net.torvald.terrarum.gameactors.ActorHumanoid import net.torvald.terrarum.gameactors.ActorHumanoid
import net.torvald.terrarum.gameactors.faction.FactionFactory import net.torvald.terrarum.gameactors.faction.FactionFactory
import net.torvald.terrarum.gameitem.EquipPosition
import net.torvald.terrarum.itemproperties.ItemPropCodex import net.torvald.terrarum.itemproperties.ItemPropCodex
import net.torvald.terrarum.mapdrawer.MapDrawer import net.torvald.terrarum.mapdrawer.MapDrawer
import org.newdawn.slick.SlickException import org.newdawn.slick.SlickException
@@ -61,7 +62,7 @@ object PlayerBuilderSigrid {
p.actorValue[AVKey.BASEDEFENCE] = 141 p.actorValue[AVKey.BASEDEFENCE] = 141
p.actorValue[AVKey.__PLAYER_QUICKBARSEL] = 0 p.actorValue[AVKey.__PLAYER_QUICKBARSEL] = 0
p.actorValue["__selectedtile"] = 147 // test code; replace with <tile_item>.primaryUse(gc, delta) //p.actorValue["__selectedtile"] = 147 // test code; replace with <tile_item>.primaryUse(gc, delta)
p.actorValue["__aimhelper"] = true // TODO when you'll gonna implement it? p.actorValue["__aimhelper"] = true // TODO when you'll gonna implement it?
p.setHitboxDimension(15, p.actorValue.getAsInt(AVKey.BASEHEIGHT)!!, 10, 0) p.setHitboxDimension(15, p.actorValue.getAsInt(AVKey.BASEHEIGHT)!!, 10, 0)
@@ -76,7 +77,7 @@ object PlayerBuilderSigrid {
// Test fill up inventory // Test fill up inventory
p.inventory.add(16) p.inventory.add(16)
p.itemHolding = ItemPropCodex.getProp(16) p.itemEquipped[EquipPosition.HAND_GRIP] = ItemPropCodex.getProp(16)

View File

@@ -10,13 +10,9 @@ interface Pocketed {
var inventory: ActorInventory var inventory: ActorInventory
/** Item currentry holding, like tools/weapons/scrolls/magic/etc.
* Null if not holding anything
*/
var itemHolding: InventoryItem?
/** /**
* List of all equipped items (tools, armours, rings, necklaces, etc.) * List of all equipped items (tools, armours, rings, necklaces, etc.)
*/ */
val itemEquipped: ArrayList<InventoryItem> val itemEquipped: Array<InventoryItem?>
} }

View File

@@ -12,7 +12,8 @@ import org.newdawn.slick.GameContainer
* *
* Created by minjaesong on 16-09-08. * Created by minjaesong on 16-09-08.
*/ */
open class DynamicItem(val baseItemID: Int, val newMass: Double? = null, val newScale: Double? = null) : InventoryItem { open class DynamicItem(val baseItemID: Int?, newMass: Double? = null, newScale: Double? = null)
: InventoryItemAdapter() {
/** /**
* Internal ID of an Item, Long * Internal ID of an Item, Long
@@ -23,6 +24,13 @@ open class DynamicItem(val baseItemID: Int, val newMass: Double? = null, val new
*/ */
override val itemID: Int = generateUniqueDynamicItemID() override val itemID: Int = generateUniqueDynamicItemID()
override val equipPosition: Int = // default to HAND_GRIP if no baseItemID given
if (baseItemID != null)
ItemPropCodex.getProp(baseItemID).equipPosition
else
EquipPosition.HAND_GRIP
private fun generateUniqueDynamicItemID(): Int { private fun generateUniqueDynamicItemID(): Int {
var ret: Int var ret: Int
do { do {
@@ -55,38 +63,18 @@ open class DynamicItem(val baseItemID: Int, val newMass: Double? = null, val new
init { init {
// set mass to the value from item codex using baseItemID // set mass to the value from item codex using baseItemID
// if you forget this, the game will throw NullPointerException! if (baseItemID == null) {
if (newMass != null) mass = newMass mass = newMass!!
if (newScale != null) scale = newScale }
else {
mass = newMass ?: ItemPropCodex.getProp(baseItemID).mass
}
if (baseItemID == null) {
scale = newScale!!
}
else {
scale = newScale ?: ItemPropCodex.getProp(baseItemID).scale
}
} }
/**
* Effects applied continuously while in pocket
*/
override fun effectWhileInPocket(gc: GameContainer, delta: Int) { }
/**
* Effects applied immediately only once if picked up
*/
override fun effectWhenPickedUp(gc: GameContainer, delta: Int) { }
/**
* Effects applied (continuously or not) while primary button (usually left mouse button) is down
*/
override fun primaryUse(gc: GameContainer, delta: Int) { }
/**
* Effects applied (continuously or not) while secondary button (usually right mouse button) is down
*/
override fun secondaryUse(gc: GameContainer, delta: Int) { }
/**
* Effects applied immediately only once if thrown from pocket
*/
override fun effectWhenThrown(gc: GameContainer, delta: Int) { }
/**
* Effects applied (continuously or not) while thrown to the world
*/
override fun effectWhenTakenOut(gc: GameContainer, delta: Int) { }
} }

View File

@@ -15,6 +15,11 @@ interface InventoryItem {
*/ */
val itemID: Int val itemID: Int
/**
* Where to equip the item
*/
val equipPosition: Int
/** /**
* Base mass of the item. Real mass must be calculated from * Base mass of the item. Real mass must be calculated from
* mass * scale^3 * mass * scale^3
@@ -55,7 +60,39 @@ interface InventoryItem {
fun effectWhenThrown(gc: GameContainer, delta: Int) fun effectWhenThrown(gc: GameContainer, delta: Int)
/** /**
* Effects applied (continuously or not) while thrown to the world * Effects applied (continuously or not) when equipped
*/ */
fun effectWhenTakenOut(gc: GameContainer, delta: Int) fun effectWhenEquipped(gc: GameContainer, delta: Int)
/**
* Effects applied only once when unequipped
*/
fun effectWhenUnEquipped(gc: GameContainer, delta: Int)
}
object EquipPosition {
const val NULL = -1
const val ARMOUR = 0
// you can add alias to address something like LEGGINGS, BREASTPLATE, RINGS, NECKLACES, etc.
const val BODY_BACK = 1 // wings, jetpacks, etc.
const val BODY_BUFF2 = 2
const val BODY_BUFF3 = 3
const val BODY_BUFF4 = 4
const val BODY_BUFF5 = 5
const val BODY_BUFF6 = 6
const val BODY_BUFF7 = 7
const val BODY_BUFF8 = 8
const val HAND_GRIP = 9
const val HAND_GAUNTLET = 10
const val HAND_BUFF2 = 11
const val HAND_BUFF3 = 12
const val HAND_BUFF4 = 13
const val FOOTWEAR = 14
const val HEADGEAR = 15
const val INDEX_MAX = 15
} }

View File

@@ -7,6 +7,7 @@ import org.newdawn.slick.GameContainer
*/ */
abstract class InventoryItemAdapter : InventoryItem { abstract class InventoryItemAdapter : InventoryItem {
override abstract val itemID: Int override abstract val itemID: Int
override abstract val equipPosition: Int
override abstract var mass: Double override abstract var mass: Double
override abstract var scale: Double override abstract var scale: Double
@@ -25,6 +26,9 @@ abstract class InventoryItemAdapter : InventoryItem {
override fun effectWhenThrown(gc: GameContainer, delta: Int) { override fun effectWhenThrown(gc: GameContainer, delta: Int) {
} }
override fun effectWhenTakenOut(gc: GameContainer, delta: Int) { override fun effectWhenEquipped(gc: GameContainer, delta: Int) {
}
override fun effectWhenUnEquipped(gc: GameContainer, delta: Int) {
} }
} }

View File

@@ -5,8 +5,10 @@ import net.torvald.terrarum.KVHashMap
import net.torvald.terrarum.gameactors.CanBeAnItem import net.torvald.terrarum.gameactors.CanBeAnItem
import net.torvald.terrarum.gameitem.InventoryItem import net.torvald.terrarum.gameitem.InventoryItem
import net.torvald.terrarum.Terrarum import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gamecontroller.mouseTileX import net.torvald.terrarum.gamecontroller.mouseTileX
import net.torvald.terrarum.gamecontroller.mouseTileY import net.torvald.terrarum.gamecontroller.mouseTileY
import net.torvald.terrarum.gameitem.EquipPosition
import net.torvald.terrarum.gameitem.InventoryItemAdapter import net.torvald.terrarum.gameitem.InventoryItemAdapter
import net.torvald.terrarum.gameworld.GameWorld import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.tileproperties.TileProp import net.torvald.terrarum.tileproperties.TileProp
@@ -39,6 +41,7 @@ object ItemPropCodex {
for (i in 0..ITEM_TILE_MAX) { for (i in 0..ITEM_TILE_MAX) {
itemCodex[i] = object : InventoryItemAdapter() { itemCodex[i] = object : InventoryItemAdapter() {
override val itemID: Int = i override val itemID: Int = i
override val equipPosition = EquipPosition.HAND_GRIP
override var mass: Double = TilePropCodex.getProp(i).density / 1000.0 override var mass: Double = TilePropCodex.getProp(i).density / 1000.0
// no need to set setter as scale would not change // no need to set setter as scale would not change
override var scale: Double = 1.0 override var scale: Double = 1.0