Skip to content

Commit da3496a

Browse files
committed
Added toJSON support to the Animation Manager and Animations.
1 parent 01a5423 commit da3496a

5 files changed

Lines changed: 77 additions & 10 deletions

File tree

v3/src/animation/AnimationFrame.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
var AnimationFrame = function (index, frame)
1+
var AnimationFrame = function (textureKey, textureFrame, index, frame)
22
{
3+
// The keys into the Texture Manager of the texture + frame this uses
4+
this.textureKey = textureKey;
5+
this.textureFrame = textureFrame;
6+
37
// The index of this frame within the Animation.frames array
48
this.index = index;
59

@@ -33,6 +37,16 @@ AnimationFrame.prototype.constructor = AnimationFrame;
3337

3438
AnimationFrame.prototype = {
3539

40+
toJSON: function ()
41+
{
42+
return {
43+
key: this.textureKey,
44+
frame: this.textureFrame,
45+
duration: this.duration,
46+
visible: this.visible
47+
};
48+
},
49+
3650
destroy: function ()
3751
{
3852
this.frame = undefined;

v3/src/animation/AnimationManager.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ AnimationManager.prototype = {
104104
return anim;
105105
},
106106

107+
108+
107109
get: function (key)
108110
{
109111
return this.anims.get(key);
@@ -263,8 +265,34 @@ AnimationManager.prototype = {
263265
}
264266

265267
return out;
266-
}
268+
},
269+
270+
toJSON: function (key)
271+
{
272+
if (key !== undefined)
273+
{
274+
return this.anims.get(key).toJSON();
275+
}
276+
else
277+
{
278+
var output = {
279+
anims: [],
280+
globalTimeScale: this.globalTimeScale
281+
};
282+
283+
this.anims.each(function (key, anim)
284+
{
285+
output.anims.push(anim.toJSON());
286+
});
287+
288+
return output;
289+
}
290+
},
267291

292+
destroy: function ()
293+
{
294+
// TODO
295+
}
268296
};
269297

270298
module.exports = AnimationManager;

v3/src/animation/frame/Animation.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var Animation = function (manager, key, config)
1919

2020
if (this.duration === null && this.frameRate === null)
2121
{
22+
// No duration or frameRate given, use default frameRate of 24fps
2223
this.frameRate = 24;
2324
this.duration = this.frameRate / this.frames.length;
2425
}
@@ -31,7 +32,7 @@ var Animation = function (manager, key, config)
3132
}
3233
else
3334
{
34-
// No duration, so derive from the frameRate
35+
// frameRate given, derive duration from it (even if duration also specified)
3536
// I.e. 15 frames in the animation, frameRate = 30 fps
3637
// So duration is 15 / 30 = 0.5 (half a second)
3738
this.duration = this.frames.length / this.frameRate;
@@ -252,6 +253,31 @@ Animation.prototype = {
252253
}
253254
},
254255

256+
toJSON: function ()
257+
{
258+
var output = {
259+
key: this.key,
260+
type: 'frame',
261+
frames: [],
262+
framerate: this.frameRate,
263+
duration: this.duration,
264+
skipMissedFrames: this.skipMissedFrames,
265+
delay: this.delay,
266+
repeat: this.repeat,
267+
repeatDelay: this.repeatDelay,
268+
yoyo: this.yoyo,
269+
showOnStart: this.showOnStart,
270+
hideOnComplete: this.hideOnComplete
271+
};
272+
273+
this.frames.forEach(function (frame)
274+
{
275+
output.frames.push(frame.toJSON());
276+
});
277+
278+
return output;
279+
},
280+
255281
destroy: function ()
256282
{
257283

v3/src/animation/frame/GetFrames.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ var GetFrames = function (textureManager, frames)
3030
}
3131

3232
var frame = GetObjectValue(item, 'frame', 0);
33-
var duration = GetObjectValue(item, 'duration', 0);
34-
var onUpdate = GetObjectValue(item, 'onUpdate', null);
35-
var visible = GetObjectValue(item, 'visible', null);
3633

3734
var textureFrame = textureManager.getFrame(key, frame);
3835

39-
animationFrame = new AnimationFrame(index, textureFrame);
36+
animationFrame = new AnimationFrame(key, frame, index, textureFrame);
37+
38+
animationFrame.duration = GetObjectValue(item, 'duration', 0);
39+
animationFrame.onUpdate = GetObjectValue(item, 'onUpdate', null);
4040

41-
animationFrame.duration = duration;
42-
animationFrame.onUpdate = onUpdate;
41+
var visible = GetObjectValue(item, 'visible', null);
4342

4443
if (visible !== null)
4544
{

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '3cdef7a0-1ec7-11e7-a131-f3c6d91827e1'
2+
build: '314dd020-1f74-11e7-b5ea-2d9635d37e06'
33
};
44
module.exports = CHECKSUM;

0 commit comments

Comments
 (0)