fix game loop timer
This commit is contained in:
+6
-2
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -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
@@ -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() {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user