use obstacle-in-path pathfinding
This commit is contained in:
+44
-21
@@ -4,18 +4,22 @@ import Projectile from './projectile.js'
|
||||
import Terrain from './terrain.js'
|
||||
|
||||
export default class Game {
|
||||
tickRate = 30
|
||||
averageTick = 0
|
||||
currentTick = 0
|
||||
width = 2000
|
||||
height = 2000
|
||||
nextTickAt = 0
|
||||
secondToSlowestTick = 0
|
||||
tickRate = 30
|
||||
width = 2000
|
||||
|
||||
#logic = null
|
||||
#currentTiming = 0
|
||||
#entities = []
|
||||
#eventEmitter = new EventEmitter()
|
||||
#logic = null
|
||||
#nextTickAt = 0
|
||||
#projectiles = []
|
||||
#terrains = []
|
||||
#tickBudget = 1000 / this.tickRate
|
||||
#timings = new Float32Array(this.tickRate)
|
||||
|
||||
get logic() { return this.#logic }
|
||||
get entities() { return this.#entities }
|
||||
@@ -84,23 +88,8 @@ export default class Game {
|
||||
}
|
||||
}
|
||||
|
||||
gameLoop() {
|
||||
const tickBudget = this.#tickBudget
|
||||
|
||||
if (this.nextTickAt != null) {
|
||||
const nextTickAt = this.nextTickAt
|
||||
this.nextTickAt = null
|
||||
|
||||
let start = performance.now()
|
||||
while (start < nextTickAt) { start = performance.now() }
|
||||
|
||||
this.update()
|
||||
this.nextTickAt = start + tickBudget
|
||||
}
|
||||
}
|
||||
|
||||
start() {
|
||||
setInterval(() => this.gameLoop(), 1)
|
||||
setInterval(() => this.#gameLoop(), 1)
|
||||
}
|
||||
|
||||
update() {
|
||||
@@ -110,7 +99,41 @@ export default class Game {
|
||||
this.#logic()
|
||||
}
|
||||
|
||||
this.currentTick++
|
||||
this.#calculateTickMetrics()
|
||||
this.eventEmitter.emit('tick')
|
||||
|
||||
this.currentTick++
|
||||
}
|
||||
|
||||
#calculateTickMetrics() {
|
||||
this.averageTick = Math.floor(10 * this.#timings.reduce((sum, t) => sum += t, 0) / this.#timings.length) / 10
|
||||
this.secondToSlowestTick = Math.floor(10 * this.#timings.toSorted().at(-2)) / 10
|
||||
}
|
||||
|
||||
#gameLoop() {
|
||||
const tickBudget = this.#tickBudget
|
||||
|
||||
if (this.#nextTickAt != null) {
|
||||
const nextTickAt = this.#nextTickAt
|
||||
this.#nextTickAt = null
|
||||
|
||||
let start = 0
|
||||
while (start < nextTickAt) { start = performance.now() }
|
||||
|
||||
const before = performance.now()
|
||||
this.update()
|
||||
this.#nextTickAt = start + tickBudget
|
||||
const after = performance.now()
|
||||
const taken = (after - before)
|
||||
|
||||
this.#timings[this.#currentTiming] = taken
|
||||
if (this.#currentTiming++ > this.#timings.length) {
|
||||
this.#currentTiming = 0
|
||||
}
|
||||
|
||||
if (after - before > tickBudget) {
|
||||
console.warn(`Can't keep up! A tick took ${taken.toFixed(1)} ms (Budget: ${tickBudget.toFixed(1)} ms)`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user