Reducing hierarchy by merging ActorWithBody with ActorWBMovable

This commit is contained in:
minjaesong
2020-03-20 19:30:17 +09:00
parent 5cafe16f20
commit 07b9e76090
26 changed files with 1844 additions and 1864 deletions

View File

@@ -1,7 +1,7 @@
package net.torvald.aa
import net.torvald.terrarum.Point2d
import net.torvald.terrarum.gameactors.ActorWBMovable
import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.sqr
/**
@@ -17,34 +17,34 @@ import net.torvald.terrarum.sqr
* Remarks:
* - NOT using the fullCodePage with 2x2 mode makes it slower... skewed tree generation?
*/
class KDHeapifiedTree(actors: List<ActorWBMovable>) {
class KDHeapifiedTree(actors: List<ActorWithBody>) {
private val dimension = 2
private val initialSize = 128
private val nodes = Array<ActorWBMovable?>(initialSize, { null })
private val nodes = Array<ActorWithBody?>(initialSize, { null })
private val root: Int = 0
fun findNearestActor(query: Point2d): ActorWBMovable =
fun findNearestActor(query: Point2d): ActorWithBody =
getNearest(root, query, 0).getActor()!!
private fun Int.get() = nodes[this]?.feetPosPoint
private fun Int.getActor() = nodes[this]
private fun Int.getLeft() = this * 2 + 1
private fun Int.getRight() = this * 2 + 2
private fun Int.set(value: ActorWBMovable?) {
private fun Int.set(value: ActorWithBody?) {
try {
nodes[this] = value
}
catch (_: ArrayIndexOutOfBoundsException) {
// modification of the private fun expandArray()
val prevNodes = nodes.copyOf() + value
Array<ActorWBMovable?>(prevNodes.size * 2, { null })
Array<ActorWithBody?>(prevNodes.size * 2, { null })
create(prevNodes.toList(), 0, 0)
}
}
private fun Int.setLeftChild(value: ActorWBMovable?) { nodes[this.getLeft()] = value }
private fun Int.setRightChild(value: ActorWBMovable?) { nodes[this.getRight()] = value }
private fun Int.setLeftChild(value: ActorWithBody?) { nodes[this.getLeft()] = value }
private fun Int.setRightChild(value: ActorWithBody?) { nodes[this.getRight()] = value }
private val zeroPoint = Point2d(0.0, 0.0)
@@ -52,7 +52,7 @@ class KDHeapifiedTree(actors: List<ActorWBMovable>) {
create(actors, 0, 0)
}
private fun create(points: List<ActorWBMovable?>, depth: Int, index: Int): ActorWBMovable? {
private fun create(points: List<ActorWithBody?>, depth: Int, index: Int): ActorWithBody? {
if (points.isEmpty()) {
index.set(null)
@@ -103,7 +103,7 @@ class KDHeapifiedTree(actors: List<ActorWBMovable>) {
private fun expandArray() {
val prevNodes = nodes.copyOf()
Array<ActorWBMovable?>(prevNodes.size * 2, { null })
Array<ActorWithBody?>(prevNodes.size * 2, { null })
create(prevNodes.toList(), 0, 0)
}