quickslot works, new event for actor "actorValueChanged"

- "unpacking" fresh new tool would un-register quickslot desig.
- priority issue on "equipped by quickslot change" and "equipped by inventory UI"
This commit is contained in:
Song Minjae
2017-04-28 12:21:27 +09:00
parent 253260de6f
commit 93939f22bf
34 changed files with 215 additions and 61 deletions

View File

@@ -0,0 +1,88 @@
import net.torvald.terrarum.itemproperties.InventoryItem
import net.torvald.terrarum.itemproperties.Material
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
/**
* Created by SKYHi14 on 2017-04-28.
*/
static InventoryItem invoke(int id) {
return new InventoryItem() {
@Override
int getDynamicID() {
return 0
}
@Override
void setDynamicID(int i) {
}
@Override
int getOriginalID() {
return 0
}
@Override
boolean isUnique() {
return false
}
@Override
protected String getOriginalName() {
return null
}
@Override
double getBaseMass() {
return 0
}
@Override
void setBaseMass(double v) {
}
@Override
Double getBaseToolSize() {
return null
}
@Override
void setBaseToolSize(@Nullable Double aDouble) {
}
@Override
String getInventoryCategory() {
return null
}
@Override
void setInventoryCategory(@NotNull String s) {
}
@Override
boolean getStackable() {
return false
}
@Override
void setStackable(boolean b) {
}
@Override
boolean isDynamic() {
return false
}
@Override
Material getMaterial() {
return null
}
}
}

Binary file not shown.

View File

