-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsequence.ts
138 lines (117 loc) · 3.61 KB
/
sequence.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
namespace story {
let stateStack: StoryState[];
export class StoryState {
queue: (() => void)[];
running: boolean;
lock: boolean;
protected activeTasks: Task[];
constructor() {
this.activeTasks = [];
this.queue = [];
this.running = false;
}
trackTask(task: Task) {
this.activeTasks.push(task);
}
reset() {
this.activeTasks = [];
this.running = false;
}
shouldAdvance() {
return !this.activeTasks.some(task => !task.isDone());
}
cancelByKey(key: string) {
for (const task of this.activeTasks) {
if (task.key === key && task.cancel) {
task.cancel();
return;
}
}
}
clear() {
this.queue = [];
for (const task of this.activeTasks) {
if (task.cancel)
task.cancel();
}
this.reset();
}
clearFinishedTasks() {
if (this.activeTasks.some(task => task.isDone())) {
this.activeTasks = this.activeTasks.filter(task => !task.isDone());
}
}
}
//% blockId=story_queue_story_part
//% block="queue story part"
//% group="Sequence"
//% handlerStatement=1
//% deprecated=1
export function queueStoryPart(cb: () => void) {
init();
stateStack[stateStack.length - 1].queue.push(cb);
}
//% blockId=story_clear_story_parts
//% block="clear queued story parts"
//% group="Scene"
//% deprecated=1
export function clearQueuedStoryParts() {
init();
stateStack[stateStack.length - 1].clear();
}
export function _trackTask(task: Task) {
init();
const state = stateStack && stateStack[stateStack.length - 1];
if (state) {
state.trackTask(task);
}
}
export function _cancelTask(key: string) {
const state = stateStack && stateStack[stateStack.length - 1];
if (state) {
state.cancelByKey(key);
}
}
export function _isInQueueStoryPart() {
if (stateStack && stateStack.length) {
const state = stateStack[stateStack.length - 1]
return state.lock;
}
return false;
}
function init() {
if (stateStack) return;
stateStack = [new StoryState()];
let lock = false;
game.addScenePushHandler(function () {
stateStack.push(new StoryState());
});
game.addScenePopHandler(function() {
stateStack.pop();
if (stateStack.length === 0) {
stateStack.push(new StoryState());
}
});
game.onUpdate(function() {
const state = stateStack[stateStack.length - 1];
state.clearFinishedTasks();
if (state.lock) return;
if (state.queue.length) {
if (state.shouldAdvance() || !state.running) {
if (state.running) {
state.queue.shift();
state.reset();
}
if (state.queue.length) {
state.running = true;
control.runInParallel(function () {
state.lock = true;
state.queue[0]();
state.lock = false;
});
}
}
}
});
}
}