mr-crocs-adventures/js/retro/RetroAnimation.js

67 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2020-01-22 22:50:45 +01:00
import RetroSprite from "./RetroSprite.js";
2020-01-23 23:09:03 +01:00
import GeometryRect from "../geometry/GeometryRect.js";
2020-01-22 22:50:45 +01:00
export default class RetroAnimation extends RetroSprite
{
constructor(image, frames, scale = 1, fps = 6)
{
super(image, scale);
this.frames = frames;
this.fps = fps;
this.frameDuration = 1000 / this.fps;
this.currentFrame = 0;
this.lastUpdate = undefined;
this.frameWidth = this.canvas.width / this.frames;
this.isPlaying = false;
}
getWidth() {
return this.frameWidth;
}
2020-01-23 23:09:03 +01:00
getRect() {
return new GeometryRect(this.position.x, this.position.y, this.frameWidth, this.canvas.height);
}
2020-01-22 22:50:45 +01:00
setFootPosition(x, y) {
this.position.x = x - this.frameWidth * 0.5;
this.position.y = y - this.canvas.height;
}
play(timestamp)
{
this.isPlaying = true;
if (this.lastUpdate === undefined) {
this.lastUpdate = timestamp;
}
if (timestamp - this.lastUpdate >= this.frameDuration) {
this.currentFrame = (this.currentFrame + 1) % this.frames;
this.lastUpdate = timestamp;
}
}
2020-02-09 22:06:59 +01:00
draw(context, camera)
2020-01-22 22:50:45 +01:00
{
if (!this.isPlaying) {
this.currentFrame = 0;
}
context.drawImage(
this.canvas,
this.frameWidth * this.currentFrame,
0,
this.frameWidth,
this.canvas.height,
2020-02-09 22:06:59 +01:00
this.position.x - camera.position.x,
this.position.y - camera.position.y,
2020-01-22 22:50:45 +01:00
this.frameWidth,
this.canvas.height
);
this.isPlaying = false;
}
2024-10-20 15:08:42 +02:00
}