escalator-web/src/collectable.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-08-27 10:47:48 +02:00
class Collectable extends AnimatedSprite {
2018-08-24 11:49:25 +02:00
constructor(x, y) {
2018-08-27 10:47:48 +02:00
super(Collectable.texture, 14);
2018-08-24 11:49:25 +02:00
this.size = 0.1;
2018-08-27 10:47:48 +02:00
this.width = this.height = this.size;
2018-08-24 11:49:25 +02:00
this.x = x;
this.y = y;
this.collected = false;
2018-08-27 10:47:48 +02:00
this.maxTimer = 30;
2018-08-26 21:53:16 +02:00
}
update(time = 0.02) {
2018-08-27 10:47:48 +02:00
if (!this.collected) {
super.update(time);
} else {
this.textTimer += time * 50;
}
}
2018-08-26 21:53:16 +02:00
2018-08-27 10:47:48 +02:00
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);
2018-08-26 21:53:16 +02:00
}
2018-08-24 11:49:25 +02:00
}
2018-08-27 10:47:48 +02:00
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
);
}
2018-08-24 11:49:25 +02:00
}
Collectable.texture = new Image();
Collectable.texture.src = "img/collectable.png";