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