Skip to content

Commit

Permalink
Decouple Timer class to module.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomle committed Sep 25, 2017
1 parent 0394ab7 commit ee34fc2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
29 changes: 29 additions & 0 deletions public/js/Timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default class Timer {
constructor(deltaTime) {
let accumulatedTime = 0;
let lastTime = 0;

this.updateProxy = (time) => {
accumulatedTime += (time - lastTime) / 1000;

while (accumulatedTime > deltaTime) {
this.update(deltaTime);
accumulatedTime -= deltaTime;
}

lastTime = time;

this.draw();

this.enqueue();
};
}

enqueue() {
requestAnimationFrame(this.updateProxy);
}

start() {
this.enqueue();
}
}
32 changes: 1 addition & 31 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Compositor from './Compositor.js';
import Entity from './Entity.js';
import Timer from './Timer.js';
import {createMario} from './entities.js';
import {loadLevel} from './loaders.js';
import {loadMarioSprite, loadBackgroundSprites} from './sprites.js';
Expand All @@ -9,37 +10,6 @@ const canvas = document.getElementById('screen');
const context = canvas.getContext('2d');


class Timer {
constructor(deltaTime) {
let accumulatedTime = 0;
let lastTime = 0;

this.updateProxy = (time) => {
accumulatedTime += (time - lastTime) / 1000;

while (accumulatedTime > deltaTime) {
this.update(deltaTime);
accumulatedTime -= deltaTime;
}

lastTime = time;

this.draw();

this.enqueue();
};
}

enqueue() {
requestAnimationFrame(this.updateProxy);
}

start() {
this.enqueue();
}
}


Promise.all([
createMario(),
loadBackgroundSprites(),
Expand Down

0 comments on commit ee34fc2

Please sign in to comment.