Skip to content

Commit

Permalink
Sync with published result of episode 21.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomle committed Apr 3, 2020
1 parent 47440b3 commit 6e40795
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 41 deletions.
1 change: 0 additions & 1 deletion public/js/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion public/js/EventBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions public/js/MusicController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
7 changes: 0 additions & 7 deletions public/js/MusicPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});
}
}
32 changes: 23 additions & 9 deletions public/js/layers/dashboard.js
Original file line number Diff line number Diff line change
@@ -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);
};
}
15 changes: 10 additions & 5 deletions public/js/loaders/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion public/js/traits/Jump.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion public/js/traits/LevelTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions public/js/traits/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 0 additions & 9 deletions public/js/traits/PlayerController.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import {Trait} from '../Entity.js';
import Stomper from '../traits/Stomper.js';
import {Vec2} from '../math.js';

export default class PlayerController extends Trait {
constructor() {
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) {
Expand All @@ -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;
}
}
}

0 comments on commit 6e40795

Please sign in to comment.