Skip to content

Commit 08a5b49

Browse files
committed
Split the Animation functions up to make them more manageable. Fixed bug in AddFrameAt.
1 parent 4ad8795 commit 08a5b49

18 files changed

Lines changed: 340 additions & 300 deletions

v3/src/animation/frame/AddFrame.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// config = Array of Animation config objects, like:
2+
// [
3+
// { key: 'gems', frame: 'diamond0001', [duration], [visible], [onUpdate] }
4+
// ]
5+
6+
// Add frames to the end of the animation
7+
var AddFrame = function (config)
8+
{
9+
return this.addFrameAt(this.frames.length, config);
10+
};
11+
12+
module.exports = AddFrame;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var GetFrames = require('./GetFrames');
2+
3+
// config = Array of Animation config objects, like:
4+
// [
5+
// { key: 'gems', frame: 'diamond0001', [duration], [visible], [onUpdate] }
6+
// ]
7+
8+
// Add frame/s into the animation
9+
var AddFrameAt = function (index, config)
10+
{
11+
if (index === undefined) { index = 0; }
12+
13+
var newFrames = GetFrames(this.manager.textureManager, config);
14+
15+
if (newFrames.length > 0)
16+
{
17+
if (index === 0)
18+
{
19+
this.frames = newFrames.concat(this.frames);
20+
}
21+
else if (index === this.frames.length)
22+
{
23+
this.frames = this.frames.concat(newFrames);
24+
}
25+
else
26+
{
27+
var pre = this.frames.slice(0, index);
28+
var post = this.frames.slice(index);
29+
30+
this.frames = pre.concat(newFrames, post);
31+
}
32+
33+
this.updateFrameSequence();
34+
}
35+
36+
return this;
37+
};
38+
39+
module.exports = AddFrameAt;

v3/src/animation/frame/Animation.js

Lines changed: 16 additions & 299 deletions
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,22 @@ Animation.prototype.constructor = Animation;
100100

