-
Notifications
You must be signed in to change notification settings - Fork 3
/
Timer.ts
102 lines (92 loc) · 2.16 KB
/
Timer.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Created by martelly on 5/5/2014.
*/
class Timer
{
private sequences:Sequence[];
fps:number;
constructor(fps:number)
{
this.fps = fps;
this.sequences = []
}
addSequence(sequence:Sequence):void
{
this.sequences[sequence.name] = sequence;
}
removeSequence(sequence:Sequence):void
{
delete this.sequences[sequence.name];
}
update()
{
for (var key in this.sequences)
{
this.sequences[key].nextFrame();
}
}
}
class Sequence
{
game:GameObject;
actions:SequenceAction[];
name:string;
currentIndex:number;
constructor(game:GameObject, name:string, actions:SequenceAction[])
{
this.name = name;
this.game = game;
this.actions = actions;
this.currentIndex = 0;
}
static makeSequence(game:GameObject, name:string, actions:SequenceAction[]):void
{
var seq:Sequence = new Sequence(game, name, actions);
game.timer.addSequence(seq);
}
nextFrame():void
{
if (this.currentIndex >= this.actions.length)
{
this.game.timer.removeSequence(this);
} else
{
// run the current action
this.actions[this.currentIndex].nextFrame();
// check if it's time to move on to next action
if (this.actions[this.currentIndex].isComplete())
{
console.log("Next action index");
this.currentIndex += 1;
}
}
}
}
/**
*
*/
class SequenceAction
{
action:Function;
currentFrame:number;
totalFrameCount:number;
private completed:boolean;
constructor(game:GameObject, delay:number, action:Function) {
this.action = action;
this.currentFrame = 0;
this.totalFrameCount = delay*game.timer.fps;
this.completed = false;
}
isComplete():boolean
{
return this.completed;
}
nextFrame():void {
if (!this.completed && this.currentFrame > this.totalFrameCount)
{
this.completed = true;
this.action();
}
this.currentFrame += 1;
}
}