fix game loop timer

This commit is contained in:
2025-01-12 10:57:22 +09:00
parent e0dd7dcaf3
commit d9849f770b
6 changed files with 123 additions and 13 deletions
+6 -2
View File
@@ -4,12 +4,16 @@ export default class Ability {
id = crypto.randomUUID()
name = 'Ability'
cooldown = 0
effect = () => {}
castTime = 0
#effect = () => {}
get effect() { return this.#effect }
set effect(value) { this.#effect = value }
constructor(options) {
if (options.name != null) { this.name = options.name }
if (options.effect != null) { this.effect = options.effect }
if (options.effect != null) { this.#effect = options.effect }
if (options.cooldown != null) { this.cooldown = options.cooldown }
if (options.castTime != null) { this.castTime = options.castTime }
}
-2
View File
@@ -3,7 +3,6 @@ import SAT from 'sat'
import SATX from './satx.js'
import Pathfind from './pathfind.js'
import Ability from './ability.js'
import Effect from './effect.js'
export default class Entity {
id = crypto.randomUUID()
@@ -16,7 +15,6 @@ export default class Entity {
Ability.straightShot(800, 7, 3000),
() => {},
() => {},
() => {},
]
casting = null
// TODO: teams
+19 -4
View File
@@ -8,12 +8,14 @@ export default class Game {
currentTick = 0
width = 2000
height = 2000
running = false
nextTick = 0
#entities = []
#eventEmitter = new EventEmitter()
#projectiles = []
#terrains = []
#tickBudget = Math.floor(1000 / this.tickRate)
#tickBudget = 1000 / this.tickRate
get entities() { return this.#entities }
get eventEmitter() { return this.#eventEmitter }
@@ -80,10 +82,23 @@ export default class Game {
}
}
gameLoop() {
const tickBudget = this.#tickBudget
if (this.nextTick != null) {
const nextTick = this.nextTick
this.nextTick = null
let start = performance.now()
while (start < nextTick) { start = performance.now() }
this.update()
this.nextTick = start + tickBudget
}
}
start() {
const start = performance.now()
this.update()
setTimeout(() => this.start(), Math.max(0, this.#tickBudget - (performance.now() - start)))
setInterval(() => this.gameLoop(), 1)
}
update() {
-1
View File
@@ -3,7 +3,6 @@ import { WebSocketExpress } from 'websocket-express'
import Game from './game.js'
import Entity from './entity.js'
import Terrain from './terrain.js'
import SATX from './satx.js'
import { Vector2 } from 'three'
const app = new WebSocketExpress()