From ee34fc2854901f7d708a90e86d9317437efb1f0b Mon Sep 17 00:00:00 2001 From: Pontus Persson Date: Mon, 25 Sep 2017 20:59:03 +0200 Subject: [PATCH] Decouple Timer class to module. --- public/js/Timer.js | 29 +++++++++++++++++++++++++++++ public/js/main.js | 32 +------------------------------- 2 files changed, 30 insertions(+), 31 deletions(-) create mode 100644 public/js/Timer.js diff --git a/public/js/Timer.js b/public/js/Timer.js new file mode 100644 index 00000000..6cdcedc5 --- /dev/null +++ b/public/js/Timer.js @@ -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(); + } +} diff --git a/public/js/main.js b/public/js/main.js index 19711ed0..c7c504a5 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -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'; @@ -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(),