Skip to content

Commit 00755cd

Browse files
committed
waitForAnimation works with currentAnim.
If it's given a context it will look for a currentAnim property on that and use it.
1 parent 88b411a commit 00755cd

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

eventChain.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,21 @@ ig.module(
106106
return this;
107107
};
108108

109-
update.waitForAnimation = function(animation) {
109+
update.waitForAnimation = function(animation, times) {
110+
// If we were not given an animation, then look in context for
111+
// a currentAnim property.
112+
if (!times) {
113+
times = 1;
114+
if (typeof animation === 'number') {
115+
times = animation;
116+
animation = context.currentAnim;
117+
}
118+
if (typeof animation === 'undefined') {
119+
animation = context.currentAnim;
120+
}
121+
}
110122
steps.push(function() {
111-
if (animation.loopCount > 0) {
123+
if (animation.loopCount >= times) {
112124
steps.shift();
113125
}
114126
});

test/eventChainSpec.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,82 @@ describe('eventChain', function() {
253253
chain();
254254
assert(done);
255255
});
256+
257+
it('can wait until an animation loops n times', function(){
258+
var fakeAnimation = {
259+
loopCount: 0
260+
};
261+
var done = false;
262+
chain
263+
.waitForAnimation(fakeAnimation, 2)
264+
.then(function() {
265+
done = true;
266+
});
267+
chain();
268+
assert(!done);
269+
chain();
270+
assert(!done);
271+
chain();
272+
assert(!done);
273+
fakeAnimation.loopCount = 1;
274+
chain();
275+
chain();
276+
assert(!done);
277+
fakeAnimation.loopCount = 2;
278+
chain();
279+
chain();
280+
assert(done);
281+
});
282+
283+
it('can wait for current animation if none specified', function() {
284+
var done = false;
285+
var Fake = function() {
286+
// This is supposed to mimic an Entity's currentAnim property.
287+
this.currentAnim = {
288+
loopCount: 0
289+
};
290+
this.chain = EventChain(this)
291+
.waitForAnimation()
292+
.then(function() {
293+
done = true;
294+
});
295+
};
296+
var f = new Fake();
297+
f.chain();
298+
assert(!done);
299+
f.chain();
300+
assert(!done);
301+
f.currentAnim.loopCount = 1;
302+
f.chain();
303+
f.chain();
304+
assert(done);
305+
});
306+
307+
it('can wait for current animation to run n times if none specified', function() {
308+
var done = false;
309+
var Fake = function() {
310+
// This is supposed to mimic an Entity's currentAnim property.
311+
this.currentAnim = {
312+
loopCount: 0
313+
};
314+
this.chain = EventChain(this)
315+
.waitForAnimation(2)
316+
.then(function() {
317+
done = true;
318+
});
319+
};
320+
var f = new Fake();
321+
f.chain();
322+
assert(!done);
323+
f.chain();
324+
assert(!done);
325+
f.currentAnim.loopCount = 1;
326+
f.chain();
327+
f.chain();
328+
assert(!done);
329+
f.currentAnim.loopCount = 2;
330+
f.chain();
331+
f.chain();
332+
assert(done);
333+
});
256334
});

0 commit comments

Comments
 (0)