mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 20:31:51 +09:00
changed lightboxes so that (de)serialiser won't complain; world/actor json will will write game version it saved
This commit is contained in:
@@ -2,7 +2,18 @@ package net.torvald.terrarum.gameactors
|
||||
|
||||
import net.torvald.gdx.graphics.Cvec
|
||||
|
||||
data class Lightbox(val hitbox: Hitbox, val getLight: () -> Cvec)
|
||||
class Lightbox() {
|
||||
var hitbox: Hitbox = Hitbox(0.0,0.0,0.0,0.0)
|
||||
var light: Cvec = Cvec()
|
||||
|
||||
constructor(hitbox: Hitbox, light: Cvec) : this() {
|
||||
this.hitbox = hitbox
|
||||
this.light = light
|
||||
}
|
||||
|
||||
operator fun component1() = hitbox
|
||||
operator fun component2() = light
|
||||
}
|
||||
|
||||
/**
|
||||
* For actors that either emits or blocks lights
|
||||
@@ -16,6 +27,8 @@ interface Luminous {
|
||||
*
|
||||
* Hitbox(x-offset, y-offset, width, height)
|
||||
* (Use ArrayList for normal circumstances)
|
||||
*
|
||||
* NOTE: MUST NOT SERIALISE (use `@Transient`)
|
||||
*/
|
||||
val lightBoxList: List<Lightbox>
|
||||
|
||||
@@ -24,6 +37,8 @@ interface Luminous {
|
||||
*
|
||||
* Hitbox(x-offset, y-offset, width, height)
|
||||
* (Use ArrayList for normal circumstances)
|
||||
*
|
||||
* NOTE: MUST NOT SERIALISE (use `@Transient`)
|
||||
*/
|
||||
val shadeBoxList: List<Lightbox>
|
||||
}
|
||||
@@ -127,7 +127,7 @@ open class GameWorld() : Disposable {
|
||||
|
||||
val extraFields = HashMap<String, Any?>()
|
||||
|
||||
internal var genver = -1 // only gets used when the game saves and loads
|
||||
// internal var genver = -1 // only gets used when the game saves and loads
|
||||
internal var comp = -1 // only gets used when the game saves and loads
|
||||
|
||||
internal val dynamicItemInventory = ItemTable()
|
||||
|
||||
@@ -99,11 +99,11 @@ open class ActorHumanoid : ActorWithBody, Controllable, Pocketed, Factionable, L
|
||||
* (Use ArrayList for normal circumstances)
|
||||
*/
|
||||
override val lightBoxList: List<Lightbox>
|
||||
get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3)) { actorValueColour }).toList() // things are asymmetric!!
|
||||
get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3), actorValueColour)).toList() // things are asymmetric!!
|
||||
// use getter; dimension of the player may change by time.
|
||||
|
||||
override val shadeBoxList: List<Lightbox>
|
||||
get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3)) { actorValueShade }).toList() // things are asymmetric!!
|
||||
get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3), actorValueShade)).toList() // things are asymmetric!!
|
||||
// use getter; dimension of the player may change by time.
|
||||
|
||||
@Transient val BASE_DENSITY = 980.0
|
||||
|
||||
@@ -27,13 +27,13 @@ internal class FixtureTikiTorch : FixtureBase, Luminous {
|
||||
private val rndHash2 = rng.nextInt()
|
||||
|
||||
private var color: Cvec
|
||||
get() = BlockCodex[Block.TORCH].getLumCol(rndHash1, rndHash2)
|
||||
get() = try { BlockCodex[Block.TORCH].getLumCol(rndHash1, rndHash2) } catch (e: NullPointerException) { Cvec() }
|
||||
set(value) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override val lightBoxList: ArrayList<Lightbox> = ArrayList(1)
|
||||
override val shadeBoxList: ArrayList<Lightbox> = ArrayList(1)
|
||||
@Transient override val lightBoxList: ArrayList<Lightbox> = ArrayList(1)
|
||||
@Transient override val shadeBoxList: ArrayList<Lightbox> = ArrayList(1)
|
||||
|
||||
constructor() : super(
|
||||
BlockBox(BlockBox.NO_COLLISION, 1, 2),
|
||||
@@ -53,7 +53,7 @@ internal class FixtureTikiTorch : FixtureBase, Luminous {
|
||||
|
||||
setHitboxDimension(16, 32, 0, 0)
|
||||
|
||||
lightBoxList.add(Lightbox(Hitbox(6.0, 5.0, 4.0, 3.0)) { color })
|
||||
lightBoxList.add(Lightbox(Hitbox(6.0, 5.0, 4.0, 3.0), color))
|
||||
|
||||
makeNewSprite(CommonResourcePool.getAsTextureRegionPack("sprites-fixtures-tiki_torch.tga"))
|
||||
sprite!!.setRowsAndFrames(1, 2)
|
||||
|
||||
@@ -7,7 +7,7 @@ import net.torvald.terrarum.gameitems.ItemID
|
||||
/**
|
||||
* Created by minjaesong on 2016-04-26.
|
||||
*/
|
||||
class WeaponSwung : ActorWithBody, Luminous {
|
||||
class ItemCarrying : ActorWithBody, Luminous {
|
||||
|
||||
var itemID: ItemID = ""; private set
|
||||
|
||||
@@ -38,8 +38,8 @@ open class ProjectileSimple : ActorWithBody, Luminous, Projectile {
|
||||
* Hitbox(x-offset, y-offset, width, height)
|
||||
* (Use ArrayList for normal circumstances)
|
||||
*/
|
||||
override val lightBoxList = ArrayList<Lightbox>()
|
||||
override val shadeBoxList = ArrayList<Lightbox>()
|
||||
@Transient override val lightBoxList = ArrayList<Lightbox>()
|
||||
@Transient override val shadeBoxList = ArrayList<Lightbox>()
|
||||
|
||||
private val lifetimeMax = 2500
|
||||
private var lifetimeCounter = 0f
|
||||
@@ -58,7 +58,7 @@ open class ProjectileSimple : ActorWithBody, Luminous, Projectile {
|
||||
setPosition(fromPoint.x, fromPoint.y)
|
||||
posPre = Point2d(fromPoint.x, fromPoint.y)
|
||||
// lightbox sized 8x8 centered to the bullet
|
||||
lightBoxList.add(Lightbox(Hitbox(-4.0, -4.0, 8.0, 8.0)) { color })
|
||||
lightBoxList.add(Lightbox(Hitbox(-4.0, -4.0, 8.0, 8.0), color))
|
||||
//this.externalV.set(velocity)
|
||||
|
||||
damage = bulletDatabase[type][OFFSET_DAMAGE] as Int
|
||||
|
||||
@@ -46,7 +46,7 @@ object Common {
|
||||
|
||||
// install custom (de)serialiser
|
||||
init {
|
||||
// jsoner.ignoreUnknownFields = true
|
||||
jsoner.ignoreUnknownFields = true
|
||||
jsoner.setUsePrototypes(false)
|
||||
jsoner.setIgnoreDeprecated(false)
|
||||
|
||||
|
||||
@@ -21,16 +21,17 @@ import java.util.*
|
||||
*/
|
||||
object WriteActor {
|
||||
|
||||
// genver must be found on fixed location of the JSON string
|
||||
operator fun invoke(actor: Actor): String {
|
||||
val s = Common.jsoner.toJson(actor, actor.javaClass)
|
||||
return """{"class":"${actor.javaClass.canonicalName}",${s.substring(1)}"""
|
||||
return """{"genver":${Common.GENVER},"class":"${actor.javaClass.canonicalName}",${s.substring(1)}"""
|
||||
}
|
||||
|
||||
fun encodeToByteArray64(actor: Actor): ByteArray64 {
|
||||
val baw = ByteArray64Writer(Common.CHARSET)
|
||||
|
||||
val classDef = """{"class":"${actor.javaClass.canonicalName}""""
|
||||
baw.write(classDef)
|
||||
val header = """{"genver":${Common.GENVER},"class":"${actor.javaClass.canonicalName}""""
|
||||
baw.write(header)
|
||||
Common.jsoner.toJson(actor, actor.javaClass, baw)
|
||||
baw.flush(); baw.close()
|
||||
// by this moment, contents of the baw will be:
|
||||
@@ -39,7 +40,7 @@ object WriteActor {
|
||||
// and we want to turn it into this:
|
||||
// {"class":"some.class.Name","actorValue":{},......}
|
||||
val ba = baw.toByteArray64()
|
||||
ba[classDef.toByteArray(Common.CHARSET).size.toLong()] = ','.code.toByte()
|
||||
ba[header.toByteArray(Common.CHARSET).size.toLong()] = ','.code.toByte()
|
||||
|
||||
return ba
|
||||
}
|
||||
@@ -90,6 +91,7 @@ object WritePlayer {
|
||||
|
||||
|
||||
val actorJson = WriteActor.encodeToByteArray64(player)
|
||||
|
||||
val adl = player.animDesc!!.getRawADL()
|
||||
val adlGlow = player.animDescGlow?.getRawADL() // NULLABLE!
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ object WriteWorld {
|
||||
val world = ingame.world
|
||||
val currentPlayTime_t = time_t - ingame.loadedTime_t
|
||||
|
||||
world.genver = Common.GENVER
|
||||
// world.genver = Common.GENVER
|
||||
world.comp = Common.COMP_GZIP
|
||||
world.lastPlayTime = time_t
|
||||
world.totalPlayTime += currentPlayTime_t
|
||||
@@ -56,17 +56,28 @@ object WriteWorld {
|
||||
return world
|
||||
}
|
||||
|
||||
// genver must be found on fixed location of the JSON string
|
||||
operator fun invoke(ingame: TerrarumIngame, time_t: Long, actorsList: List<Actor>, playersList: List<IngamePlayer>): String {
|
||||
return Common.jsoner.toJson(preWrite(ingame, time_t, actorsList, playersList))
|
||||
val s = Common.jsoner.toJson(preWrite(ingame, time_t, actorsList, playersList))
|
||||
return """{"genver":${Common.GENVER},${s.substring(1)}"""
|
||||
}
|
||||
|
||||
fun encodeToByteArray64(ingame: TerrarumIngame, time_t: Long, actorsList: List<Actor>, playersList: List<IngamePlayer>): ByteArray64 {
|
||||
val baw = ByteArray64Writer(Common.CHARSET)
|
||||
|
||||
val header = """{"genver":${Common.GENVER}"""
|
||||
baw.write(header)
|
||||
Common.jsoner.toJson(preWrite(ingame, time_t, actorsList, playersList), baw)
|
||||
baw.flush(); baw.close()
|
||||
// by this moment, contents of the baw will be:
|
||||
// {"genver":123456{"actorValue":{},......}
|
||||
// (note that first bracket is not closed, and another open bracket after "genver" property)
|
||||
// and we want to turn it into this:
|
||||
// {"genver":123456,"actorValue":{},......}
|
||||
val ba = baw.toByteArray64()
|
||||
ba[header.toByteArray(Common.CHARSET).size.toLong()] = ','.code.toByte()
|
||||
|
||||
return baw.toByteArray64()
|
||||
return ba
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -358,7 +358,7 @@ object LightmapRenderer {
|
||||
..lightBoxX.plus(lightBoxW).div(TILE_SIZE).floorInt()) {
|
||||
|
||||
val oldLight = lanternMap[LandUtil.getBlockAddr(world, x, y)] ?: Cvec(0) // if two or more luminous actors share the same block, mix the light
|
||||
val actorLight = colour()
|
||||
val actorLight = colour
|
||||
|
||||
lanternMap[LandUtil.getBlockAddr(world, x, y)] = oldLight.maxAndAssign(actorLight)
|
||||
}
|
||||
@@ -377,7 +377,7 @@ object LightmapRenderer {
|
||||
..lightBoxX.plus(lightBoxW).div(TILE_SIZE).floorInt()) {
|
||||
|
||||
val oldLight = shadowMap[LandUtil.getBlockAddr(world, x, y)] ?: Cvec(0) // if two or more luminous actors share the same block, mix the light
|
||||
val actorLight = colour()
|
||||
val actorLight = colour
|
||||
|
||||
shadowMap[LandUtil.getBlockAddr(world, x, y)] = oldLight.maxAndAssign(actorLight)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user