add skillshots

This commit is contained in:
2025-01-12 00:11:00 +09:00
parent 957b09b878
commit 51b61ab449
7 changed files with 275 additions and 30 deletions
+39 -8
View File
@@ -1,4 +1,7 @@
import { EventEmitter } from 'node:events'
import Entity from './entity.js'
import Terrain from './terrain.js'
import Projectile from './projectile.js'
export default class Game {
tickRate = 30
@@ -8,11 +11,13 @@ export default class Game {
#entities = []
#eventEmitter = new EventEmitter()
#projectiles = []
#terrains = []
#tickBudget = Math.floor(1000 / this.tickRate)
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 }
@@ -20,29 +25,54 @@ export default class Game {
return this.terrains.map((t) => t.unadjustedWaypoints).concat(this.entities.map((e) => e.unadjustedWaypoints)).flat()
}
spawn_entity(entity) {
this.#entities.push(entity)
entity.game = this
addTerrain(terrain) {
this.#terrains.push(terrain)
}
despawn(entity) {
despawn(object) {
if (object instanceof Entity) { this.despawnEntity(object) }
else if (object instanceof Terrain) { this.removeTerrain(object) }
else if (object instanceof Projectile) { this.despawnProjectile(object) }
else { console.error({ error: { reason: 'Can\'t despawn object', object } }) }
}
despawnEntity(entity) {
this.#entities = this.#entities.filter((e) => e.id != entity.id)
entity.game = null
}
add_terrain(terrain) {
this.#terrains.push(terrain)
despawnProjectile(projectile) {
this.#projectiles = this.#projectiles.filter((p) => p.id != projectile.id)
projectile.game = null
}
remove_terrain(terrain) {
removeTerrain(terrain) {
this.#terrains = this.#terrains.filter((t) => t.id != terrain.id)
}
spawn(object) {
if (object instanceof Entity) { this.spawnEntity(object) }
else if (object instanceof Terrain) { this.addTerrain(object) }
else if (object instanceof Projectile) { this.spawnProjectile(object) }
else { console.error({ error: { reason: 'Can\'t spawn object', object } }) }
}
spawnEntity(entity) {
this.#entities.push(entity)
entity.game = this
}
spawnProjectile(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()),
}
}
@@ -53,7 +83,8 @@ export default class Game {
}
update() {
this.#entities.map((e) => e.update())
this.#entities.forEach((e) => e.update())
this.#projectiles.forEach((p) => p.update())
this.currentTick++
this.eventEmitter.emit('tick')
}