-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync with published result of episode 17.
- Loading branch information
Showing
12 changed files
with
53 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,4 +105,4 @@ | |
] | ||
} | ||
] | ||
} | ||
} |