46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
import { Vector } from "../geometry/vector.ts";
|
|
|
|
export class SpriteAnimation {
|
|
image: HTMLImageElement;
|
|
origin: Vector;
|
|
constructor(
|
|
private imageUrl: string,
|
|
private cellWidth: number,
|
|
private cellHeight: number,
|
|
private cellCountX: number,
|
|
private cellCountY: number,
|
|
private timing = 1,
|
|
private scale = 1,
|
|
) {
|
|
this.image = new Image();
|
|
this.image.src = this.imageUrl;
|
|
this.origin = new Vector();
|
|
}
|
|
|
|
private _frameCount = 0;
|
|
private get frameCount() {
|
|
return this._frameCount += this.timing;
|
|
}
|
|
|
|
getCell() {
|
|
const time = Math.floor(this.frameCount);
|
|
const x = (time % this.cellCountX) * this.cellWidth;
|
|
const y = (Math.floor(time / this.cellCountX) % this.cellCountY) *
|
|
this.cellHeight;
|
|
return { x, y };
|
|
}
|
|
|
|
draw() {
|
|
const { x, y } = this.getCell();
|
|
doodler.drawSprite(
|
|
this.image,
|
|
new Vector(x, y),
|
|
this.cellWidth,
|
|
this.cellHeight,
|
|
this.origin,
|
|
this.cellWidth * this.scale,
|
|
this.cellHeight * this.scale,
|
|
);
|
|
}
|
|
}
|