breakout/assets/js/Bricks.js

44 lines
1.2 KiB
JavaScript

class Brick {
constructor(type, column, row) {
this.type = type;
this.row = row;
this.column = column;
this.vspace = 2;
this.hspace = 2;
this.w = (32) - this.hspace;
this.h = (32) - this.vspace;
this.x = 2 + (this.w + this.hspace) * column;
this.y = 80 + (this.h + this.vspace) * row;
this.img = [];
switch (type) {
case 2: this.lives = 2; this.img = [ resources.get('b31'),resources.get('b32') ]; break;
case 3: this.lives = 3; this.img = [ resources.get('b31'),resources.get('b32'), resources.get('b33') ]; break;
default: this.lives = 1; this.img = [ resources.get('b31') ]; break;
}
}
crack() {
this.lives--;
}
update(ctx) {
if (this.lives == 0) return false;
switch (this.lives) {
case 1:
ctx.drawImage(this.img[0], this.x + 1, this.y);
break;
case 2:
ctx.drawImage(this.img[1], this.x + 1, this.y);
break;
case 3:
ctx.drawImage(this.img[2], this.x + 1, this.y);
break;
}
return true;
}
}