@@ -35,7 +35,7 @@ class GameFontImpl(noShadow: Boolean = false) : GameFontBase(noShadow = noShadow
GameFontBase.uniPunct = SpriteSheet(
"./assets/graphics/fonts/unipunct.tga", GameFontBase.W_LATIN_WIDE, GameFontBase.H)
GameFontBase.uniHan = SpriteSheet(
"./assets/graphics/fonts/wenquanyi.tga.gz", 16, 16)
"./assets/graphics/fonts/wenquanyi.tga.gz", 16, 16) // ~32 MB
GameFontBase.greekSheet = SpriteSheet(
"./assets/graphics/fonts/greek_variable.tga", 15, 19, 1)
GameFontBase.thaiSheet = SpriteSheet(

View File

@@ -1,28 +1,28 @@
package net.torvald.terrarum
import com.google.gson.JsonPrimitive
import net.torvald.terrarum.gameactors.Actor
import java.util.*
import java.util.function.Consumer
import kotlin.collections.HashMap
typealias ActorValue = KVHashMap
typealias ItemValue = KVHashMap
typealias GameConfig = KVHashMap
/**
* Created by minjaesong on 15-12-30.
*/
class KVHashMap {
open class KVHashMap {
constructor() {
hashMap = HashMap<String, Any>()
}
private constructor(newMap: HashMap<String, Any>) {
protected constructor(newMap: HashMap<String, Any>) {
hashMap = newMap
}
private val hashMap: HashMap<String, Any>
protected var hashMap: HashMap<String, Any>
/**
* Add key-value pair to the configuration table.
@@ -33,7 +33,7 @@ class KVHashMap {
* *
* @param value
*/
operator fun set(key: String, value: Any) {
open operator fun set(key: String, value: Any) {
hashMap.put(key.toLowerCase(), value)
}
@@ -99,12 +99,13 @@ class KVHashMap {
val keySet: Set<Any>
get() = hashMap.keys
fun remove(key: String) {
if (hashMap[key] != null)
open fun remove(key: String) {
if (hashMap[key] != null) {
hashMap.remove(key, hashMap[key]!!)
}
}
fun clone(): KVHashMap {
open fun clone(): KVHashMap {
val cloneOfMap = hashMap.clone() as HashMap<String, Any>
return KVHashMap(cloneOfMap)
}

View File

@@ -269,7 +269,7 @@ class StateInGame : BasicGameState() {
////////////////////////////
FeaturesDrawer.update(gc, delta)
WorldCamera.update()
BlocksDrawer.update()
///////////////////////////

View File

@@ -27,6 +27,9 @@ class StateUITest : BasicGameState() {
TODO("not implemented")
}
override fun actorValueChanged(key: String, value: Any?) {
}
override var inventory: ActorInventory = ActorInventory(this, 100, ActorInventory.CAPACITY_MODE_WEIGHT)
}

View File

@@ -50,8 +50,6 @@ object Terrarum : StateBasedGame(GAME_NAME) {
val HALFH: Int
get() = HEIGHT.ushr(1)
val QUICKSLOT_MAX = 10
/**
* To be used with physics simulator
*/

View File

@@ -7,7 +7,7 @@ import java.util.*
/**
* Created by minjaesong on 2016-12-29.
*/
object AVTracker : ConsoleCommand {
internal object AVTracker : ConsoleCommand {
private val jPanelInstances = ArrayList<ActorValueTracker>()
override fun execute(args: Array<String>) {

View File

@@ -7,7 +7,7 @@ import java.util.*
/**
* Created by minjaesong on 2016-12-29.
*/
object ActorsList : ConsoleCommand {
internal object ActorsList : ConsoleCommand {
private val jPanelInstances = ArrayList<ActorsLister>()
override fun execute(args: Array<String>) {

View File

@@ -1,7 +1,5 @@
package net.torvald.terrarum.console
import net.torvald.terrarum.Terrarum
import java.util.HashMap
/**
@@ -9,7 +7,7 @@ import java.util.HashMap
*/
object CommandDict {
internal var dict: HashMap<String, ConsoleCommand> = hashMapOf(
internal val dict: HashMap<String, ConsoleCommand> = hashMapOf(
Pair("echo", Echo),
Pair("error", EchoError),
Pair("setav", SetAV),
@@ -60,4 +58,8 @@ object CommandDict {
operator fun get(commandName: String): ConsoleCommand {
return dict[commandName]!!
}
fun add(name: String, obj: ConsoleCommand) {
dict[name] = obj
}
}

View File

@@ -13,7 +13,7 @@ import java.util.regex.Pattern
/**
* Created by minjaesong on 16-01-15.
*/
object CommandInterpreter {
internal object CommandInterpreter {
private val commandsNoAuth = arrayOf(
"auth",
@@ -31,7 +31,7 @@ object CommandInterpreter {
private val ccY = GameFontBase.colToCode["y"]
private val ccR = GameFontBase.colToCode["r"]
fun execute(command: String) {
internal fun execute(command: String) {
val cmd: Array<CommandInput?> = parse(command)
val error = Error()

View File

@@ -11,7 +11,6 @@ interface ConsoleCommand {
*
* e.g. in ```setav mass 74```, zeroth args will be ```setav```.
*/
@Throws(Exception::class)
fun execute(args: Array<String>)
fun printUsage()

View File

@@ -8,7 +8,7 @@ import javax.swing.JFrame
* Created by SKYHi14 on 2017-02-05.
*/
object JavaIMTest : ConsoleCommand {
internal object JavaIMTest : ConsoleCommand {
override fun execute(args: Array<String>) {
IMStringReader(

View File

@@ -6,7 +6,7 @@ import net.torvald.terrarum.langpack.Lang
/**
* Created by SKYHi14 on 2017-01-31.
*/
object KillActor : ConsoleCommand {
internal object KillActor : ConsoleCommand {
override fun execute(args: Array<String>) {
if (args.size == 2) {
try {

View File

@@ -6,7 +6,7 @@ import net.torvald.terrarum.gameactors.ActorWithPhysics
/**
* Created by minjaesong on 2017-01-20.
*/
object SetScale : ConsoleCommand {
internal object SetScale : ConsoleCommand {
override fun execute(args: Array<String>) {
if (args.size == 2 || args.size == 3) {
try {

View File

@@ -8,7 +8,7 @@ import java.io.FileInputStream
/**
* Created by minjaesong on 2017-01-14.
*/
object SpawnTapestry : ConsoleCommand {
internal object SpawnTapestry : ConsoleCommand {
override fun execute(args: Array<String>) {
if (args.size < 2) {
printUsage()

View File

@@ -8,7 +8,7 @@ import net.torvald.terrarum.gamecontroller.mouseY
/**
* Created by minjaesong on 2016-12-17.
*/
object SpawnTikiTorch : ConsoleCommand {
internal object SpawnTikiTorch : ConsoleCommand {
override fun execute(args: Array<String>) {
val torch = FixtureTikiTorch()
torch.setPosition(Terrarum.appgc.mouseX, Terrarum.appgc.mouseY)

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum.debuggerapp
import net.torvald.terrarum.ActorValue
import net.torvald.terrarum.gameactors.ActorValue
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.console.SetAV

View File

@@ -1,7 +1,7 @@
package net.torvald.terrarum.gameactors
import net.torvald.random.HQRNG
import net.torvald.terrarum.ActorValue
import net.torvald.terrarum.gameactors.ActorValue
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.itemproperties.ItemCodex.ACTORID_MIN
@@ -37,7 +37,7 @@ abstract class Actor(val renderOrder: RenderOrder) : Comparable<Actor>, Runnable
* @return Reference ID. (16777216-0x7FFF_FFFF)
*/
open var referenceID: ActorID = generateUniqueReferenceID()
var actorValue = ActorValue()
var actorValue = ActorValue(this)
@Volatile var flagDespawn = false
override fun equals(other: Any?) = referenceID == (other as Actor).referenceID
@@ -79,4 +79,10 @@ abstract class Actor(val renderOrder: RenderOrder) : Comparable<Actor>, Runnable
return ret
}
/**
* ActorValue change event handler
*
* @param value null if the key is deleted
*/
abstract fun actorValueChanged(key: String, value: Any?)
}

View File

@@ -473,6 +473,21 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
}
}
override fun actorValueChanged(key: String, value: Any?) {
// quickslot implementation
if (key == AVKey.__PLAYER_QUICKSLOTSEL && value != null) {
// ONLY FOR HAND_GRIPs!!
val quickBarItem = inventory.getQuickBar(actorValue.getAsInt(key)!!)?.item
if (quickBarItem == null) {
unequipSlot(InventoryItem.EquipPosition.HAND_GRIP)
}
else if (quickBarItem.equipPosition == InventoryItem.EquipPosition.HAND_GRIP) {
equipItem(quickBarItem)
}
}
}
fun isNoClip(): Boolean {
return noClip
}

View File

@@ -0,0 +1,29 @@
package net.torvald.terrarum.gameactors
import net.torvald.terrarum.KVHashMap
/**
* Created by SKYHi14 on 2017-04-28.
*/
class ActorValue(val actor: Actor) : KVHashMap() {
private constructor(actor: Actor, newMap: HashMap<String, Any>): this(actor) {
hashMap = newMap
}
override fun set(key: String, value: Any) {
super.set(key, value)
actor.actorValueChanged(key, value) // fire the event handler
}
override fun remove(key: String) {
if (hashMap[key] != null) {
hashMap.remove(key, hashMap[key]!!)
actor.actorValueChanged(key, null)
}
}
fun clone(newActor: Actor): ActorValue {
return ActorValue(newActor, hashMap)
}
}

View File

@@ -1124,6 +1124,13 @@ open class ActorWithPhysics(renderOrder: RenderOrder, val immobileBody: Boolean
}
}
override fun actorValueChanged(key: String, value: Any?) {
// do nothing
}
private fun clampW(x: Double): Double =
if (x < TILE_SIZE + nextHitbox.width / 2) {
TILE_SIZE + nextHitbox.width / 2

View File

@@ -7,7 +7,7 @@ import java.util.*
/**
* Created by minjaesong on 16-06-17.
*/
class FixtureTikiTorch : FixtureBase(), Luminous {
internal class FixtureTikiTorch : FixtureBase(), Luminous {
override var luminosity: Int
get() = BlockCodex[Block.TORCH].luminosity

View File

@@ -4,7 +4,7 @@ import net.torvald.JsonFetcher
import net.torvald.random.Fudge3
import net.torvald.terrarum.langpack.Lang
import com.google.gson.JsonObject
import net.torvald.terrarum.ActorValue
import net.torvald.terrarum.gameactors.ActorValue
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.gameactors.ActorHumanoid
import org.newdawn.slick.SlickException

View File

@@ -1,6 +1,5 @@
package net.torvald.terrarum.gameactors
import net.torvald.terrarum.ui.UIQuickBar
import org.newdawn.slick.GameContainer
/**

View File

@@ -1,6 +1,6 @@
package net.torvald.terrarum.gameactors
import net.torvald.terrarum.ActorValue
import net.torvald.terrarum.gameactors.ActorValue
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.gameactors.faction.FactionFactory
import net.torvald.terrarum.itemproperties.ItemCodex
@@ -28,7 +28,6 @@ object PlayerBuilderSigrid {
p.spriteGlow!!.delay = 200
p.spriteGlow!!.setRowsAndFrames(1, 1)
p.actorValue = ActorValue()
p.actorValue[AVKey.SCALE] = 1.0
p.actorValue[AVKey.SPEED] = 4.0
p.actorValue[AVKey.SPEEDBUFF] = 1.0

View File

@@ -14,7 +14,9 @@ interface Pocketed {
/**
* Equips an item. If the item is not in the inventory, an error will be thrown.
*/
fun unequipItem(item: InventoryItem) {
fun unequipItem(item: InventoryItem?) {
if (item == null) return
if (item.equipPosition == InventoryItem.EquipPosition.NULL)
throw Error("Unequipping the item that cannot be equipped in the first place")
@@ -28,6 +30,14 @@ interface Pocketed {
item.effectOnUnequip(Terrarum.appgc, Terrarum.delta)
}
// no need for equipSlot(Int)
fun unequipSlot(slot: Int) {
if (slot < 0 || slot > InventoryItem.EquipPosition.INDEX_MAX)
throw IllegalArgumentException("Slot index out of range: $slot")
unequipItem(inventory.itemEquipped[slot])
}
/**
* Equips an item. If the item is not in the inventory, adds the item first.
*/

View File

@@ -1,4 +1,4 @@
package net.torvald.serialise
package net.torvald.terrarum.serialise
import java.io.IOException
import java.io.InputStream
@@ -6,9 +6,10 @@ import java.io.InputStream
/**
* Created by minjaesong on 16-08-24.
*/
object ReadGameMapData {
// internal for everything: prevent malicious module from messing up the savedata
internal object ReadGameMapData {
fun InputStream.readRelative(b: ByteArray, off: Int, len: Int): Int {
internal fun InputStream.readRelative(b: ByteArray, off: Int, len: Int): Int {
if (b == null) {
throw NullPointerException()
} else if (off < 0 || len < 0 || len > b.size) {

View File

@@ -1,4 +1,4 @@
package net.torvald.serialise
package net.torvald.terrarum.serialise
import net.torvald.terrarum.Terrarum
import java.io.IOException
@@ -9,12 +9,13 @@ import java.nio.file.StandardCopyOption
/**
* Created by minjaesong on 16-03-18.
*/
object WriteCSV {
// internal for everything: prevent malicious module from messing up the savedata
internal object WriteCSV {
val META_FILENAME_TILE = "worldinfo2"
val META_FILENAME_ITEM = "worldinfo3"
val META_FILENAME_MAT = "worldinfo4"
fun write(saveDirectoryName: String): Boolean {
internal fun write(saveDirectoryName: String): Boolean {
//val tileCSV = CSVFetcher.readCSVasString(BlockCodex.CSV_PATH)
//val itemCSV = CSVFetcher.readCSVasString(ItemCodex.CSV_PATH)
//val matCSV = CSVFetcher.readCSVasString(MaterialCodex.CSV_PATH)

View File

@@ -1,18 +1,18 @@
package net.torvald.serialise
package net.torvald.terrarum.serialise
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.Terrarum
import java.io.IOException
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
/**
* Created by minjaesong on 16-03-18.
*/
object WriteGameMapData {
// internal for everything: prevent malicious module from messing up the savedata
internal object WriteGameMapData {
val META_FILENAME = "worldinfo1"
@@ -21,7 +21,7 @@ object WriteGameMapData {
val BYTE_NULL: Byte = 0
fun write(saveDirectoryName: String): Boolean {
internal fun write(saveDirectoryName: String): Boolean {
val path = Paths.get("${Terrarum.defaultSaveDir}" +
"/$saveDirectoryName/${WriteMeta.META_FILENAME}")
val tempPath = Files.createTempFile(path.toString(), "_temp")

View File

@@ -1,4 +1,4 @@
package net.torvald.serialise
package net.torvald.terrarum.serialise
import net.torvald.terrarum.worldgenerator.WorldGenerator
import net.torvald.terrarum.worldgenerator.RoguelikeRandomiser
@@ -7,7 +7,8 @@ import java.nio.charset.Charset
/**
* Created by minjaesong on 16-03-15.
*/
object WriteMeta {
// internal for everything: prevent malicious module from messing up the savedata
internal object WriteMeta {
val META_FILENAME = "world"
@@ -24,7 +25,7 @@ object WriteMeta {
* @param saveDirectoryName
* @param savegameName -- Nullable. If the value is not specified, saveDirectoryName will be used instead.
*/
fun write(saveDirectoryName: String, savegameName: String?): Boolean {
internal fun write(saveDirectoryName: String, savegameName: String?): Boolean {
/*val hashArray: ArrayList<ByteArray> = ArrayList()
val savenameAsByteArray: ByteArray =
(savegameName ?: saveDirectoryName).toByteArray(Charsets.UTF_8)

View File

@@ -1,7 +1,6 @@
package net.torvald.terrarum.ui
import net.torvald.terrarum.*
import net.torvald.terrarum.Terrarum.QUICKSLOT_MAX
import net.torvald.terrarum.Terrarum.joypadLabelNinA
import net.torvald.terrarum.Terrarum.joypadLabelNinY
import net.torvald.terrarum.gameactors.*
@@ -257,7 +256,7 @@ class UIInventory(
items[k].itemImage = ItemCodex.getItemImage(sortListItem.item)
// set quickslot number
for (qs in 1..QUICKSLOT_MAX) {
for (qs in 1..UIQuickBar.SLOT_COUNT) {
if (sortListItem.item == actor?.inventory?.getQuickBar(qs - 1)?.item) {
items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9
break

View File

@@ -3,6 +3,7 @@ package net.torvald.terrarum.ui
import net.torvald.terrarum.Millisec
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameworld.fmod
import net.torvald.terrarum.itemproperties.ItemCodex
import org.newdawn.slick.Color
import org.newdawn.slick.GameContainer
@@ -28,7 +29,7 @@ class UIQuickBar : UICanvas, MouseControlled {
private var selection: Int
get() = Terrarum.ingame!!.player?.actorValue?.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL) ?: 0
set(value) { Terrarum.ingame!!.player?.actorValue?.set(AVKey.__PLAYER_QUICKSLOTSEL, value) }
set(value) { Terrarum.ingame!!.player?.actorValue?.set(AVKey.__PLAYER_QUICKSLOTSEL, value.fmod(SLOT_COUNT)) }
override fun update(gc: GameContainer, delta: Int) {
@@ -108,8 +109,7 @@ class UIQuickBar : UICanvas, MouseControlled {
}
override fun mouseWheelMoved(change: Int) {
selection = selection.plus(if (change > 1) 1 else if (change < -1) -1 else 0).rem(SLOT_COUNT)
if (selection < 0) selection += SLOT_COUNT
selection = selection.plus(if (change > 1) 1 else if (change < -1) -1 else 0).fmod(SLOT_COUNT)
}
companion object {

View File

@@ -26,14 +26,11 @@ object BlocksDrawer {
private val TILE_SIZEF = FeaturesDrawer.TILE_SIZE.toFloat()
// TODO modular
val tilesTerrain = SpriteSheet(ModMgr.getPath("basegame", "blocks/terrain.tga.gz"), TILE_SIZE, TILE_SIZE)
// Slick has some weird quirks with PNG's transparency. I'm using 32-bit targa here.
// -> PNG transparency issue seems to be fixed (look at my customised ImageDataFactory), but
// tga.gz is smaller than png, so I'd rather keep it
val tilesWire = SpriteSheet(ModMgr.getPath("basegame", "blocks/wire.tga.gz"), TILE_SIZE, TILE_SIZE)
val tilesTerrain = SpriteSheet(ModMgr.getPath("basegame", "blocks/terrain.tga.gz"), TILE_SIZE, TILE_SIZE) // 64 MB
val tilesWire = SpriteSheet(ModMgr.getPath("basegame", "blocks/wire.tga.gz"), TILE_SIZE, TILE_SIZE) // 4 MB
val tileItemWall = Image(TILE_SIZE * 16, TILE_SIZE * GameWorld.TILES_SUPPORTED / 16)
val tileItemWall = Image(TILE_SIZE * 16, TILE_SIZE * GameWorld.TILES_SUPPORTED / 16) // 4 MB
val wallOverlayColour = Color(2f/3f, 2f/3f, 2f/3f, 1f)
@@ -291,9 +288,8 @@ object BlocksDrawer {
return TILES_BLEND_MUL.add(blockID)
}
fun update() {
val player = Terrarum.ingame!!.player
}
fun renderWall(g: Graphics) {
/**