2017-04-26 15:03:14 +00:00
|
|
|
var GetValue = require('../../utils/object/GetValue');
|
2017-04-04 15:32:33 +00:00
|
|
|
var GetFrames = require('./GetFrames');
|
|
|
|
|
2017-05-02 15:49:48 +00:00
|
|
|
// A Frame based Animation
|
|
|
|
// This consists of a key, some default values (like the frame rate) and a bunch of Frame objects.
|
|
|
|
// The Animation Manager creates these
|
|
|
|
// Game Objects don't own an instance of these directly
|
|
|
|
// Game Objects have Animation Components, which are like playheads to global Animations (these objects)
|
|
|
|
// So multiple Game Objects can have playheads all pointing to this one Animation instance
|
|
|
|
|
2017-04-04 15:50:28 +00:00
|
|
|
var Animation = function (manager, key, config)
|
2017-04-04 15:32:33 +00:00
|
|
|
{
|
|
|
|
this.manager = manager;
|
|
|
|
|
2017-04-04 15:50:28 +00:00
|
|
|
this.key = key;
|
|
|
|
|
2017-04-12 12:53:55 +00:00
|
|
|
// A frame based animation (as opposed to a bone based animation)
|
|
|
|
this.type = 'frame';
|
|
|
|
|
2017-04-04 15:50:28 +00:00
|
|
|
// Extract all the frame data into the frames array
|
2017-04-26 15:03:14 +00:00
|
|
|
this.frames = GetFrames(manager.textureManager, GetValue(config, 'frames', []));
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// The frame rate of playback in frames per second (default 24 if duration is null)
|
2017-04-26 15:03:14 +00:00
|
|
|
this.frameRate = GetValue(config, 'framerate', null);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
// How long the animation should play for. If frameRate is set it overrides this value
|
|
|
|
// otherwise frameRate is derived from duration
|
2017-04-26 15:03:14 +00:00
|
|
|
this.duration = GetValue(config, 'duration', null);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-06 23:07:20 +00:00
|
|
|
if (this.duration === null && this.frameRate === null)
|
2017-04-04 15:32:33 +00:00
|
|
|
{
|
2017-04-12 12:14:42 +00:00
|
|
|
// No duration or frameRate given, use default frameRate of 24fps
|
2017-04-06 23:07:20 +00:00
|
|
|
this.frameRate = 24;
|
|
|
|
this.duration = this.frameRate / this.frames.length;
|
2017-04-05 00:15:53 +00:00
|
|
|
}
|
2017-04-06 23:07:20 +00:00
|
|
|
else if (this.duration && this.frameRate === null)
|
2017-04-05 00:15:53 +00:00
|
|
|
{
|
2017-04-06 23:07:20 +00:00
|
|
|
// Duration given but no frameRate, so set the frameRate based on duration
|
2017-04-05 00:15:53 +00:00
|
|
|
// I.e. 12 frames in the animation, duration = 4 (4000 ms)
|
2017-04-06 23:07:20 +00:00
|
|
|
// So frameRate is 12 / 4 = 3 fps
|
|
|
|
this.frameRate = this.frames.length / this.duration;
|
2017-04-04 15:32:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-12 12:14:42 +00:00
|
|
|
// frameRate given, derive duration from it (even if duration also specified)
|
2017-04-06 23:07:20 +00:00
|
|
|
// I.e. 15 frames in the animation, frameRate = 30 fps
|
2017-04-05 00:15:53 +00:00
|
|
|
// So duration is 15 / 30 = 0.5 (half a second)
|
2017-04-06 23:07:20 +00:00
|
|
|
this.duration = this.frames.length / this.frameRate;
|
2017-04-04 15:32:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// ms per frame (without including frame specific modifiers)
|
2017-04-06 23:07:20 +00:00
|
|
|
this.msPerFrame = 1000 / this.frameRate;
|
2017-04-05 00:15:53 +00:00
|
|
|
|
|
|
|
// Skip frames if the time lags, or always advanced anyway?
|
2017-04-26 15:03:14 +00:00
|
|
|
this.skipMissedFrames = GetValue(config, 'skipMissedFrames', true);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
|
|
|
// Delay before starting playback (in seconds)
|
2017-04-26 15:03:14 +00:00
|
|
|
this.delay = GetValue(config, 'delay', 0);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Number of times to repeat the animation (-1 for infinity)
|
2017-04-26 15:03:14 +00:00
|
|
|
this.repeat = GetValue(config, 'repeat', 0);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Delay before the repeat starts (in seconds)
|
2017-04-26 15:03:14 +00:00
|
|
|
this.repeatDelay = GetValue(config, 'repeatDelay', 0);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Should the animation yoyo? (reverse back down to the start) before repeating?
|
2017-04-26 15:03:14 +00:00
|
|
|
this.yoyo = GetValue(config, 'yoyo', false);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-10 13:38:44 +00:00
|
|
|
// Should sprite.visible = true when the animation starts to play?
|
2017-04-26 15:03:14 +00:00
|
|
|
this.showOnStart = GetValue(config, 'showOnStart', false);
|
2017-04-10 13:38:44 +00:00
|
|
|
|
|
|
|
// Should sprite.visible = false when the animation finishes?
|
2017-04-26 15:03:14 +00:00
|
|
|
this.hideOnComplete = GetValue(config, 'hideOnComplete', false);
|
2017-04-10 13:38:44 +00:00
|
|
|
|
2017-04-10 15:27:32 +00:00
|
|
|
// Callbacks
|
2017-04-26 15:03:14 +00:00
|
|
|
this.callbackScope = GetValue(config, 'callbackScope', this);
|
2017-04-10 15:27:32 +00:00
|
|
|
|
2017-04-26 15:03:14 +00:00
|
|
|
this.onStart = GetValue(config, 'onStart', false);
|
|
|
|
this.onStartParams = GetValue(config, 'onStartParams', []);
|
2017-04-10 15:27:32 +00:00
|
|
|
|
2017-04-26 15:03:14 +00:00
|
|
|
this.onRepeat = GetValue(config, 'onRepeat', false);
|
|
|
|
this.onRepeatParams = GetValue(config, 'onRepeatParams', []);
|
2017-04-10 15:27:32 +00:00
|
|
|
|
2017-04-10 16:04:24 +00:00
|
|
|
// Called for EVERY frame of the animation.
|
|
|
|
// See AnimationFrame.onUpdate for a frame specific callback.
|
2017-04-26 15:03:14 +00:00
|
|
|
this.onUpdate = GetValue(config, 'onUpdate', false);
|
|
|
|
this.onUpdateParams = GetValue(config, 'onUpdateParams', []);
|
2017-04-10 15:27:32 +00:00
|
|
|
|
2017-04-26 15:03:14 +00:00
|
|
|
this.onComplete = GetValue(config, 'onComplete', false);
|
|
|
|
this.onCompleteParams = GetValue(config, 'onCompleteParams', []);
|
2017-05-02 15:49:48 +00:00
|
|
|
|
|
|
|
// Global pause, effects all Game Objects using this Animation instance
|
|
|
|
this.paused = false;
|
|
|
|
|
|
|
|
this.manager.events.on('PAUSE_ALL_ANIMATION_EVENT', this.pause.bind(this));
|
|
|
|
this.manager.events.on('RESUME_ALL_ANIMATION_EVENT', this.resume.bind(this));
|
2017-04-04 15:32:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Animation.prototype.constructor = Animation;
|
|
|
|
|
|
|
|
Animation.prototype = {
|
|
|
|
|
2017-05-02 23:31:36 +00:00
|
|
|
load: require('./Load'),
|
|
|
|
addFrame: require('./AddFrame'),
|
|
|
|
addFrameAt: require('./AddFrameAt'),
|
|
|
|
getFrameAt: require('./GetFrameAt'),
|
|
|
|
removeFrame: require('./RemoveFrame'),
|
|
|
|
removeFrameAt: require('./RemoveFrameAt'),
|
|
|
|
updateFrameSequence: require('./UpdateFrameSequence'),
|
|
|
|
checkFrame: require('./CheckFrame'),
|
|
|
|
getFirstTick: require('./GetFirstTick'),
|
|
|
|
getNextTick: require('./GetNextTick'),
|
|
|
|
nextFrame: require('./NextFrame'),
|
|
|
|
previousFrame: require('./PreviousFrame'),
|
|
|
|
repeatAnimation: require('./RepeatAnimation'),
|
|
|
|
completeAnimation: require('./CompleteAnimation'),
|
|
|
|
setFrame: require('./SetFrame'),
|
|
|
|
toJSON: require('./ToJSON'),
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-05-02 15:49:48 +00:00
|
|
|
pause: function ()
|
|
|
|
{
|
|
|
|
this.paused = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
resume: function ()
|
|
|
|
{
|
|
|
|
this.paused = false;
|
|
|
|
},
|
|
|
|
|
2017-04-04 15:32:33 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Animation;
|