fixing the critical bug in active/dormant thingies, actor ID is now positive integer (I had to), class Actor is now shipped with ID generator, optimisation in add/removeActor

Former-commit-id: f743ecb27ba1cea05215889d7e1a77e10171cb8c
Former-commit-id: 4b88f9711c34542a8a504682cffe79a2f8a43ed8
This commit is contained in:
Song Minjae
2016-04-25 01:58:17 +09:00
parent c4b64140be
commit 1dc3e6df3e
10 changed files with 145 additions and 98 deletions

View File

@@ -1,5 +1,7 @@
package net.torvald.terrarum.gameactors
import net.torvald.random.HQRNG
import net.torvald.terrarum.Terrarum
import org.newdawn.slick.GameContainer
/**
@@ -19,6 +21,23 @@ abstract class Actor : Comparable<Actor> {
override fun equals(other: Any?) = referenceID == (other as Actor).referenceID
override fun hashCode() = referenceID
override fun toString() = "ID: ${hashCode()}"
override fun toString() = if (actorValue.getAsString(AVKey.NAME).isNullOrEmpty())
"ID: ${hashCode()}"
else
"ID: ${hashCode()} (${actorValue.getAsString(AVKey.NAME)})"
override fun compareTo(other: Actor): Int = this.referenceID - other.referenceID
/**
* Usage:
*
* override var referenceID: Int = generateUniqueReferenceID()
*/
fun generateUniqueReferenceID(): Int {
fun Int.abs() = if (this < 0) -this else this
var ret: Int
do {
ret = HQRNG().nextInt().abs() // set new ID
} while (Terrarum.game.hasActor(ret)) // check for collision
return ret
}
}

View File

@@ -51,7 +51,7 @@ open class ActorWithBody constructor() : Actor(), Visible {
internal var baseSpriteWidth: Int = 0
internal var baseSpriteHeight: Int = 0
override var referenceID: Int = 0
override var referenceID: Int = generateUniqueReferenceID()
/**
* Positions: top-left point
*/
@@ -137,10 +137,6 @@ open class ActorWithBody constructor() : Actor(), Visible {
private var posAdjustY = 0
init {
do {
referenceID = HQRNG().nextInt() // set new ID
} while (Terrarum.game.hasActor(referenceID)) // check for collision
map = Terrarum.game.map
}
@@ -719,19 +715,13 @@ open class ActorWithBody constructor() : Actor(), Visible {
fun Float.round() = Math.round(this).toFloat()
fun Float.roundToInt(): Int = Math.round(this)
fun Float.abs() = FastMath.abs(this)
fun Int.abs() = if (this < 0) -this else this
companion object {
@Transient private val TSIZE = MapDrawer.TILE_SIZE
private var AUTO_CLIMB_RATE = TSIZE / 8
private fun div16(x: Int): Int {
if (x < 0) {
throw IllegalArgumentException("div16: Positive integer only: " + x.toString())
}
return x and 0x7FFFFFFF shr 4
}
private fun div16TruncateToMapWidth(x: Int): Int {
if (x < 0)
return 0
@@ -750,13 +740,6 @@ open class ActorWithBody constructor() : Actor(), Visible {
return y and 0x7FFFFFFF shr 4
}
private fun mod16(x: Int): Int {
if (x < 0) {
throw IllegalArgumentException("mod16: Positive integer only: " + x.toString())
}
return x and 15
}
private fun clampCeil(x: Float, ceil: Float): Float {
return if (Math.abs(x) > ceil) ceil else x
}