-
Notifications
You must be signed in to change notification settings - Fork 2
/
task.ts
301 lines (256 loc) · 7.2 KB
/
task.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/// <reference path="utils.ts" />
enum TaskState {
Scheduled,
Running,
Finished,
}
/** Object that represents a continuous process.
* onTick() method is invoked at every frame.
*/
class Task {
/** List to which this task belongs (assigned by TaskList). */
parent: TaskList = null;
/** True if the task is running. */
state: TaskState = TaskState.Scheduled;
/** Lifetime.
* This task automatically terminates itself after
* the time specified here passes. */
lifetime: number = Infinity;
/** Start time. */
startTime: number = Infinity;
/** Fired when this task is stopped. */
stopped: Signal;
constructor() {
this.stopped = new Signal(this);
}
toString() {
return '<Task: time='+this.getTime()+'>';
}
/** Returns the number of seconds elapsed since
* this task has started. */
getTime() {
return (getTime() - this.startTime);
}
/** Returns true if the task is scheduled but not yet running. */
isScheduled() {
return (this.state == TaskState.Scheduled);
}
/** Returns true if the task is running. */
isRunning() {
return (this.state == TaskState.Running);
}
/** Invoked when the task is started. */
onStart() {
if (this.state == TaskState.Scheduled) {
this.state = TaskState.Running;
this.startTime = getTime();
}
}
/** Invoked when the task is stopped. */
onStop() {
}
/** Invoked at every frame while the task is running. */
onTick() {
if (this.lifetime <= this.getTime()) {
this.stop();
}
}
/** Terminates the task. */
stop() {
if (this.state == TaskState.Running) {
this.state = TaskState.Finished;
this.stopped.fire();
}
}
/** Schedules another task right after this task.
* @param next Next Task.
*/
chain(next: Task, signal: Signal=null): Task {
switch (this.state) {
case TaskState.Scheduled:
case TaskState.Running:
signal = (signal !== null)? signal : this.stopped;
signal.subscribe(() => {
if (this.parent !== null) {
this.parent.add(next);
}
});
break;
case TaskState.Finished:
// Start immediately if this task has already finished.
if (this.parent !== null) {
this.parent.add(next);
}
}
return next;
}
}
/** Task that plays a sound.
*/
class SoundTask extends Task {
/** Sound object to play. */
sound: HTMLAudioElement;
/** Start time of the sound. */
soundStart: number;
/** End time of the sound. */
soundEnd: number;
/** Constructor.
* @param sound Sound object to play.
* @param soundStart Start time of the sound.
*/
constructor(sound: HTMLAudioElement, soundStart=MP3_GAP, soundEnd=0) {
super();
this.sound = sound;
this.soundStart = soundStart;
this.soundEnd = soundEnd;
}
/** Invoked when the task is started. */
onStart() {
super.onStart();
// Start playing.
this.sound.currentTime = this.soundStart;
this.sound.play();
}
/** Invoked when the task is stopped. */
onStop() {
// Stop playing.
this.sound.pause();
super.onStop();
}
/** Invoked at every frame while the task is running. */
onTick() {
super.onTick();
// Check if the playing is finished.
if (0 < this.soundEnd && this.soundEnd <= this.sound.currentTime) {
this.stop();
} else if (this.sound.ended) {
this.stop();
}
}
}
/** Abstract list of Tasks
*/
interface TaskList {
/** Add a new Task to the list.
* @param task Task to add.
*/
add(task: Task): void;
/** Remove an existing Task from the list.
* @param task Task to remove.
*/
remove(task: Task): void;
}
/** List of Tasks that run parallely.
*/
class ParallelTaskList extends Task implements TaskList {
/** List of current tasks. */
tasks: Task[] = [];
/** If true, this task is stopped when the list becomes empty. */
stopWhenEmpty: boolean = true;
toString() {
return ('<ParalellTaskList: tasks='+this.tasks+'>');
}
/** Empties the task list. */
onStart() {
super.onStart();
this.tasks = [];
}
/** Invoked at every frame. Update the current tasks. */
onTick() {
for (let task of this.tasks) {
if (task.isScheduled()) {
task.onStart();
}
if (task.isRunning()) {
task.onTick();
}
}
// Remove the finished tasks from the list.
let removed = this.tasks.filter((task: Task) => { return !task.isRunning(); });
for (let task of removed) {
this.remove(task);
}
// Terminates itself then the list is empty.
if (this.stopWhenEmpty && this.tasks.length == 0) {
this.stop();
}
}
/** Add a new Task to the list.
* @param task Task to add.
*/
add(task: Task) {
task.parent = this;
this.tasks.push(task);
}
/** Remove an existing Task from the list.
* @param task Task to remove.
*/
remove(task: Task) {
if (!task.isScheduled()) {
task.onStop();
}
removeElement(this.tasks, task);
}
}
/** List of Tasks that run sequentially.
*/
class SequentialTaskList extends Task implements TaskList {
/** List of current tasks. */
tasks: Task[] = null;
/** If true, this task is stopped when the list becomes empty. */
stopWhenEmpty: boolean = true;
/** Constructor.
* @param tasks List of tasks. (optional)
*/
constructor(tasks: Task[]=null) {
super();
this.tasks = tasks;
}
/** Empties the task list. */
onStart() {
super.onStart();
if (this.tasks === null) {
this.tasks = [];
}
}
/** Add a new Task to the list.
* @param task Task to add.
*/
add(task: Task) {
task.parent = this;
this.tasks.push(task);
}
/** Remove an existing Task from the list.
* @param task Task to remove.
*/
remove(task: Task) {
removeElement(this.tasks, task);
}
/** Returns the task that is currently running
* (or null if empty) */
getCurrentTask() {
return (0 < this.tasks.length)? this.tasks[0] : null;
}
/** Invoked at every frame. Update the current tasks. */
onTick() {
let task:Task = null;
while (true) {
task = this.getCurrentTask();
if (task === null) break;
// Starts the next task.
if (task.isScheduled()) {
task.onStart();
}
if (task.isRunning()) {
task.onTick();
break;
}
// Finishes the current task.
this.remove(task);
}
// Terminates itself then the list is empty.
if (this.stopWhenEmpty && task === null) {
this.stop();
}
}
}