escalator-web/src/collectable.js

55 lines
1.2 KiB
JavaScript

class Collectable extends AnimatedSprite {
constructor(x, y) {
super(Collectable.texture, 14);
this.size = 0.1;
this.width = this.height = this.size;
this.x = x;
this.y = y;
this.collected = false;
this.maxTimer = 30;
}
update(time = 0.02) {
if (!this.collected) {
super.update(time);
} else {
this.textTimer += time * 50;
}
}
collect() {
this.collected = true;
this.textTimer = 0;
}
drawOn(canvas, context) {
if (!this.collected) {
super.drawOn(canvas, context);
} else if (this.textTimer < this.maxTimer) {
this.drawTextOn(canvas, context);
}
}
alphaFromTimer() {
return 1 - this.textTimer / this.maxTimer;
}
drawTextOn(canvas, context) {
context.font = "15px Dimbo";
context.fillStyle = 'rgba(0, 200, 0, ' + this.alphaFromTimer() + ')';
let width = context.measureText("+1").width;
context.fillText("+1",
this.x * canvas.width - width / 2,
(1 - this.y) * canvas.height - this.textTimer
);
}
}
Collectable.texture = new Image();
Collectable.texture.src = "img/collectable.png";