animated sprites
This commit is contained in:
45
animation/sprite.ts
Normal file
45
animation/sprite.ts
Normal 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,
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user