Skip to content

Commit

Permalink
Sync with published result of episode 17.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomle committed Mar 10, 2020
1 parent f2af78d commit 3067565
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 48 deletions.
1 change: 1 addition & 0 deletions public/js/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class Entity {
constructor() {
this.audio = new AudioBoard();
this.sounds = new Set();

this.pos = new Vec2(0, 0);
this.vel = new Vec2(0, 0);
this.size = new Vec2(0, 0);
Expand Down
19 changes: 10 additions & 9 deletions public/js/entities.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import {loadBulletBill} from './entities/BulletBill.js';
import {loadMario} from './entities/Mario.js';
import {loadGoomba} from './entities/Goomba.js';
import {loadKoopa} from './entities/Koopa.js';
import {loadBullet} from './entities/Bullet.js';
import {loadCannon} from './entities/Cannon.js';

export async function loadEntities(audioContext) {

export function loadEntities(audioContext) {
const entityFactories = {};

function addAs(name) {
return factory => entityFactories[name] = factory;
}

await Promise.all([

return Promise.all([
loadMario(audioContext).then(addAs('mario')),
loadGoomba(audioContext).then(addAs('goomba')),
loadKoopa(audioContext).then(addAs('koopa')),
loadBulletBill(audioContext).then(addAs('bulletBill')),
]);

await loadCannon(entityFactories, audioContext).then(addAs('cannon'));
loadBullet(audioContext).then(addAs('bullet')),
loadCannon(audioContext, entityFactories).then(addAs('cannon')),

return entityFactories;
}
])
.then(() => entityFactories);
}
41 changes: 24 additions & 17 deletions public/js/entities/Cannon.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import {findPlayers} from '../player.js';
import Entity, {Trait} from '../Entity.js';
import Entity from '../Entity.js';
import Emitter from '../traits/Emitter.js';
import {findPlayers} from '../player.js';
import {loadAudioBoard} from '../loaders/audio.js';

export function loadCannon(entityFactory, audioContext) {
const HOLD_FIRE_THRESHOLD = 30;

export function loadCannon(audioContext, entityFactories) {
return loadAudioBoard('cannon', audioContext)
.then(audio => {
return createCannonFactory(entityFactory, audio);
return createCannonFactory(audio, entityFactories);
});
}

function createCannonFactory(entityFactory, audio) {
const createBullet = entityFactory.bulletBill;
function createCannonFactory(audio, entityFactories) {

const bulletEmitter = (entity, level) => {

function emitBullet(cannon, level) {
let dir = 1;
for (const player of findPlayers(level)) {
if (player.pos.x > entity.pos.x - 30 && player.pos.x < entity.pos.x + 30) {
if (player.pos.x > cannon.pos.x - HOLD_FIRE_THRESHOLD
&& player.pos.x < cannon.pos.x + HOLD_FIRE_THRESHOLD) {
return;
}

if (player.pos.x < cannon.pos.x) {
dir = -1;
}
}

entity.sounds.add('shoot');
const bullet = entityFactories.bullet();

bullet.pos.copy(cannon.pos);
bullet.vel.set(80 * dir, 0);

const bullet = createBullet();
bullet.vel.set(80, 0);
bullet.pos.copy(entity.pos);
cannon.sounds.add('shoot');
level.entities.add(bullet);
};
}

return function createCannon() {
const cannon = new Entity();
cannon.audio = audio;

const emitter = new Emitter();
emitter.interval = 4;
emitter.emitters.push(bulletEmitter);

emitter.emitters.push(emitBullet);
cannon.addTrait(emitter);

return cannon;
};
}
}
6 changes: 4 additions & 2 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Timer from './Timer.js';
import {createLevelLoader} from './loaders/level.js';
import {loadFont} from './loaders/font.js';
import {loadEntities} from './entities.js';
import {createPlayer, createPlayerEnv} from './player.js';
import {setupKeyboard} from './input.js';
import {createPlayerEnv, createPlayer} from './player.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 @@ -24,11 +25,12 @@ async function main(canvas) {

const camera = new Camera();

const mario = createPlayer(entityFactory.mario);
const mario = createPlayer(entityFactory.mario());

const playerEnv = createPlayerEnv(mario);
level.entities.add(playerEnv);


level.comp.layers.push(createCollisionLayer(level));
level.comp.layers.push(createDashboardLayer(font, playerEnv));

Expand Down
3 changes: 2 additions & 1 deletion public/js/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export class Vec2 {
}

copy(vec2) {
this.set(vec2.x, vec2.y);
this.x = vec2.x;
this.y = vec2.y;
}

set(x, y) {
Expand Down
7 changes: 3 additions & 4 deletions public/js/player.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Entity from './Entity.js';
import PlayerController from './traits/PlayerController.js';
import Player from './traits/Player.js';
import PlayerController from './traits/PlayerController.js';

export function createPlayerEnv(playerEntity) {
const playerEnv = new Entity();
Expand All @@ -11,13 +11,12 @@ export function createPlayerEnv(playerEntity) {
return playerEnv;
}

export function createPlayer(factory) {
const entity = factory();
export function createPlayer(entity) {
entity.addTrait(new Player());
return entity;
}

export function* findPlayers (level) {
export function* findPlayers(level) {
for (const entity of level.entities) {
if (entity.player) {
yield entity;
Expand Down
2 changes: 1 addition & 1 deletion public/js/traits/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {Trait} from '../Entity.js';
export default class Emitter extends Trait {
constructor() {
super('emitter');
this.emitters = [];
this.interval = 2;
this.coolDown = this.interval;
this.emitters = [];
}

emit(entity, level) {
Expand Down
8 changes: 2 additions & 6 deletions public/js/traits/Physics.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import {Trait} from '../Entity.js';
import Gravity from './Gravity.js';

export default class Physics extends Trait {
constructor() {
super('physics');
this.gravity = new Gravity();
}

update(entity, gameContext, level) {
const {deltaTime} = gameContext;

update(entity, {deltaTime}, level) {
entity.pos.x += entity.vel.x * deltaTime;
level.tileCollider.checkX(entity);

entity.pos.y += entity.vel.y * deltaTime;
level.tileCollider.checkY(entity);

this.gravity.update(entity, gameContext, level);
entity.vel.y += level.gravity * deltaTime;
}
}
1 change: 0 additions & 1 deletion public/js/traits/Player.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Trait} from '../Entity.js';
import {Vec2} from '../math.js';

export default class Player extends Trait {
constructor() {
Expand Down
8 changes: 3 additions & 5 deletions public/js/traits/Stomper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ export default class Stomper extends Trait {
}

bounce(us, them) {
this.queue(() => {
us.bounds.bottom = them.bounds.top;
us.vel.y = -this.bounceSpeed;
});
us.bounds.bottom = them.bounds.top;
us.vel.y = -this.bounceSpeed;
}

collides(us, them) {
Expand All @@ -19,7 +17,7 @@ export default class Stomper extends Trait {
}

if (us.vel.y > them.vel.y) {
this.bounce(us, them);
this.queue(() => this.bounce(us, them));
us.sounds.add('stomp');
this.events.emit('stomp', us, them);
}
Expand Down
3 changes: 2 additions & 1 deletion public/levels/1-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
}
]
},

"cannon-2h": {
"tiles": [
{
Expand Down Expand Up @@ -399,7 +400,7 @@
},
{
"name": "cannon",
"pos": [104, 112]
"pos": [96, 112]
}
]
}
2 changes: 1 addition & 1 deletion public/sprites/overworld.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@
]
}
]
}
}

0 comments on commit 3067565

Please sign in to comment.