blob: 9d49ec7969a90cd9ea0a30c39e49afa7663ea965 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import { ctx } from "./consts";
import Drawable from "./Drawable";
export default class Anime extends Drawable {
i = 0;
j = 0;
cb: any | null;
constructor(public drawable: Drawable, public frames: Array<string | HTMLImageElement>) {
super(drawable._x, drawable._y)
this.width = drawable.width
this.height = drawable.height
// if (frames.length === 0) debugger
}
draw(): boolean {
if (typeof this.frames[0] === "string") {
ctx.fillStyle = (this.frames as string[])[this.i];
ctx.fillRect(this.drawable.x, this.drawable.y, this.drawable.width, this.drawable.height);
} else {
try {
const f = (this.frames as HTMLImageElement[])[this.i]
if (f)
ctx.drawImage(f, this.drawable.x, this.drawable.y)
} catch (e) { debugger }
}
if (++this.j === 5) { this.i++; this.j = 0; }
if (this.i === this.frames.length && this.cb) this.cb()
return this.i !== this.frames.length;
}
}
|