Find nearest pathable tile

main
idylls 1 year ago
parent e70dce298b
commit 5e2e229177
Signed by: idylls
GPG Key ID: 52D7502B0C319049

@ -78,9 +78,36 @@ fun findPath(
}
}
// TODO: Add out-of-bounds pathfinding
// If there is not a path to the requested tile, try to find the
// nearest pathable tile.
val dist = { sp: ScenePoint ->
val dx = sp.getX() - to.getX()
val dy = sp.getY() - to.getY()
return null
(dx * dx) + (dy * dy)
}
val candidates = ArrayList(backEdges.keys)
candidates.add(from)
val candidateDistances = candidates.map({ sp ->
Pair(sp, dist(sp))
})
val minDistance = candidateDistances.minByOrNull({ c -> c.second })
if (minDistance == null) {
return null
}
val minimalCandidates = candidateDistances.filter({ c ->
c.second == minDistance.second
})
val minPath = minimalCandidates.mapNotNull({ c ->
buildPath(backEdges, c.first)
}).minWithOrNull({ a, b -> a.size - b.size })
return minPath
}
fun sceneX(t: RLTile) = t.sceneLocation.x

Loading…
Cancel
Save