From 87c7c82dc6179b4daf564d053d13744f36a86faf Mon Sep 17 00:00:00 2001 From: Pontus Persson Date: Wed, 27 Sep 2017 11:00:18 +0200 Subject: [PATCH] Sync with published result of Episode 3. --- public/js/Timer.js | 6 ++---- public/js/entities.js | 6 +++--- public/js/layers.js | 6 ++---- public/js/main.js | 26 +++++++++----------------- public/js/math.js | 4 ++++ 5 files changed, 20 insertions(+), 28 deletions(-) diff --git a/public/js/Timer.js b/public/js/Timer.js index 6cdcedc5..783cf606 100644 --- a/public/js/Timer.js +++ b/public/js/Timer.js @@ -1,5 +1,5 @@ export default class Timer { - constructor(deltaTime) { + constructor(deltaTime = 1/60) { let accumulatedTime = 0; let lastTime = 0; @@ -13,10 +13,8 @@ export default class Timer { lastTime = time; - this.draw(); - this.enqueue(); - }; + } } enqueue() { diff --git a/public/js/entities.js b/public/js/entities.js index 0ae61349..b10e6b50 100644 --- a/public/js/entities.js +++ b/public/js/entities.js @@ -3,11 +3,11 @@ import {loadMarioSprite} from './sprites.js'; export function createMario() { return loadMarioSprite() - .then(marioSprite => { + .then(sprite => { const mario = new Entity(); mario.draw = function drawMario(context) { - marioSprite.draw('idle', context, this.pos.x, this.pos.y); + sprite.draw('idle', context, this.pos.x, this.pos.y); } mario.update = function updateMario(deltaTime) { @@ -17,4 +17,4 @@ export function createMario() { return mario; }); -} +} \ No newline at end of file diff --git a/public/js/layers.js b/public/js/layers.js index 67bd4dd1..929f35f0 100644 --- a/public/js/layers.js +++ b/public/js/layers.js @@ -22,10 +22,8 @@ export function createBackgroundLayer(backgrounds, sprites) { }; } -export function createSpriteLayer(entities) { +export function createSpriteLayer(entity) { return function drawSpriteLayer(context) { - entities.forEach(entity => { - entity.draw(context); - }); + entity.draw(context); }; } diff --git a/public/js/main.js b/public/js/main.js index c7c504a5..0de7e78f 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -1,15 +1,13 @@ 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'; +import {createMario} from './entities.js'; +import {loadBackgroundSprites} from './sprites.js'; import {createBackgroundLayer, createSpriteLayer} from './layers.js'; const canvas = document.getElementById('screen'); const context = canvas.getContext('2d'); - Promise.all([ createMario(), loadBackgroundSprites(), @@ -21,26 +19,20 @@ Promise.all([ const backgroundLayer = createBackgroundLayer(level.backgrounds, backgroundSprites); comp.layers.push(backgroundLayer); - mario.pos.x = 64; - mario.pos.y = 180; - mario.vel.x = 200; - mario.vel.y = -600; + const gravity = 30; + mario.pos.set(64, 180); + mario.vel.set(200, -600); - const entities = [mario]; - const spriteLayer = createSpriteLayer(entities); + const spriteLayer = createSpriteLayer(mario); comp.layers.push(spriteLayer); const timer = new Timer(1/60); timer.update = function update(deltaTime) { - entities.forEach(entity => { - entity.update(deltaTime); - entity.vel.y += 30; - }); - }; - timer.draw = function draw() { comp.draw(context); - }; + mario.update(deltaTime); + mario.vel.y += gravity; + } timer.start(); }); diff --git a/public/js/math.js b/public/js/math.js index 4de531bc..734c152c 100644 --- a/public/js/math.js +++ b/public/js/math.js @@ -1,5 +1,9 @@ export class Vec2 { constructor(x, y) { + this.set(x, y); + } + + set(x, y) { this.x = x; this.y = y; }