ballz.js/rect.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-04-06 17:19:26 +00:00
class Rect {
constructor(game, x, y, number) {
this.game = game;
this.x = x;
this.y = y;
this.number = number;
this.color = calculateColor(number);
this.lastHits = {};
}
draw() {
this.game.ctx.fillStyle = this.color;
this.game.ctx.fillRect(this.x + 2, this.y + 2, this.game.rectSize - 4, this.game.rectSize - 4);
this.game.ctx.fillStyle = '#000';
this.game.ctx.font = '20px Roboto, Arial, sans-serif';
this.game.ctx.textAlign = 'center';
this.game.ctx.textBaseline = 'middle';
this.game.ctx.fillText(this.number, this.x + this.game.rectSize / 2, this.y + this.game.rectSize / 2);
}
hit(ball) {
if (this.lastHits[ball.id] && Date.now() - this.lastHits[ball.id] < 100) {
return;
}
this.number--;
this.color = calculateColor(this.number);
sounds.hitBlock.play();
if (this.number <= 0) {
this.game.rects.splice(this.game.rects.findIndex(rect => rect === this), 1);
}
this.lastHits[ball.id] = Date.now();
}
}