101101
Animation.prototype = {
102102

103-
load: function (component, startFrame)
104-
{
105-
if (startFrame >= this.frames.length)
106-
{
107-
startFrame = 0;
108-
}
109-
110-
if (component.currentAnim !== this)
111-
{
112-
component.currentAnim = this;
113-
114-
component._timeScale = 1;
115-
component.frameRate = this.frameRate;
116-
component.duration = this.duration;
117-
component.msPerFrame = this.msPerFrame;
118-
component.skipMissedFrames = this.skipMissedFrames;
119-
component._delay = this.delay;
120-
component._repeat = this.repeat;
121-
component._repeatDelay = this.repeatDelay;
122-
component._yoyo = this.yoyo;
123-
component._callbackArgs[1] = this;
124-
component._updateParams = component._callbackArgs.concat(this.onUpdateParams);
125-
}
126-
127-
component.updateFrame(this.frames[startFrame]);
128-
},
103+
load: require('./Load'),
104+
addFrame: require('./AddFrame'),
105+
addFrameAt: require('./AddFrameAt'),
106+
getFrameAt: require('./GetFrameAt'),
107+
removeFrame: require('./RemoveFrame'),
108+
removeFrameAt: require('./RemoveFrameAt'),
109+
updateFrameSequence: require('./UpdateFrameSequence'),
110+
checkFrame: require('./CheckFrame'),
111+
getFirstTick: require('./GetFirstTick'),
112+
getNextTick: require('./GetNextTick'),
113+
nextFrame: require('./NextFrame'),
114+
previousFrame: require('./PreviousFrame'),
115+
repeatAnimation: require('./RepeatAnimation'),
116+
completeAnimation: require('./CompleteAnimation'),
117+
setFrame: require('./SetFrame'),
118+
toJSON: require('./ToJSON'),
129119

130120
pause: function ()
131121
{
@@ -137,279 +127,6 @@ Animation.prototype = {
137127
this.paused = false;
138128
},
139129

140-
// config = Array of Animation config objects, like:
141-
// [
142-
// { key: 'gems', frame: 'diamond0001', [duration], [visible], [onUpdate] }
143-
// ]
144-
145-
// Add frames to the end of the animation
146-
addFrame: function (config)
147-
{
148-
return this.addFrameAt(this.frames.length, config);
149-
},
150-
151-
// Add frame/s into the animation
152-
addFrameAt: function (index, config)
153-
{
154-
if (index === undefined) { index = 0; }
155-
156-
var newFrames = GetFrames(this.manager.textureManager, config);
157-
158-
if (newFrames.length === 0)
159-
{
160-
if (index === 0)
161-
{
162-
this.frames = newFrames.concat(this.frames);
163-
}
164-
else if (index === this.frames.length)
165-
{
166-
this.frames = this.frames.concat(newFrames);
167-
}
168-
else
169-
{
170-
var pre = this.frames.slice(0, index);
171-
var post = this.frames.slice(index);
172-
173-
this.frames = pre.concat(newFrames, post);
174-
}
175-
176-
this.updateFrameSequence();
177-
}
178-
179-
return this;
180-
},
181-
182-
getFrameAt: function (index)
183-
{
184-
return this.frames[index];
185-
},
186-
187-
// Remove frame if it matches the given frame
188-
removeFrame: function (frame)
189-
{
190-
var index = this.frames.indexOf(frame);
191-
192-
if (index !== -1)
193-
{
194-
this.removeFrameAt(index);
195-
}
196-
197-
return this;
198-
},
199-
200-
removeFrameAt: function (index)
201-
{
202-
this.frames.splice(index, 1);
203-
204-
this.updateFrameSequence();
205-
206-
return this;
207-
},
208-
209-
updateFrameSequence: function ()
210-
{
211-
var len = this.frames.length;
212-
var slice = 1 / (len - 1);
213-
214-
for (var i = 0; i < len; i++)
215-
{
216-
var frame = this.frames[i];
217-
218-
frame.index = i + 1;
219-
frame.isFirst = false;
220-
frame.isLast = false;
221-
frame.progress = i * slice;
222-
223-
if (i === 0)
224-
{
225-
frame.isFirst = true;
226-
frame.isLast = (len === 1);
227-
frame.prevFrame = this.frames[len - 1];
228-
frame.nextFrame = this.frames[i + 1];
229-
}
230-
else if (i === len - 1)
231-
{
232-
frame.isLast = true;
233-
frame.prevFrame = this.frames[len - 2];
234-
frame.nextFrame = this.frames[0];
235-
}
236-
else if (len > 1)
237-
{
238-
frame.prevFrame = this.frames[i - 1];
239-
frame.nextFrame = this.frames[i + 1];
240-
}
241-
}
242-
243-
return this;
244-
},
245-
246-
checkFrame: function (index)
247-
{
248-
return (index < this.frames.length);
249-
},
250-
251-
getFirstTick: function (component, includeDelay)
252-
{
253-
if (includeDelay === undefined) { includeDelay = true; }
254-
255-
// When is the first update due?
256-
component.accumulator = 0;
257-
component.nextTick = component.msPerFrame + component.currentFrame.duration;
258-
259-
if (includeDelay)
260-
{
261-
component.nextTick += (component._delay * 1000);
262-
}
263-
},
264-
265-
getNextTick: function (component)
266-
{
267-
// When is the next update due?
268-
component.accumulator -= component.nextTick;
269-
component.nextTick = component.msPerFrame + component.currentFrame.duration;
270-
},
271-
272-
nextFrame: function (component)
273-
{
274-
var frame = component.currentFrame;
275-
276-
// TODO: Add frame skip support
277-
278-
if (frame.isLast)
279-
{
280-
// We're at the end of the animation
281-
282-
// Yoyo? (happens before repeat)
283-
if (this.yoyo)
284-
{
285-
component.forward = false;
286-
287-
component.updateFrame(frame.prevFrame);
288-
289-
// Delay for the current frame
290-
this.getNextTick(component);
291-
}
292-
else if (component.repeatCounter > 0)
293-
{
294-
// Repeat (happens before complete)
295-
this.repeatAnimation(component);
296-
}
297-
else
298-
{
299-
this.completeAnimation(component);
300-
}
301-
}
302-
else
303-
{
304-
component.updateFrame(frame.nextFrame);
305-
306-
this.getNextTick(component);
307-
}
308-
},
309-
310-
previousFrame: function (component)
311-
{
312-
var frame = component.currentFrame;
313-
314-
// TODO: Add frame skip support
315-
316-
if (frame.isFirst)
317-
{
318-
// We're at the start of the animation
319-
320-
if (component.repeatCounter > 0)
321-
{
322-
// Repeat (happens before complete)
323-
this.repeatAnimation(component);
324-
}
325-
else
326-
{
327-
this.completeAnimation(component);
328-
}
329-
}
330-
else
331-
{
332-
component.updateFrame(frame.prevFrame);
333-
334-
this.getNextTick(component);
335-
}
336-
},
337-
338-
repeatAnimation: function (component)
339-
{
340-
if (component._repeatDelay > 0 && component.pendingRepeat === false)
341-
{
342-
component.pendingRepeat = true;
343-
component.accumulator -= component.nextTick;
344-
component.nextTick += (component._repeatDelay * 1000);
345-
}
346-
else
347-
{
348-
component.repeatCounter--;
349-
350-
component.forward = true;
351-
352-
component.updateFrame(component.currentFrame.nextFrame);
353-
354-
this.getNextTick(component);
355-
356-
component.pendingRepeat = false;
357-
358-
if (this.onRepeat)
359-
{
360-
this.onRepeat.apply(this.callbackScope, component._callbackArgs.concat(this.onRepeatParams));
361-
}
362-
}
363-
},
364-
365-
completeAnimation: function (component)
366-
{
367-
if (this.hideOnComplete)
368-
{
369-
component.parent.visible = false;
370-
}
371-
372-
component.stop(true);
373-
},
374-
375-
setFrame: function (component)
376-
{
377-
// Work out which frame should be set next on the child, and set it
378-
if (component.forward)
379-
{
380-
this.nextFrame(component);
381-
}
382-
else
383-
{
384-
this.previousFrame(component);
385-
}
386-
},
387-
388-
toJSON: function ()
389-
{
390-
var output = {
391-
key: this.key,
392-
type: this.type,
393-
frames: [],
394-
framerate: this.frameRate,
395-
duration: this.duration,
396-
skipMissedFrames: this.skipMissedFrames,
397-
delay: this.delay,
398-
repeat: this.repeat,
399-
repeatDelay: this.repeatDelay,
400-
yoyo: this.yoyo,
401-
showOnStart: this.showOnStart,
402-
hideOnComplete: this.hideOnComplete
403-
};
404-
405-
this.frames.forEach(function (frame)
406-
{
407-
output.frames.push(frame.toJSON());
408-
});
409-
410-
return output;
411-
},
412-
413130
destroy: function ()
414131
{
415132

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var CheckFrame = function (index)
2+
{
3+
return (index < this.frames.length);
4+
};
5+
6+
module.exports = CheckFrame;

0 commit comments

Comments
 (0)