-
Notifications
You must be signed in to change notification settings - Fork 7
/
eventChain.js
206 lines (185 loc) · 4.6 KB
/
eventChain.js
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
/*global ig: true */
ig.module(
'game.system.eventChain'
)
.requires(
'impact.impact'
)
.defines((function(global) {
'use strict';
var mixins = {};
global.EventChain = function(context) {
var steps, stepsCopy, update;
steps = [];
// Called every frame.
update = function() {
if (!stepsCopy || !stepsCopy.length) {
stepsCopy = steps.slice(0);
}
if (steps && steps.length) {
steps[0]();
}
};
for (var name in mixins) {
update[name] = mixins[name](context, steps);
}
update.reset = function() {
var args = [1, 0].concat(stepsCopy.slice(0));
Array.prototype.splice.apply(steps, args);
};
// Returned from this factory thing.
return update;
};
global.EventChain.mixin = function(name, fn) {
mixins[name] = fn;
};
global.EventChain.mixin('then', function(context, steps) {
return function(doThis) {
steps.push(function() {
// Update.
doThis.call(context);
// End.
steps.shift();
});
return this;
};
});
global.EventChain.mixin('thenUntil', function(context, steps) {
return function(doThis, predicate) {
steps.push(function() {
if( !predicate.call(context) ){
doThis.call(context);
}
if( predicate.call(context) ){
steps.shift();
var func = steps[0];
if( func ){
func();
}
}
});
return this;
};
});
global.EventChain.mixin('waitUntil', function(context, steps) {
return function(predicate) {
var doThis = function() {
return;
};
steps.push(function() {
if( !predicate.call(context) ){
doThis.call(context);
}
if( predicate.call(context) ){
steps.shift();
var func = steps[0];
if( func ){
func();
}
}
});
return this;
};
});
global.EventChain.mixin('wait', function(context, steps) {
return function(secs) {
var decrement = secs;
steps.push(function() {
// Update.
if (decrement) {
decrement -= ig.system.tick;
}
// End.
if (decrement <= 0) {
steps.shift();
// Necessary because of repeat.
decrement = secs;
}
});
return this;
};
});
global.EventChain.mixin('during', function(context, steps) {
return function(doThis) {
if (!steps) {
throw new Error('during only works with previous step!');
}
var func = steps[steps.length - 1];
steps[steps.length - 1] = function() {
doThis.call(context);
func();
};
return this;
};
});
global.EventChain.mixin('repeat', function(context, steps) {
return function(times) {
var stepsCopy, originalTimes;
times = times || Infinity;
originalTimes = times;
steps.push(function() {
times -= 1;
if (times > 0) {
var args = stepsCopy.slice(0);
args.unshift(1, 0);
Array.prototype.splice.apply(steps, args);
} else {
// For successive repeats.
times = originalTimes;
}
// End.
steps.shift();
});
stepsCopy = steps.slice(0);
return this;
};
});
global.EventChain.mixin('every', function(context) {
return function(sec, doThis) {
return this.during(
global.EventChain(context)
.wait(sec)
.then(doThis)
.repeat()
);
};
});
global.EventChain.mixin('orUntil', function(context, steps) {
return function(predicate) {
if (!steps) {
throw new Error('orUntil only works with previous step!');
}
var func = steps[steps.length - 1];
steps[steps.length - 1] = function() {
if (predicate.call(context)) {
steps.shift();
return;
}
func();
};
return this;
};
});
global.EventChain.mixin('waitForAnimation', function(context, steps) {
return function(animation, times) {
// If we were not given an animation, then look in context for
// a currentAnim property.
if (!times) {
times = 1;
if (typeof animation === 'number') {
times = animation;
animation = context.currentAnim;
}
if (typeof animation === 'undefined') {
animation = context.currentAnim;
}
}
steps.push(function() {
if (animation.loopCount >= times) {
steps.shift();
}
});
return this;
};
});
}).bind(this, this));