fix some pathfinding problems
This commit is contained in:
+1
-1
@@ -105,7 +105,7 @@ export default class Entity {
|
|||||||
const unadjustedGraph = (this.game?.waypointGraph ?? []).concat(extraGraphEntries)
|
const unadjustedGraph = (this.game?.waypointGraph ?? []).concat(extraGraphEntries)
|
||||||
console.timeEnd('unadjustedGraph')
|
console.timeEnd('unadjustedGraph')
|
||||||
console.time('path')
|
console.time('path')
|
||||||
this.#path = Pathfind.shortestPath(unadjustedGraph, this.position, fixedDest).map(([unadjusted, direction]) => unadjusted.clone().add(direction.clone().multiplyScalar(this.radius)))
|
this.#path = Pathfind.shortestPath(unadjustedGraph, this.position, fixedDest, this.radius, this.collidables()).map(([unadjusted, direction]) => unadjusted.clone().add(direction.clone().multiplyScalar(this.radius)))
|
||||||
console.log(this.#path)
|
console.log(this.#path)
|
||||||
console.timeEnd('path')
|
console.timeEnd('path')
|
||||||
console.timeEnd('pathfinding')
|
console.timeEnd('pathfinding')
|
||||||
|
|||||||
+14
-4
@@ -7,7 +7,7 @@ export default class Pathfind {
|
|||||||
return `${pos.x},${pos.y}`
|
return `${pos.x},${pos.y}`
|
||||||
}
|
}
|
||||||
|
|
||||||
static shortestPath(graph, start, goal) {
|
static shortestPath(graph, start, goal, radius = 0, colliders = []) {
|
||||||
const queue = new PriorityQueue((a, b) => a[1] < b[1])
|
const queue = new PriorityQueue((a, b) => a[1] < b[1])
|
||||||
const visited = new Map()
|
const visited = new Map()
|
||||||
|
|
||||||
@@ -26,7 +26,17 @@ export default class Pathfind {
|
|||||||
if (!visited.has(key) || visited.get(key) > cost) {
|
if (!visited.has(key) || visited.get(key) > cost) {
|
||||||
visited.set(key, cost)
|
visited.set(key, cost)
|
||||||
|
|
||||||
for (const { to, direction, distance } of graph.filter(e => e.from.equals(waypoint))) {
|
for (const { from, to, direction, reverseDirection, distance } of graph.filter(e => e.from.equals(waypoint))) {
|
||||||
|
if (radius > 0 && colliders.length > 0) {
|
||||||
|
const adjustedFrom = from.clone().add(reverseDirection.clone().multiplyScalar(radius))
|
||||||
|
const adjustedTo = to.clone().add(direction.clone().multiplyScalar(radius))
|
||||||
|
const colliding = SATX.collideObjects(SATX.entityTunnel(adjustedFrom, adjustedTo, radius), colliders)
|
||||||
|
|
||||||
|
if (colliding) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const keyTo = this.keyFor(to)
|
const keyTo = this.keyFor(to)
|
||||||
if (!visited.has(keyTo) || visited.get(keyTo) > cost + distance) {
|
if (!visited.has(keyTo) || visited.get(keyTo) > cost + distance) {
|
||||||
queue.push([[...path, [to, direction]], cost + distance])
|
queue.push([[...path, [to, direction]], cost + distance])
|
||||||
@@ -57,8 +67,8 @@ export default class Pathfind {
|
|||||||
|
|
||||||
if (lineOfSight) {
|
if (lineOfSight) {
|
||||||
const distance = from.distanceTo(to)
|
const distance = from.distanceTo(to)
|
||||||
graph.push({ from, to, distance, direction })
|
graph.push({ from, to, distance, direction, reverseDirection })
|
||||||
graph.push({ to: from, from: to, distance, direction: reverseDirection })
|
graph.push({ to: from, from: to, distance, direction: reverseDirection, reverseDirection: direction })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user