mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 03:24:06 +09:00
loading player from json file
This commit is contained in:
@@ -9,11 +9,9 @@ import net.torvald.terrarum.AppLoader.printdbg
|
||||
import net.torvald.terrarum.gameactors.*
|
||||
import net.torvald.terrarum.gameactors.faction.Faction
|
||||
import net.torvald.terrarum.gameitem.GameItem
|
||||
import net.torvald.terrarum.gameworld.GameWorld
|
||||
import net.torvald.terrarum.itemproperties.ItemCodex
|
||||
import net.torvald.terrarum.itemproperties.Material
|
||||
import net.torvald.terrarum.realestate.LandUtil
|
||||
import net.torvald.terrarum.worlddrawer.LightmapRenderer
|
||||
import org.dyn4j.geometry.Vector2
|
||||
import java.util.*
|
||||
|
||||
@@ -28,12 +26,13 @@ import java.util.*
|
||||
*
|
||||
* Created by minjaesong on 2016-10-24.
|
||||
*/
|
||||
open class ActorHumanoid(
|
||||
birth: Long,
|
||||
death: Long? = null,
|
||||
physProp: PhysProperties = PhysProperties.HUMANOID_DEFAULT
|
||||
) : ActorWithBody(RenderOrder.MIDDLE, physProp = physProp), Controllable, Pocketed, Factionable, Luminous, LandHolder, HistoricalFigure {
|
||||
open class ActorHumanoid() : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, LandHolder, HistoricalFigure {
|
||||
|
||||
constructor(birth: Long, death: Long? = null, physProp: PhysProperties = PhysProperties.HUMANOID_DEFAULT) : this() {
|
||||
actorValue[AVKey.__HISTORICAL_BORNTIME] = birth
|
||||
death?.let { actorValue[AVKey.__HISTORICAL_DEADTIME] = death }
|
||||
this.physProp = physProp
|
||||
}
|
||||
|
||||
var vehicleRiding: Controllable? = null // usually player only
|
||||
|
||||
@@ -176,11 +175,6 @@ open class ActorHumanoid(
|
||||
override val material = Material()
|
||||
}
|
||||
|
||||
init {
|
||||
actorValue[AVKey.__HISTORICAL_BORNTIME] = birth
|
||||
death?.let { actorValue[AVKey.__HISTORICAL_DEADTIME] = death }
|
||||
}
|
||||
|
||||
override fun update(delta: Float) {
|
||||
super.update(delta)
|
||||
|
||||
@@ -217,11 +211,11 @@ open class ActorHumanoid(
|
||||
|
||||
// update inventory items
|
||||
inventory.forEach {
|
||||
if (!inventory.itemEquipped.contains(it.item)) { // unequipped
|
||||
ItemCodex[it.item]!!.effectWhileInPocket(delta)
|
||||
if (!inventory.itemEquipped.contains(it.itm)) { // unequipped
|
||||
ItemCodex[it.itm]!!.effectWhileInPocket(delta)
|
||||
}
|
||||
else { // equipped
|
||||
ItemCodex[it.item]!!.effectWhenEquipped(delta)
|
||||
ItemCodex[it.itm]!!.effectWhenEquipped(delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -640,7 +634,7 @@ open class ActorHumanoid(
|
||||
// make quickslot work
|
||||
if (key == AVKey.__PLAYER_QUICKSLOTSEL && value != null) {
|
||||
// ONLY FOR HAND_GRIPs!!
|
||||
val quickBarItem = ItemCodex[inventory.getQuickslot(actorValue.getAsInt(key)!!)?.item]
|
||||
val quickBarItem = ItemCodex[inventory.getQuickslot(actorValue.getAsInt(key)!!)?.itm]
|
||||
|
||||
if (quickBarItem != null && quickBarItem.equipPosition == GameItem.EquipPosition.HAND_GRIP) {
|
||||
equipItem(quickBarItem)
|
||||
|
||||
@@ -1,29 +1,30 @@
|
||||
package net.torvald.terrarum.modulebasegame.gameactors
|
||||
|
||||
import net.torvald.terrarum.AppLoader
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.blockproperties.BlockCodex
|
||||
import net.torvald.terrarum.gameactors.AVKey
|
||||
import net.torvald.terrarum.gameactors.Actor
|
||||
import net.torvald.terrarum.gameitem.GameItem
|
||||
import net.torvald.terrarum.itemproperties.ItemCodex
|
||||
import net.torvald.terrarum.gameitem.ItemID
|
||||
import net.torvald.terrarum.lock
|
||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||
import net.torvald.terrarum.modulebasegame.ui.UIQuickslotBar
|
||||
import java.math.BigInteger
|
||||
import java.util.*
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2016-03-15.
|
||||
*/
|
||||
|
||||
class ActorInventory(@Transient val actor: Pocketed, maxCapacity: Int, capacityMode: Int):
|
||||
FixtureInventory(maxCapacity, capacityMode) {
|
||||
class ActorInventory() : FixtureInventory() {
|
||||
|
||||
// FIXME unless absolutely necessary, don't store full item object; only store its dynamicID
|
||||
|
||||
@Transient lateinit var actor: Pocketed
|
||||
internal set
|
||||
|
||||
constructor(actor: Pocketed, maxCapacity: Int, capacityMode: Int) : this() {
|
||||
this.actor = actor
|
||||
this.maxCapacity = maxCapacity
|
||||
this.capacityMode = capacityMode
|
||||
}
|
||||
|
||||
/**
|
||||
* List of all equipped items (tools, armours, rings, necklaces, etc.)
|
||||
*/
|
||||
@@ -38,7 +39,7 @@ class ActorInventory(@Transient val actor: Pocketed, maxCapacity: Int, capacityM
|
||||
override fun remove(item: GameItem, count: Int) {
|
||||
super.remove(item, count) { existingItem ->
|
||||
// unequip, if applicable
|
||||
actor.unequipItem(existingItem.item)
|
||||
actor.unequipItem(existingItem.itm)
|
||||
// also unequip on the quickslot
|
||||
actor.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
|
||||
setQuickBar(it, null)
|
||||
|
||||
@@ -13,8 +13,16 @@ import java.util.concurrent.locks.ReentrantLock
|
||||
* Created by minjaesong on 2021-03-16.
|
||||
*/
|
||||
|
||||
open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
|
||||
|
||||
open class FixtureInventory() {
|
||||
|
||||
var maxCapacity = 100
|
||||
var capacityMode = CAPACITY_MODE_COUNT
|
||||
|
||||
constructor(maxCapacity: Int, capacityMode: Int) : this() {
|
||||
this.maxCapacity = maxCapacity
|
||||
this.capacityMode = capacityMode
|
||||
}
|
||||
|
||||
companion object {
|
||||
val CAPACITY_MODE_NO_ENCUMBER = 0
|
||||
val CAPACITY_MODE_COUNT = 1
|
||||
@@ -60,7 +68,7 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
|
||||
// if the item already exists
|
||||
if (existingItem != null) {
|
||||
// increment count
|
||||
existingItem.amount += count
|
||||
existingItem.qty += count
|
||||
}
|
||||
// new item
|
||||
else {
|
||||
@@ -90,14 +98,14 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
|
||||
|
||||
val existingItem = invSearchByDynamicID(item.dynamicID)
|
||||
if (existingItem != null) { // if the item already exists
|
||||
val newCount = existingItem.amount - count
|
||||
val newCount = existingItem.qty - count
|
||||
|
||||
if (newCount < 0) {
|
||||
throw Error("Tried to remove $count of $item, but the inventory only contains ${existingItem.amount} of them.")
|
||||
throw Error("Tried to remove $count of $item, but the inventory only contains ${existingItem.qty} of them.")
|
||||
}
|
||||
else if (newCount > 0) {
|
||||
// decrement count
|
||||
existingItem.amount = newCount
|
||||
existingItem.qty = newCount
|
||||
}
|
||||
else {
|
||||
// depleted item; remove entry from inventory
|
||||
@@ -128,12 +136,12 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
|
||||
else
|
||||
getTotalCount().toDouble()
|
||||
|
||||
fun getTotalWeight(): Double = itemList.map { ItemCodex[it.item]!!.mass * it.amount }.sum()
|
||||
fun getTotalWeight(): Double = itemList.map { ItemCodex[it.itm]!!.mass * it.qty }.sum()
|
||||
|
||||
/**
|
||||
* Real amount
|
||||
*/
|
||||
fun getTotalCount(): Int = itemList.map { it.amount }.sum()
|
||||
fun getTotalCount(): Int = itemList.map { it.qty }.sum()
|
||||
|
||||
/**
|
||||
* Unique amount, multiple items are calculated as one
|
||||
@@ -182,7 +190,7 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
|
||||
ReentrantLock().lock {
|
||||
var j = arr.lastIndex - 1
|
||||
val x = arr.last()
|
||||
while (j >= 0 && arr[j].item > x.item) {
|
||||
while (j >= 0 && arr[j].itm > x.itm) {
|
||||
arr[j + 1] = arr[j]
|
||||
j -= 1
|
||||
}
|
||||
@@ -200,9 +208,9 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
|
||||
val mid = (low + high).ushr(1) // safe from overflows
|
||||
|
||||
val midVal = if (searchMode == STATIC_ID)
|
||||
ItemCodex[this[mid].item]!!.originalID
|
||||
ItemCodex[this[mid].itm]!!.originalID
|
||||
else
|
||||
ItemCodex[this[mid].item]!!.dynamicID
|
||||
ItemCodex[this[mid].itm]!!.dynamicID
|
||||
|
||||
if (ID > midVal)
|
||||
low = mid + 1
|
||||
@@ -215,4 +223,19 @@ open class FixtureInventory(var maxCapacity: Int, var capacityMode: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
data class InventoryPair(val item: ItemID, var amount: Int)
|
||||
class InventoryPair {
|
||||
|
||||
var itm: ItemID = ""; private set
|
||||
var qty: Int = 0
|
||||
|
||||
private constructor()
|
||||
|
||||
constructor(item: ItemID, quantity: Int) : this() {
|
||||
itm = item
|
||||
qty = quantity
|
||||
}
|
||||
|
||||
operator fun component1() = itm
|
||||
operator fun component2() = qty
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package net.torvald.terrarum.modulebasegame.gameactors
|
||||
|
||||
import net.torvald.spriteanimation.HasAssembledSprite
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.gameactors.AVKey
|
||||
|
||||
|
||||
/**
|
||||
@@ -10,11 +11,17 @@ import net.torvald.terrarum.Terrarum
|
||||
* Created by minjaesong on 2015-12-31.
|
||||
*/
|
||||
|
||||
class IngamePlayer(
|
||||
override var animDescPath: String,
|
||||
override var animDescPathGlow: String? = null,
|
||||
born: Long
|
||||
) : ActorHumanoid(born), HasAssembledSprite {
|
||||
class IngamePlayer() : ActorHumanoid(), HasAssembledSprite {
|
||||
|
||||
override var animDescPath = "invalid"
|
||||
override var animDescPathGlow: String? = null
|
||||
|
||||
|
||||
constructor(animDescPath: String, animDescPathGlow: String?, born: Long) : this() {
|
||||
this.animDescPath = animDescPath
|
||||
this.animDescPathGlow = animDescPathGlow
|
||||
actorValue[AVKey.__HISTORICAL_BORNTIME] = born
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new Player instance with empty elements (sprites, actorvalue, etc.).
|
||||
|
||||
@@ -16,9 +16,9 @@ class ThreadActorUpdate(val startIndex: Int, val endIndex: Int) : Callable<Unit>
|
||||
|
||||
if (it is Pocketed) {
|
||||
it.inventory.forEach { inventoryEntry ->
|
||||
ItemCodex[inventoryEntry.item]?.effectWhileInPocket(AppLoader.UPDATE_RATE)
|
||||
if (it.equipped(inventoryEntry.item)) {
|
||||
ItemCodex[inventoryEntry.item]?.effectWhenEquipped(AppLoader.UPDATE_RATE)
|
||||
ItemCodex[inventoryEntry.itm]?.effectWhileInPocket(AppLoader.UPDATE_RATE)
|
||||
if (it.equipped(inventoryEntry.itm)) {
|
||||
ItemCodex[inventoryEntry.itm]?.effectWhenEquipped(AppLoader.UPDATE_RATE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user