Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
startGame() {
this.gameIsRunning = true;
this.nextTimestep = this.nextTimestep || Date.now();
while (Date.now() > this.nextTimestep) {
this.frame++
!this.inputBuffer.isEmpty() && this.processInputBuffer();
Engine.update(this.engine, TIMESTEP);
if (!this.targetScoreInterval) { this.reduceTargetScoreInterval() }
if (!this.winner && this.gameIsOver()) {
this.winner = true;
clearInterval(this.targetScoreInterval);
this.endRound();
}
let snapshot = this.generateSnapshot(this.chips, this.pegs, this.score,
this.winner, this.targetScore);
this.broadcastSnapshot(snapshot);
this.nextTimestep += TIMESTEP;
}
loop() {
const currTime = 0.001 * Date.now();
Engine.update(
this.engine,
1000 / 60,
this.lastTime ? currTime / this.lastTime : 1
);
this.lastTime = currTime;
}
}
getElementsForNextFrame(deltaSeconds) {
PhysicsEngine.update(this._physicsEngine, deltaSeconds);
let elementsToReturn = [];
for (let physicsBody of this._physicsEngine.world.bodies) {
if (physicsBody.isStatic) {
continue;
}
physicsBody.element.x = physicsBody.position.x;
physicsBody.element.y = physicsBody.position.y;
physicsBody.element.rotation = physicsBody.angle;
elementsToReturn.push(physicsBody.element);
}
return elementsToReturn;
}
loop() {
const currTime = 0.001 * Date.now();
Engine.update(
this.engine,
1000 / 60,
this.lastTime ? currTime / this.lastTime : 1,
);
this.lastTime = currTime;
}
}
draw(ctx) {
this.progress = this.renderer.progress;
this.progressPosition = this.renderer.width * 0.3;
const { chord } = this.renderer;
Engine.update(this.engine);
const bodies = Composite.allBodies(this.engine.world);
if (this.avatar) {
this.avatar.update();
}
if (chord) {
this.avatarChord.update();
}
ctx.save();
ctx.save();
if (this.checkBand()) {
ctx.fillStyle = palette[6];
} else {
this.loop = setInterval(() => {
while (Date.now() > this.nextTimestep) {
Engine.update(this.engine, TIMESTEP);
this.nextTimestep += TIMESTEP;
}
this.renderer.render(this.stage);
}, 0)
}
update() {
for (let k in this.state.entities) {
this.state.entities[k].update(this.clock.deltaTime);
}
Engine.update(this.engine, this.clock.deltaTime);
}
this.loop = setInterval(() => {
while (Date.now() > this.nextTimestep) {
Engine.update(this.engine, TIMESTEP);
this.nextTimestep += TIMESTEP;
}
let interpolation = (Date.now() + TIMESTEP - this.nextTimestep)
/ TIMESTEP;
this.env === 'client' && this.chips.forEach(chip => {
chip.sprite.destination = chip.body.position;
chip.sprite.begin = chip.sprite.position;
let incrementX = chip.sprite.destination.x - chip.sprite.begin.x;
let incrementY = chip.sprite.destination.y - chip.sprite.begin.y;
chip.sprite.position.x += incrementX * interpolation;
chip.sprite.position.y += incrementY * interpolation;
chip.sprite.rotation = chip.body.angle;