-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.ts
38 lines (31 loc) · 967 Bytes
/
Animation.ts
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
33
34
35
36
37
38
import Frame from './Frame';
export default class Animation {
private id: string;
private duration: number;
private frames: Frame[] = [];
private currentFrame: number = 0;
private remainTime: number = 0;
constructor(id: string, duration: number) {
this.id = id;
this.duration = duration;
}
public addFrame(frame: Frame): void {
this.frames.push(frame);
this.remainTime = this.duration / this.frames.length;
}
public getCurrentFrame(): Frame {
return this.frames[this.currentFrame];
}
public onUpdate(dt: number): boolean {
this.remainTime -= dt;
while (this.remainTime <= 0) {
this.remainTime += (this.duration / this.frames.length);
this.currentFrame++;
if (this.currentFrame >= this.frames.length) {
this.currentFrame = 0;
}
return true;
}
return false;
}
}