all potentially serialisable actors now have no-arg constructor

This commit is contained in:
minjaesong
2021-08-28 18:58:46 +09:00
parent 043bd3a1db
commit 3a6100107e
18 changed files with 135 additions and 78 deletions

View File

@@ -355,7 +355,7 @@ class TitleScreen(batch: SpriteBatch) : IngameInstance(batch) {
} }
} }
private class CameraPlayer(val demoWorld: GameWorld, override val ai: ActorAI) : ActorWithBody(RenderOrder.FRONT, physProp = PhysProperties.MOBILE_OBJECT), AIControlled { private class CameraPlayer(val demoWorld: GameWorld, override var ai: ActorAI) : ActorWithBody(RenderOrder.FRONT, physProp = PhysProperties.MOBILE_OBJECT), AIControlled {
override val hitbox = Hitbox(0.0, 0.0, 2.0, 2.0) override val hitbox = Hitbox(0.0, 0.0, 2.0, 2.0)

View File

@@ -8,7 +8,7 @@ import net.torvald.terrarum.gameactors.ai.ActorAI
* Created by minjaesong on 2016-01-31. * Created by minjaesong on 2016-01-31.
*/ */
interface AIControlled { interface AIControlled {
val ai: ActorAI var ai: ActorAI
fun moveLeft(amount: Float = 1f) fun moveLeft(amount: Float = 1f)
fun moveRight(amount: Float = 1f) fun moveRight(amount: Float = 1f)

View File

@@ -12,7 +12,7 @@ typealias ActorID = Int
* *
* Created by minjaesong on 2015-12-31. * Created by minjaesong on 2015-12-31.
*/ */
abstract class Actor() : Comparable<Actor>, Runnable { abstract class Actor : Comparable<Actor>, Runnable {
/** /**
* Valid RefID is equal to or greater than 16777216. * Valid RefID is equal to or greater than 16777216.
@@ -21,6 +21,8 @@ abstract class Actor() : Comparable<Actor>, Runnable {
open var referenceID: ActorID = 0 // in old time this was nullable without initialiser. If you're going to revert to that, add the reason why this should be nullable. open var referenceID: ActorID = 0 // in old time this was nullable without initialiser. If you're going to revert to that, add the reason why this should be nullable.
var renderOrder = RenderOrder.MIDDLE var renderOrder = RenderOrder.MIDDLE
protected constructor()
// needs zero-arg constructor for serialiser to work // needs zero-arg constructor for serialiser to work
constructor(renderOrder: RenderOrder, id: ActorID?) : this() { constructor(renderOrder: RenderOrder, id: ActorID?) : this() {
referenceID = id ?: Terrarum.generateUniqueReferenceID(renderOrder) referenceID = id ?: Terrarum.generateUniqueReferenceID(renderOrder)

View File

@@ -36,14 +36,14 @@ import kotlin.math.roundToInt
* *
* Created by minjaesong on 2016-01-13. * Created by minjaesong on 2016-01-13.
*/ */
open class ActorWithBody() : Actor() { open class ActorWithBody : Actor {
var physProp = PhysProperties.HUMANOID_DEFAULT var physProp = PhysProperties.HUMANOID_DEFAULT
constructor(renderOrder: RenderOrder, physProp: PhysProperties, id: ActorID? = null) : this() { protected constructor()
constructor(renderOrder: RenderOrder, physProp: PhysProperties, id: ActorID? = null) : super(renderOrder, id) {
this.physProp = physProp this.physProp = physProp
this.renderOrder = renderOrder
id?.let { this.referenceID = id }
} }
@Transient val COLLISION_TEST_MODE = false @Transient val COLLISION_TEST_MODE = false

View File

@@ -14,7 +14,7 @@ import net.torvald.terrarum.toInt
* *
* Created by minjaesong on 2021-07-30. * Created by minjaesong on 2021-07-30.
*/ */
class WireActor(id: ActorID) : ActorWithBody(RenderOrder.OVERLAY, PhysProperties.IMMOBILE, id) { class WireActor : ActorWithBody {
companion object { companion object {
val WIRE_NEARBY = arrayOf( val WIRE_NEARBY = arrayOf(
@@ -25,6 +25,10 @@ class WireActor(id: ActorID) : ActorWithBody(RenderOrder.OVERLAY, PhysProperties
) )
} }
private constructor()
constructor(id: ActorID) : super(RenderOrder.OVERLAY, PhysProperties.IMMOBILE, id)
init { init {
setHitboxDimension(TILE_SIZE, TILE_SIZE, 0, 0) setHitboxDimension(TILE_SIZE, TILE_SIZE, 0, 0)
} }

View File

@@ -26,9 +26,11 @@ import java.util.*
* *
* Created by minjaesong on 2016-10-24. * Created by minjaesong on 2016-10-24.
*/ */
open class ActorHumanoid() : ActorWithBody(), 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() { protected constructor()
constructor(birth: Long, death: Long? = null, physProp: PhysProperties = PhysProperties.HUMANOID_DEFAULT) : super() {
actorValue[AVKey.__HISTORICAL_BORNTIME] = birth actorValue[AVKey.__HISTORICAL_BORNTIME] = birth
death?.let { actorValue[AVKey.__HISTORICAL_DEADTIME] = death } death?.let { actorValue[AVKey.__HISTORICAL_DEADTIME] = death }
this.physProp = physProp this.physProp = physProp

View File

@@ -17,13 +17,18 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/** /**
* Created by minjaesong on 2016-03-15. * Created by minjaesong on 2016-03-15.
*/ */
open class DroppedItem(private val itemID: ItemID, topLeftX: Int, topLeftY: Int) : ActorWithBody(RenderOrder.MIDTOP, PhysProperties.PHYSICS_OBJECT) { open class DroppedItem : ActorWithBody {
private var itemID: ItemID = ""
private val textureRegion = ItemCodex.getItemImage(itemID) private val textureRegion = ItemCodex.getItemImage(itemID)
var itemCount = 1 var itemCount = 1
init { protected constructor()
constructor(itemID: ItemID, topLeftX: Int, topLeftY: Int) : super(RenderOrder.MIDTOP, PhysProperties.PHYSICS_OBJECT) {
this.itemID = itemID
if (itemID.startsWith("actor@")) if (itemID.startsWith("actor@"))
throw RuntimeException("Attempted to create DroppedItem actor of a real actor; the real actor must be dropped instead.") throw RuntimeException("Attempted to create DroppedItem actor of a real actor; the real actor must be dropped instead.")

View File

@@ -27,22 +27,34 @@ interface Electric {
/** /**
* Created by minjaesong on 2016-06-17. * Created by minjaesong on 2016-06-17.
*/ */
open class FixtureBase( open class FixtureBase : ActorWithBody, CuedByTerrainChange {
blockBox0: BlockBox,
val blockBoxProps: BlockBoxProps = BlockBoxProps(0),
renderOrder: RenderOrder = RenderOrder.MIDDLE,
val nameFun: () -> String,
val mainUI: UICanvas? = null,
val inventory: FixtureInventory? = null,
id: ActorID? = null
// disabling physics (not allowing the fixture to move) WILL make things easier in many ways
) : ActorWithBody(renderOrder, PhysProperties.IMMOBILE, id), CuedByTerrainChange {
var blockBox: BlockBox = blockBox0 lateinit var blockBox: BlockBox // something like TapestryObject will want to redefine this
protected set // something like TapestryObject will want to redefine this
fun blockBoxIndexToPoint2i(it: BlockBoxIndex): Point2i = this.blockBox.width.let { w -> Point2i(it % w, it / w) } fun blockBoxIndexToPoint2i(it: BlockBoxIndex): Point2i = this.blockBox.width.let { w -> Point2i(it % w, it / w) }
var blockBoxProps: BlockBoxProps = BlockBoxProps(0)
var nameFun: () -> String = { "" }
var mainUI: UICanvas? = null
var inventory: FixtureInventory? = null
protected constructor()
constructor(blockBox0: BlockBox,
blockBoxProps: BlockBoxProps = BlockBoxProps(0),
renderOrder: RenderOrder = RenderOrder.MIDDLE,
nameFun: () -> String,
mainUI: UICanvas? = null,
inventory: FixtureInventory? = null,
id: ActorID? = null) : super(renderOrder, PhysProperties.IMMOBILE, id) {
blockBox = blockBox0
this.blockBoxProps = blockBoxProps
this.renderOrder = renderOrder
this.nameFun = nameFun
this.mainUI = mainUI
this.inventory = inventory
if (mainUI != null)
AppLoader.disposableSingletonsPool.add(mainUI)
}
/** /**
* Tile-wise position of this fixture when it's placed on the world, top-left origin. Null if it's not on the world * Tile-wise position of this fixture when it's placed on the world, top-left origin. Null if it's not on the world
@@ -50,11 +62,6 @@ open class FixtureBase(
var worldBlockPos: Point2i? = null var worldBlockPos: Point2i? = null
private set private set
init {
if (mainUI != null)
AppLoader.disposableSingletonsPool.add(mainUI)
}
fun forEachBlockbox(action: (Int, Int) -> Unit) { fun forEachBlockbox(action: (Int, Int) -> Unit) {
worldBlockPos!!.let { (posX, posY) -> worldBlockPos!!.let { (posX, posY) ->
for (y in posY until posY + blockBox.height) { for (y in posY until posY + blockBox.height) {

View File

@@ -7,13 +7,18 @@ import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import org.dyn4j.geometry.Vector2 import org.dyn4j.geometry.Vector2
class FixtureLogicSignalEmitter(nameFun: () -> String) class FixtureLogicSignalEmitter : FixtureBase, Electric {
: FixtureBase(BlockBox(BlockBox.NO_COLLISION, 1, 1), nameFun = nameFun), Electric {
override val wireEmitterTypes: HashMap<String, BlockBoxIndex> = HashMap() override val wireEmitterTypes: HashMap<String, BlockBoxIndex> = HashMap()
override val wireEmission: HashMap<BlockBoxIndex, Vector2> = HashMap() override val wireEmission: HashMap<BlockBoxIndex, Vector2> = HashMap()
override val wireConsumption: HashMap<BlockBoxIndex, Vector2> = HashMap() override val wireConsumption: HashMap<BlockBoxIndex, Vector2> = HashMap()
protected constructor()
constructor(nameFun: () -> String) : super(
BlockBox(BlockBox.NO_COLLISION, 1, 1),
nameFun = nameFun)
init { init {
density = 1400.0 density = 1400.0
setHitboxDimension(TILE_SIZE, TILE_SIZE, 0, -1) setHitboxDimension(TILE_SIZE, TILE_SIZE, 0, -1)

View File

@@ -30,14 +30,17 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/** /**
* Created by minjaesong on 2019-07-08. * Created by minjaesong on 2019-07-08.
*/ */
internal class FixtureStorageChest(nameFun: () -> String) : FixtureBase( internal class FixtureStorageChest : FixtureBase {
BlockBox(BlockBox.ALLOW_MOVE_DOWN, 1, 1),
inventory = FixtureInventory(40, CAPACITY_MODE_COUNT), private constructor()
mainUI = UIStorageChest(),
nameFun = nameFun constructor(nameFun: () -> String) : super(
) { BlockBox(BlockBox.ALLOW_MOVE_DOWN, 1, 1),
mainUI = UIStorageChest(),
inventory = FixtureInventory(40, CAPACITY_MODE_COUNT),
nameFun = nameFun
) {
init {
(mainUI as UIStorageChest).chestInventory = this.inventory!! (mainUI as UIStorageChest).chestInventory = this.inventory!!
(mainUI as UIStorageChest).chestNameFun = this.nameFun (mainUI as UIStorageChest).chestNameFun = this.nameFun

View File

@@ -20,11 +20,11 @@ import kotlin.math.roundToInt
/** /**
* Created by minjaesong on 2016-06-17. * Created by minjaesong on 2016-06-17.
*/ */
internal class FixtureTikiTorch(nameFun: () -> String) : FixtureBase(BlockBox(BlockBox.NO_COLLISION, 1, 2), nameFun = nameFun), Luminous { internal class FixtureTikiTorch : FixtureBase, Luminous {
private val rng = HQRNG() private val rng = HQRNG()
private val rndHash1: Int private val rndHash1 = rng.nextInt()
private val rndHash2: Int private val rndHash2 = rng.nextInt()
override var color: Cvec override var color: Cvec
get() = BlockCodex[Block.TORCH].getLumCol(rndHash1, rndHash2) get() = BlockCodex[Block.TORCH].getLumCol(rndHash1, rndHash2)
@@ -32,9 +32,15 @@ internal class FixtureTikiTorch(nameFun: () -> String) : FixtureBase(BlockBox(Bl
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
override val lightBoxList: ArrayList<Hitbox> override val lightBoxList: ArrayList<Hitbox> = ArrayList(1)
private constructor()
constructor(nameFun: () -> String) : super(
BlockBox(BlockBox.NO_COLLISION, 1, 2),
nameFun = nameFun
) {
init {
// loading textures // loading textures
CommonResourcePool.addToLoadingList("sprites-fixtures-tiki_torch.tga") { CommonResourcePool.addToLoadingList("sprites-fixtures-tiki_torch.tga") {
TextureRegionPack(ModMgr.getGdxFile("basegame", "sprites/fixtures/tiki_torch.tga"), 16, 32) TextureRegionPack(ModMgr.getGdxFile("basegame", "sprites/fixtures/tiki_torch.tga"), 16, 32)
@@ -48,17 +54,12 @@ internal class FixtureTikiTorch(nameFun: () -> String) : FixtureBase(BlockBox(Bl
setHitboxDimension(16, 32, 0, 0) setHitboxDimension(16, 32, 0, 0)
lightBoxList = ArrayList(1)
lightBoxList.add(Hitbox(6.0, 5.0, 4.0, 3.0)) lightBoxList.add(Hitbox(6.0, 5.0, 4.0, 3.0))
makeNewSprite(CommonResourcePool.getAsTextureRegionPack("sprites-fixtures-tiki_torch.tga")) makeNewSprite(CommonResourcePool.getAsTextureRegionPack("sprites-fixtures-tiki_torch.tga"))
sprite!!.setRowsAndFrames(1, 2) sprite!!.setRowsAndFrames(1, 2)
actorValue[AVKey.BASEMASS] = MASS actorValue[AVKey.BASEMASS] = MASS
rndHash1 = rng.nextInt()
rndHash2 = rng.nextInt()
} }
private var nextDelay = 0.25f private var nextDelay = 0.25f

View File

@@ -12,16 +12,20 @@ import net.torvald.terrarum.itemproperties.Material
* *
* Created by minjaesong on 2016-01-31. * Created by minjaesong on 2016-01-31.
*/ */
open class HumanoidNPC( open class HumanoidNPC : ActorHumanoid, AIControlled, CanBeAnItem {
override val ai: ActorAI, // it's there for written-in-Kotlin, "hard-wired" AIs
born: Long override lateinit var ai: ActorAI
//forceAssignRefID: Int? = null
) : ActorHumanoid(born), AIControlled, CanBeAnItem {
companion object { companion object {
val DEFAULT_COLLISION_TYPE = COLLISION_DYNAMIC val DEFAULT_COLLISION_TYPE = COLLISION_DYNAMIC
} }
protected constructor()
constructor(ai: ActorAI, born: Long) : super(born) {
this.ai = ai
}
init { init {
collisionType = DEFAULT_COLLISION_TYPE collisionType = DEFAULT_COLLISION_TYPE
} }

View File

@@ -11,13 +11,14 @@ import net.torvald.terrarum.gameactors.AVKey
* Created by minjaesong on 2015-12-31. * Created by minjaesong on 2015-12-31.
*/ */
class IngamePlayer() : ActorHumanoid(), HasAssembledSprite { class IngamePlayer : ActorHumanoid, HasAssembledSprite {
override var animDescPath = "invalid" override var animDescPath = "invalid"
override var animDescPathGlow: String? = null override var animDescPathGlow: String? = null
private constructor()
constructor(animDescPath: String, animDescPathGlow: String?, born: Long) : this() { constructor(animDescPath: String, animDescPathGlow: String?, born: Long) : super(born) {
this.animDescPath = animDescPath this.animDescPath = animDescPath
this.animDescPathGlow = animDescPathGlow this.animDescPathGlow = animDescPathGlow
actorValue[AVKey.__HISTORICAL_BORNTIME] = born actorValue[AVKey.__HISTORICAL_BORNTIME] = born

View File

@@ -6,12 +6,13 @@ import org.dyn4j.geometry.Vector2
/** /**
* Created by minjaesong on 2016-08-29. * Created by minjaesong on 2016-08-29.
*/ */
class ProjectileHoming( class ProjectileHoming : ProjectileSimple {
type: Int,
fromPoint: Vector2, // projected coord
toPoint: Vector2 // arriving coord
) : ProjectileSimple(type, fromPoint, toPoint) {
protected constructor()
constructor(type: Int,
fromPoint: Vector2, // projected coord
toPoint: Vector2 // arriving coord
) : super(type, fromPoint, toPoint)
} }

View File

@@ -21,16 +21,13 @@ import java.util.*
*/ */
// TODO simplified, lightweight physics (does not call PhysicsSolver) // TODO simplified, lightweight physics (does not call PhysicsSolver)
open class ProjectileSimple( open class ProjectileSimple : ActorWithBody, Luminous, Projectile {
private val type: Int,
fromPoint: Vector2, // projected coord
toPoint: Vector2 // arriving coord
) : ActorWithBody(RenderOrder.MIDTOP, PhysProperties.PHYSICS_OBJECT), Luminous, Projectile {
val damage: Int private var type: Int = 0
val displayColour: Color var damage: Int = 0
lateinit var displayColour: Color
/** scalar part of velocity */ /** scalar part of velocity */
val speed: Int var speed: Int = 0
override var color: Cvec override var color: Cvec
@@ -48,9 +45,17 @@ open class ProjectileSimple(
private val lifetimeMax = 2500 private val lifetimeMax = 2500
private var lifetimeCounter = 0f private var lifetimeCounter = 0f
private val posPre: Point2d private lateinit var posPre: Point2d
protected constructor()
constructor(type: Int,
fromPoint: Vector2, // projected coord
toPoint: Vector2 // arriving coord
) : super(RenderOrder.MIDTOP, PhysProperties.PHYSICS_OBJECT) {
this.type = type
init {
setPosition(fromPoint.x, fromPoint.y) setPosition(fromPoint.x, fromPoint.y)
posPre = Point2d(fromPoint.x, fromPoint.y) posPre = Point2d(fromPoint.x, fromPoint.y)
// lightbox sized 8x8 centered to the bullet // lightbox sized 8x8 centered to the bullet

View File

@@ -12,13 +12,20 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/** /**
* Created by minjaesong on 2017-01-07. * Created by minjaesong on 2017-01-07.
*/ */
class TapestryObject(pixmap: Pixmap, val artName: String, val artAuthor: String, nameFun: () -> String) : class TapestryObject : FixtureBase {
FixtureBase(BlockBox(BlockBox.NO_COLLISION, 1, 1), renderOrder = RenderOrder.BEHIND, nameFun = nameFun) // placeholder blockbox
{
// physics = false only speeds up for ~2 frames with 50 tapestries // physics = false only speeds up for ~2 frames with 50 tapestries
init { var artName = ""; private set
var artAuthor = ""; private set
private constructor()
constructor(pixmap: Pixmap, artName: String, artAuthor: String, nameFun: () -> String) : super() {
this.artName = artName
this.artAuthor = artAuthor
this.nameFun = nameFun
val texture = Texture(pixmap) val texture = Texture(pixmap)
pixmap.dispose() pixmap.dispose()
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
@@ -30,7 +37,8 @@ class TapestryObject(pixmap: Pixmap, val artName: String, val artAuthor: String,
// you CAN'T destroy the image // you CAN'T destroy the image
// redefine blockbox // redefine blockbox
blockBox = BlockBox(BlockBox.NO_COLLISION, texture.width.div(TILE_SIZEF).ceilInt(), texture.height.div(TILE_SIZEF).ceilInt()) this.blockBox = BlockBox(BlockBox.NO_COLLISION, texture.width.div(TILE_SIZEF).ceilInt(), texture.height.div(TILE_SIZEF).ceilInt())
this.renderOrder = RenderOrder.BEHIND
} }
override fun update(delta: Float) { override fun update(delta: Float) {

View File

@@ -5,11 +5,22 @@ import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gameactors.Hitbox import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameactors.Luminous import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.gameactors.PhysProperties import net.torvald.terrarum.gameactors.PhysProperties
import net.torvald.terrarum.gameitem.ItemID
/** /**
* Created by minjaesong on 2016-04-26. * Created by minjaesong on 2016-04-26.
*/ */
class WeaponSwung(val itemID: Int) : ActorWithBody(RenderOrder.MIDTOP, PhysProperties.IMMOBILE), Luminous { class WeaponSwung : ActorWithBody, Luminous {
var itemID: ItemID = ""; private set
private constructor()
constructor(itemID: ItemID) : super(RenderOrder.MIDTOP, PhysProperties.IMMOBILE) {
this.itemID = itemID
}
// just let the solver use AABB; it's cheap but works just enough // just let the solver use AABB; it's cheap but works just enough
/** /**

View File

@@ -20,8 +20,6 @@ class ReadActor(val ingame: TerrarumIngame) {
} }
open fun invoke(worldDataStream: Reader) { open fun invoke(worldDataStream: Reader) {
IngamePlayer()
postRead(Common.jsoner.fromJson(IngamePlayer::class.java, worldDataStream)) postRead(Common.jsoner.fromJson(IngamePlayer::class.java, worldDataStream))
} }