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, public timing = 1, public 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, ); } }