Skip to content

Commit

Permalink
Sync with published result of Episode 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomle committed Sep 27, 2017
1 parent ee34fc2 commit 87c7c82
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
6 changes: 2 additions & 4 deletions public/js/Timer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class Timer {
constructor(deltaTime) {
constructor(deltaTime = 1/60) {
let accumulatedTime = 0;
let lastTime = 0;

Expand All @@ -13,10 +13,8 @@ export default class Timer {

lastTime = time;

this.draw();

this.enqueue();
};
}
}

enqueue() {
Expand Down
6 changes: 3 additions & 3 deletions public/js/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -17,4 +17,4 @@ export function createMario() {

return mario;
});
}
}
6 changes: 2 additions & 4 deletions public/js/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
26 changes: 9 additions & 17 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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();
});
4 changes: 4 additions & 0 deletions public/js/math.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export class Vec2 {
constructor(x, y) {
this.set(x, y);
}

set(x, y) {
this.x = x;
this.y = y;
}
Expand Down

0 comments on commit 87c7c82

Please sign in to comment.