52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { setRegion } from "./display";
|
|
import { Drawable } from "./drawable";
|
|
import type { IRegion, ISpriteDefinition } from "./types";
|
|
|
|
export class Figure extends Drawable {
|
|
private frames: IRegion[];
|
|
private animationCounter = 0;
|
|
private animationPeriod;
|
|
|
|
constructor(
|
|
public x: number,
|
|
public y: number,
|
|
definition: ISpriteDefinition,
|
|
public frame: number = 0,
|
|
) {
|
|
super();
|
|
this.x = x | 0;
|
|
this.y = y | 0;
|
|
this.frames = definition.frames;
|
|
this.animationCounter = 0;
|
|
this.animationPeriod = definition.animationPeriod ?? Number.POSITIVE_INFINITY;
|
|
}
|
|
|
|
override doDraw() {
|
|
this.animationCounter++;
|
|
if (this.animationCounter >= this.animationPeriod) {
|
|
this.animationCounter = 0;
|
|
this.frame = (this.frame + 1) % this.numFrames;
|
|
}
|
|
|
|
setRegion(this.x, this.y, this.width, this.height, this.image);
|
|
}
|
|
|
|
get width() {
|
|
const frame = this.frames[(this.frame % this.numFrames)];
|
|
const line = frame[0];
|
|
return line?.length ?? 0;
|
|
}
|
|
|
|
get height() {
|
|
const frame = this.frames[(this.frame % this.numFrames)];
|
|
return frame.length;
|
|
}
|
|
|
|
get numFrames() {
|
|
return this.frames.length;
|
|
}
|
|
|
|
get image() {
|
|
return this.frames[(this.frame % this.numFrames)];
|
|
}
|
|
} |