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 x(value) { this.position.x = value }
|
||||||
set y(value) { this.position.y = 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() {
|
get collider() {
|
||||||
return new SAT.Circle(new SAT.Vector(this.x, this.y), this.radius)
|
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) {
|
isColliding(...colliders) {
|
||||||
return SATX.collideObjects(this.collider, colliders)
|
return SATX.collideObjects(this.collider, colliders)
|
||||||
}
|
}
|
||||||
@@ -86,15 +86,23 @@ export default class Entity {
|
|||||||
this.position.set(x, y)
|
this.position.set(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
takeStep(distanceTraveled = 0) {
|
async takeStep(distanceTraveled = 0) {
|
||||||
const speed = (this.speed / (this.game?.tickBudget ?? 1000)) - distanceTraveled
|
const speed = (this.speed / (this.game?.tickBudget ?? 1000)) - distanceTraveled
|
||||||
if (this.#dest != null) {
|
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)) {
|
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 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)
|
this.#path = Pathfind.shortestPath(graph, this.position, fixedDest)
|
||||||
|
console.timeEnd('path')
|
||||||
|
console.timeEnd('pathfinding')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.#path.length > 0) {
|
if (this.#path.length > 0) {
|
||||||
@@ -105,7 +113,7 @@ export default class Entity {
|
|||||||
const position = distance <= speed ? destination : stepTaken
|
const position = distance <= speed ? destination : stepTaken
|
||||||
|
|
||||||
const collider = Entity.collider(position.x, position.y, this.radius)
|
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) {
|
if (!isColliding) {
|
||||||
this.position.copy(position)
|
this.position.copy(position)
|
||||||
@@ -114,7 +122,7 @@ export default class Entity {
|
|||||||
if (this.position.equals(destination)) {
|
if (this.position.equals(destination)) {
|
||||||
this.#path = this.#path.slice(1)
|
this.#path = this.#path.slice(1)
|
||||||
if (this.#path.length > 0) {
|
if (this.#path.length > 0) {
|
||||||
this.takeStep(distance)
|
await this.takeStep(distance)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.#dest = null
|
this.#dest = null
|
||||||
|
|||||||
@@ -4,10 +4,6 @@ import Game from './game.js'
|
|||||||
import Entity from './entity.js'
|
import Entity from './entity.js'
|
||||||
import Terrain from './terrain.js'
|
import Terrain from './terrain.js'
|
||||||
|
|
||||||
import { Vector2 } from 'three'
|
|
||||||
import Pathfind from './pathfind.js'
|
|
||||||
import SATX from './satx.js'
|
|
||||||
|
|
||||||
const app = new WebSocketExpress()
|
const app = new WebSocketExpress()
|
||||||
const port = 1280
|
const port = 1280
|
||||||
const game = new Game()
|
const game = new Game()
|
||||||
|
|||||||
Reference in New Issue
Block a user