Skip to content

Commit

Permalink
Move sounds from Trait to Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
pomle committed Mar 9, 2020
1 parent 71754ba commit 21224a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions public/js/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class Trait {
this.NAME = name;

this.events = new EventEmitter();
this.sounds = new Set();
this.tasks = [];
}

Expand All @@ -36,14 +35,6 @@ export class Trait {

}

playSounds(audioBoard, audioContext) {
this.sounds.forEach(name => {
audioBoard.playAudio(name, audioContext);
});

this.sounds.clear();
}

update() {

}
Expand All @@ -52,6 +43,7 @@ export class Trait {
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 Expand Up @@ -89,12 +81,21 @@ export default class Entity {
});
}

playSounds(audioBoard, audioContext) {
this.sounds.forEach(name => {
audioBoard.playAudio(name, audioContext);
});

this.sounds.clear();
}

update(gameContext, level) {
this.traits.forEach(trait => {
trait.update(this, gameContext, level);
trait.playSounds(this.audio, gameContext.audioContext);
});

this.playSounds(this.audio, gameContext.audioContext);

this.lifetime += gameContext.deltaTime;
}
}
2 changes: 1 addition & 1 deletion public/js/traits/Jump.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class Jump extends Trait {
update(entity, {deltaTime}) {
if (this.requestTime > 0) {
if (this.ready > 0) {
this.sounds.add('jump');
entity.sounds.add('jump');
this.engageTime = this.duration;
this.requestTime = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion public/js/traits/Stomper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class Stomper extends Trait {

if (us.vel.y > them.vel.y) {
this.bounce(us, them);
this.sounds.add('stomp');
us.sounds.add('stomp');
this.events.emit('stomp', us, them);
}
}
Expand Down

0 comments on commit 21224a6

Please sign in to comment.