Skip to content

Commit

Permalink
[IMP] add bonus icons for each player
Browse files Browse the repository at this point in the history
Added updateBonusIcons method to Field class
Added bonusActivated event listener

Close sdegueldre#15
  • Loading branch information
kolumb committed Mar 14, 2021
1 parent a5e8dc0 commit fa23b44
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/client/game/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class Game {
this.on('playerScored', this.playerScored.bind(this));
this.on('bonusSpawned', this.field.spawnBonus.bind(this.field));
this.on('bonusCollected', this.field.setBonuses.bind(this.field);
this.on('bonusActivated', this.field.updateBonusIcons.bind(this.field));
this.on('collision', ({pos: {x, y}, vel: {x: vx, y: vy}}) => {
this.particleGroups.push(new CollisionParticles({
x, y,
Expand Down
19 changes: 19 additions & 0 deletions src/client/game/objects/Field.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ export default class Field extends BaseField {
this.bonuses.forEach(b => this.graphics.removeChild(b.graphics));
this.bonuses = bonusesPaddles.bonuses.map(b => new DoubleBall(b.x, b.y));
this.bonuses.forEach(b => this.graphics.addChild(b.graphics));
this.updateBonusIcons(bonusesPaddles.players);
}

updateBonusIcons(players){
const bonusIconRadius = 10;
const iconMargin = 5;
const computeIconOffset = i => 150 + i * (bonusIconRadius * 2 + iconMargin) + bonusIconRadius * 1.3;

this.players.forEach((player, playerNumber) => {
player.bonusIcons.forEach(b => this.graphics.removeChild(b.graphics));
const direction = playerNumber % 2 ? 1 : -1;
player.bonusIcons = players[playerNumber].bonuses
.map((_, i) => {
const x = this.w / 2 + direction * computeIconOffset(i);
const y = 50 + bonusIconRadius;
return new DoubleBall(x, y, bonusIconRadius);
});
player.bonusIcons.forEach(b => this.graphics.addChild(b.graphics));
});
}

shake({x, y}){
Expand Down
1 change: 1 addition & 0 deletions src/client/game/objects/Paddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class Paddle extends BasePaddle {
this.graphics.drawRect(-this.w / 2, -this.h / 2, this.w, this.h);
this.graphics.endFill();
Object.assign(this, options);
this.bonusIcons = [];
}

set x(value){
Expand Down
1 change: 1 addition & 0 deletions src/common/game/objects/BasePaddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = class BasePaddle {
const bonus = this.bonuses.shift();
if(bonus){
bonus.activate(ball, field);
field.emit('bonusActivated');
}
}
};
3 changes: 3 additions & 0 deletions src/server/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ module.exports = class Room {
players: this.field.players,
});
});
this.field.on('bonusActivated', () => {
this.broadcast('bonusActivated', this.field.players);
});
this.tick(this.field);
}

Expand Down

0 comments on commit fa23b44

Please sign in to comment.