fix pathfinding nekimegyafalnak style
This commit is contained in:
+28
-9
@@ -2,6 +2,7 @@ import { Vector2 } from 'three'
|
||||
import SAT from 'sat'
|
||||
import SATX from './satx.js'
|
||||
import Pathfind from './pathfind.js'
|
||||
import Terrain from './terrain.js'
|
||||
|
||||
export default class Entity {
|
||||
id = crypto.randomUUID()
|
||||
@@ -94,15 +95,29 @@ export default class Entity {
|
||||
|
||||
if (this.#path.length < 1 || !this.#path.at(-1).equals(fixedDest)) {
|
||||
console.time('pathfinding')
|
||||
// console.time('waypoints')
|
||||
const waypoints = this.waypoints().concat([this.position, fixedDest])
|
||||
// console.timeEnd('waypoints')
|
||||
// console.time('graph')
|
||||
console.time('waypoints')
|
||||
const start = SATX.vectorToFloat32Array(this.position)
|
||||
const goal = SATX.vectorToFloat32Array(fixedDest)
|
||||
const nonUniqueWaypoints = this.waypoints().map((w) => SATX.vectorToFloat32Array(w)).concat([start, goal])
|
||||
const waypoints = Pathfind.uniqueWaypoints(nonUniqueWaypoints)
|
||||
console.timeEnd('waypoints')
|
||||
console.time('graph')
|
||||
const graph = Pathfind.buildGraph(waypoints, collidables, this.radius)
|
||||
// console.timeEnd('graph')
|
||||
// console.time('path')
|
||||
this.#path = Pathfind.shortestPath(graph, this.position, fixedDest)
|
||||
// console.timeEnd('path')
|
||||
|
||||
// console.log(Pathfind.formatFloat32Array(graph, 5, true))
|
||||
// const tunnels = []
|
||||
// for (let i = 0; i < graph.length; i += 5) {
|
||||
// tunnels.push(SATX.entityTunnel(graph[i], graph[i + 1], graph[i + 2], graph[i + 3], 1))
|
||||
// }
|
||||
|
||||
// tunnels.map((t) => SATX.satPolygonToVectors(t)).forEach((t) => this.#game.add_terrain(new Terrain(t)))
|
||||
// this.#dest = null
|
||||
|
||||
console.timeEnd('graph')
|
||||
console.time('path')
|
||||
this.#path = Pathfind.shortestPath(graph, start, goal).map((waypoint) => new Vector2(waypoint[0], waypoint[1]))
|
||||
console.log(this.#path)
|
||||
console.timeEnd('path')
|
||||
console.timeEnd('pathfinding')
|
||||
}
|
||||
|
||||
@@ -140,6 +155,10 @@ export default class Entity {
|
||||
}
|
||||
|
||||
waypoints() {
|
||||
return this.game?.unadjustedWaypoints.map(([waypoint, direction]) => waypoint.clone().add(direction.clone().multiplyScalar(this.radius))) ?? []
|
||||
const entityColliders = (this.game?.entities ?? []).filter((e) => e.id != this.id)
|
||||
const terrainColliders = (this.game?.terrains ?? [])
|
||||
const unadjustedWaypoints = entityColliders.concat(terrainColliders).map((e) => e.unadjustedWaypoints).flat()
|
||||
|
||||
return unadjustedWaypoints.map(([waypoint, direction]) => waypoint.clone().add(direction.clone().multiplyScalar(this.radius))) ?? []
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user