rely on stringification instead of state reports

This commit is contained in:
2025-01-17 23:04:38 +09:00
parent 80ccb92815
commit 9345c7af04
9 changed files with 123 additions and 73 deletions
+13 -23
View File
@@ -1,31 +1,30 @@
import { EventEmitter } from 'node:events'
import Ability from './ability.js'
import Entity from './entity.js'
import Projectile from './projectile.js'
import Terrain from './terrain.js'
export default class Game {
abilities = Object.values({...Ability})
averageTick = 0
currentTick = 0
entities = []
height = 2000
projectiles = []
secondToSlowestTick = 0
terrains = []
tickRate = 30
width = 2000
#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 }
get eventEmitter() { return this.#eventEmitter }
get projectiles() { return this.#projectiles }
get terrains() { return this.#terrains }
get tickBudget() { return this.#tickBudget }
set logic(value) { this.#logic = value }
@@ -34,7 +33,7 @@ export default class Game {
}
addTerrain(terrain) {
this.#terrains.push(terrain)
this.terrains.push(terrain)
}
despawn(object) {
@@ -45,17 +44,17 @@ export default class Game {
}
despawnEntity(entity) {
this.#entities = this.#entities.filter((e) => e.id != entity.id)
this.entities = this.entities.filter((e) => e.id != entity.id)
entity.game = null
}
despawnProjectile(projectile) {
this.#projectiles = this.#projectiles.filter((p) => p.id != projectile.id)
this.projectiles = this.projectiles.filter((p) => p.id != projectile.id)
projectile.game = null
}
removeTerrain(terrain) {
this.#terrains = this.#terrains.filter((t) => t.id != terrain.id)
this.terrains = this.terrains.filter((t) => t.id != terrain.id)
}
secToTick(sec) {
@@ -70,31 +69,22 @@ export default class Game {
}
spawnEntity(entity) {
this.#entities.push(entity)
this.entities.push(entity)
entity.game = this
}
spawnProjectile(projectile) {
this.#projectiles.push(projectile)
this.projectiles.push(projectile)
projectile.game = this
}
state() {
return {
...this,
entities: this.#entities.map((e) => e.state()),
terrains: this.#terrains.map((t) => t.state()),
projectiles: this.#projectiles.map((p) => p.state()),
}
}
start() {
setInterval(() => this.#gameLoop(), 1)
}
update() {
this.#entities.forEach((e) => e.update())
this.#projectiles.forEach((p) => p.update())
this.entities.forEach((e) => e.update())
this.projectiles.forEach((p) => p.update())
if (this.#logic != null) {
this.#logic()
}