animated sprites

This commit is contained in:
2023-11-03 23:10:45 -06:00
parent a7e7cd139f
commit 2e039719f6
3 changed files with 127 additions and 52 deletions

45
animation/sprite.ts Normal file
View File

@@ -0,0 +1,45 @@
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,
);
}
}