From dfdd5e47deec1fe61de4d7f57d7b6ecdf0541fb0 Mon Sep 17 00:00:00 2001 From: Pontus Alexander Date: Wed, 19 Feb 2020 23:20:03 +0100 Subject: [PATCH] Sync with published result of episode 15. --- public/js/entities.js | 4 ++-- public/js/entities/Mario.js | 8 ++++---- public/js/main.js | 13 ++++++++----- public/js/traits/Go.js | 3 +-- public/js/traits/Jump.js | 3 +-- public/js/traits/Killable.js | 4 ++-- public/js/traits/PendulumMove.js | 9 +-------- public/js/traits/Stomper.js | 10 +--------- 8 files changed, 20 insertions(+), 34 deletions(-) diff --git a/public/js/entities.js b/public/js/entities.js index 7f97c896..a735ed8d 100644 --- a/public/js/entities.js +++ b/public/js/entities.js @@ -13,8 +13,8 @@ export function loadEntities(audioContext) { return Promise.all([ loadMario(audioContext).then(addAs('mario')), - loadGoomba().then(addAs('goomba')), - loadKoopa().then(addAs('koopa')), + loadGoomba(audioContext).then(addAs('goomba')), + loadKoopa(audioContext).then(addAs('koopa')), ]) .then(() => entityFactories); } \ No newline at end of file diff --git a/public/js/entities/Mario.js b/public/js/entities/Mario.js index 58d2360e..87c88110 100644 --- a/public/js/entities/Mario.js +++ b/public/js/entities/Mario.js @@ -16,12 +16,12 @@ export function loadMario(audioContext) { loadSpriteSheet('mario'), loadAudioBoard('mario', audioContext), ]) - .then(([sprite, audioBoard]) => { - return createMarioFactory(sprite, audioBoard); + .then(([sprite, audio]) => { + return createMarioFactory(sprite, audio); }); } -function createMarioFactory(sprite, audioBoard) { +function createMarioFactory(sprite, audio) { const runAnim = sprite.animations.get('run'); function routeFrame(mario) { @@ -50,7 +50,7 @@ function createMarioFactory(sprite, audioBoard) { return function createMario() { const mario = new Entity(); - mario.audio = audioBoard; + mario.audio = audio; mario.size.set(14, 16); mario.addTrait(new Physics()); diff --git a/public/js/main.js b/public/js/main.js index c1797ef2..94c09fe7 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -1,10 +1,8 @@ -import AudioBoard from './AudioBoard.js'; import Camera from './Camera.js'; import Entity from './Entity.js'; import PlayerController from './traits/PlayerController.js'; import Timer from './Timer.js'; import {createLevelLoader} from './loaders/level.js'; -import {createAudioLoader} from './loaders/audio.js'; import {loadFont} from './loaders/font.js'; import {loadEntities} from './entities.js'; import {setupKeyboard} from './input.js'; @@ -29,9 +27,8 @@ async function main(canvas) { loadFont(), ]); - const loadAudio = createAudioLoader(audioContext); - const loadLevel = createLevelLoader(entityFactory); + const loadLevel = await createLevelLoader(entityFactory); const level = await loadLevel('1-1'); @@ -68,4 +65,10 @@ async function main(canvas) { } const canvas = document.getElementById('screen'); -main(canvas); + +const start = () => { + window.removeEventListener('click', start); + main(canvas); +}; + +window.addEventListener('click', start); diff --git a/public/js/traits/Go.js b/public/js/traits/Go.js index 7601bd98..e7d3be35 100644 --- a/public/js/traits/Go.js +++ b/public/js/traits/Go.js @@ -13,8 +13,7 @@ export default class Go extends Trait { this.heading = 1; } - update(entity, gameContext) { - const deltaTime = gameContext.deltaTime; + update(entity, {deltaTime}) { const absX = Math.abs(entity.vel.x); if (this.dir !== 0) { diff --git a/public/js/traits/Jump.js b/public/js/traits/Jump.js index 278831d9..c98db112 100644 --- a/public/js/traits/Jump.js +++ b/public/js/traits/Jump.js @@ -34,8 +34,7 @@ export default class Jump extends Trait { } } - update(entity, gameContext, level) { - const deltaTime = gameContext.deltaTime; + update(entity, {deltaTime}) { if (this.requestTime > 0) { if (this.ready > 0) { this.sounds.add('jump'); diff --git a/public/js/traits/Killable.js b/public/js/traits/Killable.js index 80384c2e..9cc03f62 100644 --- a/public/js/traits/Killable.js +++ b/public/js/traits/Killable.js @@ -17,9 +17,9 @@ export default class Killable extends Trait { this.deadTime = 0; } - update(entity, gameContext, level) { + update(entity, {deltaTime}, level) { if (this.dead) { - this.deadTime += gameContext.deltaTime; + this.deadTime += deltaTime; if (this.deadTime > this.removeAfter) { this.queue(() => { level.entities.delete(entity); diff --git a/public/js/traits/PendulumMove.js b/public/js/traits/PendulumMove.js index 9cb99e34..4111ae2f 100644 --- a/public/js/traits/PendulumMove.js +++ b/public/js/traits/PendulumMove.js @@ -4,23 +4,16 @@ export default class PendulumMove extends Trait { constructor() { super('pendulumMove'); this.enabled = true; - this.bumped = false; this.speed = -30; } obstruct(entity, side) { if (side === Sides.LEFT || side === Sides.RIGHT) { this.speed = -this.speed; - this.bumped = true; } } - update(entity, {audioContext}) { - if (this.bumped) { - entity.audio.playAudio('bump', audioContext); - this.bumped = false; - } - + update(entity) { if (this.enabled) { entity.vel.x = this.speed; } diff --git a/public/js/traits/Stomper.js b/public/js/traits/Stomper.js index c666abc8..0c785a9d 100644 --- a/public/js/traits/Stomper.js +++ b/public/js/traits/Stomper.js @@ -4,7 +4,6 @@ export default class Stomper extends Trait { constructor() { super('stomper'); this.bounceSpeed = 400; - this.didStomp = false; this.onStomp = function() { } @@ -22,15 +21,8 @@ export default class Stomper extends Trait { if (us.vel.y > them.vel.y) { this.bounce(us, them); - this.didStomp = true; - this.onStomp(us, them); - } - } - - update(entity) { - if (this.didStomp) { this.sounds.add('stomp'); - this.didStomp = false; + this.onStomp(us, them); } } }