mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-15 08:06:06 +09:00
no secondary click; tiki torch kinda spawns?
This commit is contained in:
@@ -32,7 +32,17 @@ class KDHeapifiedTree(actors: List<ActorWBMovable>) {
|
||||
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?) { nodes[this] = value }
|
||||
private fun Int.set(value: ActorWBMovable?) {
|
||||
try {
|
||||
nodes[this] = value
|
||||
}
|
||||
catch (_: ArrayIndexOutOfBoundsException) {
|
||||
// modification of the private fun expandArray()
|
||||
val prevNodes = nodes.copyOf() + value
|
||||
Array<ActorWBMovable?>(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 }
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package net.torvald.aa
|
||||
|
||||
import net.torvald.terrarum.gameactors.ActorWithBody
|
||||
import net.torvald.terrarum.gameactors.Hitbox
|
||||
import net.torvald.terrarum.sqr
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2019-04-18.
|
||||
*/
|
||||
/*class KDTree(points: List<ActorWithBody>) {
|
||||
class KDTree(points: List<ActorWithBody>) {
|
||||
|
||||
companion object {
|
||||
const val DIMENSION = 2
|
||||
@@ -15,7 +19,7 @@ package net.torvald.aa
|
||||
root = create(points, 0)
|
||||
}
|
||||
|
||||
fun findNearest(query: ActorWithBody) = getNearest(root!!, query, 0)
|
||||
fun findNearest(query: ActorWithBody) = getNearest(root!!, query.hitbox, 0)
|
||||
|
||||
private fun create(points: List<ActorWithBody>, depth: Int): KDNode? {
|
||||
if (points.isEmpty()) {
|
||||
@@ -34,21 +38,21 @@ package net.torvald.aa
|
||||
}
|
||||
}
|
||||
|
||||
private fun getNearest(currentNode: KDNode, query: ActorWithBody, depth: Int = 0): KDNode {
|
||||
val direction = currentNode.compare(query, depth % DIMENSION)
|
||||
private fun getNearest(currentNode: KDNode, actorHitbox: Hitbox, depth: Int = 0): KDNode {
|
||||
val direction = currentNode.compare(actorHitbox, depth % DIMENSION)
|
||||
|
||||
val next = if (direction < 0) currentNode.left else currentNode.right
|
||||
val other = if (direction < 0) currentNode.right else currentNode.left
|
||||
var best = if (next == null) currentNode else getNearest(next, query, depth + 1) // traverse to leaf
|
||||
var best = if (next == null) currentNode else getNearest(next, actorHitbox, depth + 1) // traverse to leaf
|
||||
|
||||
if (currentNode.position.distSqr(query) < best.position.distSqr(query)) {
|
||||
if (currentNode.position.distSqr(actorHitbox) < best.position.distSqr(actorHitbox)) {
|
||||
best = currentNode
|
||||
}
|
||||
|
||||
if (other != null) {
|
||||
if (currentNode.position.dimDistSqr(query, depth % DIMENSION) < best.position.distSqr(query)) {
|
||||
val bestCandidate = getNearest(other, query, depth + 1)
|
||||
if (bestCandidate.position.distSqr(query) < best.position.distSqr(query)) {
|
||||
if (currentNode.position.dimDistSqr(actorHitbox, depth % DIMENSION) < best.position.distSqr(actorHitbox)) {
|
||||
val bestCandidate = getNearest(other, actorHitbox, depth + 1)
|
||||
if (bestCandidate.position.distSqr(actorHitbox) < best.position.distSqr(actorHitbox)) {
|
||||
best = bestCandidate
|
||||
}
|
||||
}
|
||||
@@ -58,14 +62,15 @@ package net.torvald.aa
|
||||
}
|
||||
|
||||
data class KDNode(val left: KDNode?, val right: KDNode?, val actor: ActorWithBody, val position: Hitbox) {
|
||||
fun compare(other: ActorWithBody, dimension: Int) = other.getDimensionalPoint(dimension) - this.position.getDimensionalPoint(dimension)
|
||||
|
||||
//fun compare(other: ActorWithBody, dimension: Int) = other.getDimensionalPoint(dimension) - this.position.getDimensionalPoint(dimension)
|
||||
fun compare(other: Hitbox, dimension: Int) = other.getDimensionalPoint(dimension) - this.position.getDimensionalPoint(dimension)
|
||||
}
|
||||
|
||||
private fun Hitbox.distSqr(other: Hitbox) {
|
||||
private fun Hitbox.distSqr(other: Hitbox): Double {
|
||||
var dist = 0.0
|
||||
for (i in 0 until DIMENSION)
|
||||
dist += (this.getDimensionalPoint(i) - other.getDimensionalPoint(i)).sqr()
|
||||
return dist
|
||||
}
|
||||
|
||||
private fun Hitbox.dimDistSqr(other: Hitbox, dimension: Int) = other.getDimensionalPoint(dimension).minus(this.getDimensionalPoint(dimension)).sqr()
|
||||
@@ -74,4 +79,4 @@ package net.torvald.aa
|
||||
internal fun ActorWithBody.getDimensionalPoint(depth: Int) = this.hitbox.getDimensionalPoint(depth)
|
||||
// TODO take ROUNDWORLD into account
|
||||
internal fun Hitbox.getDimensionalPoint(depth: Int) =
|
||||
if (depth % KDTree.DIMENSION == 0) this.canonicalX else this.canonicalY*/
|
||||
if (depth % KDTree.DIMENSION == 0) this.canonicalX else this.canonicalY
|
||||
Reference in New Issue
Block a user