From 6e407951dc5a05d728f2f65c565b5427e0a4542c Mon Sep 17 00:00:00 2001 From: Pontus Alexander Date: Fri, 3 Apr 2020 14:14:51 +0200 Subject: [PATCH] Sync with published result of episode 21. --- public/js/Entity.js | 1 - public/js/EventBuffer.js | 1 - public/js/MusicController.js | 7 +++--- public/js/MusicPlayer.js | 7 ------ public/js/layers/dashboard.js | 32 ++++++++++++++++++++-------- public/js/loaders/level.js | 15 ++++++++----- public/js/main.js | 7 +++--- public/js/traits/Jump.js | 2 +- public/js/traits/LevelTimer.js | 2 +- public/js/traits/Player.js | 1 + public/js/traits/PlayerController.js | 9 -------- 11 files changed, 43 insertions(+), 41 deletions(-) diff --git a/public/js/Entity.js b/public/js/Entity.js index 5fc5b7c4..996eacd7 100644 --- a/public/js/Entity.js +++ b/public/js/Entity.js @@ -21,7 +21,6 @@ export class Trait { listen(name, callback, count = Infinity) { const listener = {name, callback, count}; this.listeners.push(listener); - console.log("Added listener", this, name); } finalize(entity) { diff --git a/public/js/EventBuffer.js b/public/js/EventBuffer.js index b6c1ca87..edee1dc9 100644 --- a/public/js/EventBuffer.js +++ b/public/js/EventBuffer.js @@ -6,7 +6,6 @@ export default class EventBuffer { emit(name, ...args) { const event = {name, args}; this.events.push(event); - console.log(this.events); } process(name, callback) { diff --git a/public/js/MusicController.js b/public/js/MusicController.js index 49bc02c4..5ef3c2d9 100644 --- a/public/js/MusicController.js +++ b/public/js/MusicController.js @@ -9,13 +9,14 @@ export default class MusicController { playTheme(speed = 1) { const audio = this.player.playTrack('main'); - audio.currentTime = 0; audio.playbackRate = speed; } playHurryTheme() { const audio = this.player.playTrack('hurry'); - this.player.waitForEnd(audio) - .then(() => this.playTheme(1.2)); + audio.loop = false; + audio.addEventListener('ended', () => { + this.playTheme(1.3); + }, {once: true}); } } diff --git a/public/js/MusicPlayer.js b/public/js/MusicPlayer.js index 273a411e..e36f96c6 100644 --- a/public/js/MusicPlayer.js +++ b/public/js/MusicPlayer.js @@ -18,11 +18,4 @@ export default class MusicPlayer { audio.play(); return audio; } - - waitForEnd(audio) { - audio.loop = false; - return new Promise(resolve => { - audio.addEventListener('ended', resolve, {once: true}); - }); - } } \ No newline at end of file diff --git a/public/js/layers/dashboard.js b/public/js/layers/dashboard.js index e6fbe943..9234dd1a 100644 --- a/public/js/layers/dashboard.js +++ b/public/js/layers/dashboard.js @@ -1,22 +1,36 @@ -export function createDashboardLayer(font, playerEnv) { +import {findPlayers} from "../player.js"; + +function getPlayerTrait(level) { + for (const entity of findPlayers(level)) { + return entity.player; + } +} + +function getTimerTrait(level) { + for (const entity of level.entities) { + if (entity.levelTimer) { + return entity.levelTimer; + } + } +} + +export function createDashboardLayer(font, level) { const LINE1 = font.size; const LINE2 = font.size * 2; - const coins = 13; - const score = 24500; + const playerTrait = getPlayerTrait(level); + const timerTrait = getTimerTrait(level); return function drawDashboard(context) { - const {score, time} = playerEnv.playerController; - - font.print('MARIO', context, 16, LINE1); - font.print(score.toString().padStart(6, '0'), context, 16, LINE2); + font.print(playerTrait.name, context, 16, LINE1); + font.print(playerTrait.score.toString().padStart(6, '0'), context, 16, LINE2); - font.print('@x' + coins.toString().padStart(2, '0'), context, 96, LINE2); + font.print('@x' + playerTrait.coins.toString().padStart(2, '0'), context, 96, LINE2); font.print('WORLD', context, 152, LINE1); font.print('1-1', context, 160, LINE2); font.print('TIME', context, 208, LINE1); - font.print(time.toFixed().toString().padStart(3, '0'), context, 216, LINE2); + font.print(timerTrait.currentTime.toFixed().toString().padStart(3, '0'), context, 216, LINE2); }; } diff --git a/public/js/loaders/level.js b/public/js/loaders/level.js index 49aee79e..598232ca 100644 --- a/public/js/loaders/level.js +++ b/public/js/loaders/level.js @@ -9,16 +9,21 @@ import {loadSpriteSheet} from './sprite.js'; import {loadJSON} from '../loaders.js'; function createTimer() { - const entity = new Entity(); - entity.addTrait(new LevelTimer()); - return entity; + const timer = new Entity(); + timer.addTrait(new LevelTimer()); + return timer; } function setupBehavior(level) { const timer = createTimer(); level.entities.add(timer); - level.events.listen(LevelTimer.EVENT_TIMER_OK, () => level.music.playTheme()); - level.events.listen(LevelTimer.EVENT_TIMER_HURRY, () => level.music.playHurryTheme()); + + level.events.listen(LevelTimer.EVENT_TIMER_OK, () => { + level.music.playTheme(); + }); + level.events.listen(LevelTimer.EVENT_TIMER_HURRY, () => { + level.music.playHurryTheme(); + }); } function setupBackgrounds(levelSpec, level, backgroundSprites) { diff --git a/public/js/main.js b/public/js/main.js index 7a76718b..da580772 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -8,7 +8,6 @@ import {setupKeyboard} from './input.js'; import {createCollisionLayer} from './layers/collision.js'; import {createDashboardLayer} from './layers/dashboard.js'; - async function main(canvas) { const context = canvas.getContext('2d'); const audioContext = new AudioContext(); @@ -26,14 +25,14 @@ async function main(canvas) { const camera = new Camera(); const mario = createPlayer(entityFactory.mario()); - window.mario = mario; + mario.player.name = "MARIO"; + level.entities.add(mario); const playerEnv = createPlayerEnv(mario); level.entities.add(playerEnv); - level.comp.layers.push(createCollisionLayer(level)); - level.comp.layers.push(createDashboardLayer(font, playerEnv)); + level.comp.layers.push(createDashboardLayer(font, level)); const input = setupKeyboard(mario); input.listenTo(window); diff --git a/public/js/traits/Jump.js b/public/js/traits/Jump.js index 7a43b3d7..e3f4afb5 100644 --- a/public/js/traits/Jump.js +++ b/public/js/traits/Jump.js @@ -34,7 +34,7 @@ export default class Jump extends Trait { } } - update(entity, {deltaTime}) { + update(entity, {deltaTime}, level) { if (this.requestTime > 0) { if (this.ready > 0) { entity.sounds.add('jump'); diff --git a/public/js/traits/LevelTimer.js b/public/js/traits/LevelTimer.js index 420a1524..a765e9fa 100644 --- a/public/js/traits/LevelTimer.js +++ b/public/js/traits/LevelTimer.js @@ -8,7 +8,7 @@ export default class LevelTimer extends Trait { super('levelTimer'); this.totalTime = 300; this.currentTime = this.totalTime; - this.hurryTime = 295; + this.hurryTime = 100; this.hurryEmitted = null; } diff --git a/public/js/traits/Player.js b/public/js/traits/Player.js index 4210846d..f27d3ea1 100644 --- a/public/js/traits/Player.js +++ b/public/js/traits/Player.js @@ -4,6 +4,7 @@ import Stomper from '../traits/Stomper.js'; export default class Player extends Trait { constructor() { super('player'); + this.name = "UNNAMED"; this.coins = 0; this.lives = 3; this.score = 0; diff --git a/public/js/traits/PlayerController.js b/public/js/traits/PlayerController.js index f143a0de..25c0f8f8 100644 --- a/public/js/traits/PlayerController.js +++ b/public/js/traits/PlayerController.js @@ -1,5 +1,4 @@ import {Trait} from '../Entity.js'; -import Stomper from '../traits/Stomper.js'; import {Vec2} from '../math.js'; export default class PlayerController extends Trait { @@ -7,12 +6,6 @@ export default class PlayerController extends Trait { super('playerController'); this.checkpoint = new Vec2(0, 0); this.player = null; - this.score = 0; - this.time = 300; - - this.listen('stomp', () => { - this.score += 100; - }); } setPlayer(entity) { @@ -24,8 +17,6 @@ export default class PlayerController extends Trait { this.player.killable.revive(); this.player.pos.set(this.checkpoint.x, this.checkpoint.y); level.entities.add(this.player); - } else { - this.time -= deltaTime * 2; } } }