This repository has been archived on 2026-05-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
instructions-clear/src/game.js
T
2024-12-22 17:00:23 +09:00

35 lines
864 B
JavaScript

import { System } from 'detect-collisions'
import { EventEmitter } from 'node:events'
export default class Game {
entities = []
tickRate = 30
currentTick = 0
#eventEmitter = new EventEmitter()
#system = new System()
#tickBudget = Math.floor(1000 / this.tickRate)
get eventEmitter() { return this.#eventEmitter }
get system() { return this.#system }
get tickBudget() { return this.#tickBudget }
spawn_entity(entity) {
this.entities.push(entity)
this.#system.insert(entity.collider)
entity.game = this
}
async start() {
const start = performance.now()
await this.update()
setTimeout(() => this.start(), Math.max(0, this.#tickBudget - (performance.now() - start)))
}
async update() {
Promise.allSettled(this.entities.map((e) => e.update()))
this.currentTick++
this.eventEmitter.emit('tick')
}
}