add pathfinding times report
This commit is contained in:
+20
-12
@@ -29,13 +29,6 @@ export default class Entity {
|
||||
set x(value) { this.position.x = value }
|
||||
set y(value) { this.position.y = value }
|
||||
|
||||
get collidables() {
|
||||
const entityColliders = (this.game?.entities ?? []).filter((e) => e.id != this.id).map((e) => e.collider)
|
||||
const terrainColliders = (this.game?.terrains ?? []).map((t) => t.colliders).flat()
|
||||
|
||||
return entityColliders.concat(terrainColliders)
|
||||
}
|
||||
|
||||
get collider() {
|
||||
return new SAT.Circle(new SAT.Vector(this.x, this.y), this.radius)
|
||||
}
|
||||
@@ -64,6 +57,13 @@ export default class Entity {
|
||||
])
|
||||
}
|
||||
|
||||
collidables() {
|
||||
const entityColliders = (this.game?.entities ?? []).filter((e) => e.id != this.id).map((e) => e.collider)
|
||||
const terrainColliders = (this.game?.terrains ?? []).map((t) => t.colliders).flat()
|
||||
|
||||
return entityColliders.concat(terrainColliders)
|
||||
}
|
||||
|
||||
isColliding(...colliders) {
|
||||
return SATX.collideObjects(this.collider, colliders)
|
||||
}
|
||||
@@ -86,15 +86,23 @@ export default class Entity {
|
||||
this.position.set(x, y)
|
||||
}
|
||||
|
||||
takeStep(distanceTraveled = 0) {
|
||||
async takeStep(distanceTraveled = 0) {
|
||||
const speed = (this.speed / (this.game?.tickBudget ?? 1000)) - distanceTraveled
|
||||
if (this.#dest != null) {
|
||||
const fixedDest = SATX.clamp(SATX.fixCollisions(this.#dest, this.collidables, this.radius), this.game?.width, this.game?.height, this.radius)
|
||||
const fixedDest = SATX.clamp(SATX.fixCollisions(this.#dest, this.collidables(), this.radius), this.game?.width, this.game?.height, this.radius)
|
||||
|
||||
if (this.#path.length < 1 || !this.#path.at(-1).equals(this.#dest)) {
|
||||
console.time('pathfinding')
|
||||
console.time('waypoints')
|
||||
const waypoints = (this.game?.unadjustedWaypoints.map(([unadjusted, direction]) => unadjusted.clone().add(direction.clone().multiplyScalar(this.radius))) ?? []).concat([this.position, fixedDest])
|
||||
const graph = Pathfind.buildGraph(waypoints, this.collidables, this.radius)
|
||||
console.timeEnd('waypoints')
|
||||
console.time('graph')
|
||||
const graph = Pathfind.buildGraph(waypoints, this.collidables(), this.radius)
|
||||
console.timeEnd('graph')
|
||||
console.time('path')
|
||||
this.#path = Pathfind.shortestPath(graph, this.position, fixedDest)
|
||||
console.timeEnd('path')
|
||||
console.timeEnd('pathfinding')
|
||||
}
|
||||
|
||||
if (this.#path.length > 0) {
|
||||
@@ -105,7 +113,7 @@ export default class Entity {
|
||||
const position = distance <= speed ? destination : stepTaken
|
||||
|
||||
const collider = Entity.collider(position.x, position.y, this.radius)
|
||||
const isColliding = SATX.collideObjects(collider, this.collidables)
|
||||
const isColliding = SATX.collideObjects(collider, this.collidables())
|
||||
|
||||
if (!isColliding) {
|
||||
this.position.copy(position)
|
||||
@@ -114,7 +122,7 @@ export default class Entity {
|
||||
if (this.position.equals(destination)) {
|
||||
this.#path = this.#path.slice(1)
|
||||
if (this.#path.length > 0) {
|
||||
this.takeStep(distance)
|
||||
await this.takeStep(distance)
|
||||
}
|
||||
else {
|
||||
this.#dest = null
|
||||
|
||||
Reference in New Issue
Block a user