1
0
Fork 0
tsgames/src/games/text-dungeon/figure.ts

53 lines
1.4 KiB
TypeScript

import type { TextDisplay } from "@common/display/text";
import { Drawable } from "./drawable";
import type { IRegion, ISpriteDefinition } from "./types";
export class Figure extends Drawable {
private frames: IRegion[];
private animationCounter = 0;
private animationPeriod;
constructor(
display: TextDisplay,
public x: number,
public y: number,
definition: ISpriteDefinition,
public frame: number = 0,
) {
super(display);
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;
}
this.display.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)];
}
}