43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
class Sprite {
|
|
constructor(spriteSheet) {
|
|
this.spriteSheet = spriteSheet;
|
|
}
|
|
|
|
drawOn(canvas, context) {
|
|
context.drawImage(
|
|
this.spriteSheet,
|
|
0, 0, 64, 64,
|
|
(this.x - this.width / 2) * canvas.width,
|
|
(1 - this.y) * canvas.height - this.height * canvas.width / 2,
|
|
this.width,
|
|
this.height,
|
|
);
|
|
}
|
|
}
|
|
|
|
class AnimatedSprite extends Sprite {
|
|
constructor(spriteSheet, numberOfFrames) {
|
|
super(spriteSheet);
|
|
this.numberOfFrames = numberOfFrames;
|
|
this.currentFrame = 0;
|
|
}
|
|
|
|
update(time = 0.02) {
|
|
this.currentFrame++;
|
|
if (this.currentFrame >= this.numberOfFrames) {
|
|
this.currentFrame = 0;
|
|
}
|
|
}
|
|
|
|
drawOn(canvas, context) {
|
|
context.drawImage(
|
|
this.spriteSheet,
|
|
0, 64 * this.currentFrame, 64, 64,
|
|
(this.x - this.width / 2) * canvas.width,
|
|
(1 - this.y) * canvas.height - this.height * canvas.width / 2,
|
|
this.width * canvas.width,
|
|
this.height * canvas.width,
|
|
);
|
|
}
|
|
}
|