2017-04-04 15:32:33 +00:00
|
|
|
var GetObjectValue = require('../../utils/object/GetObjectValue');
|
|
|
|
var GetFrames = require('./GetFrames');
|
|
|
|
|
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-04 15:32:33 +00:00
|
|
|
// frames: [
|
|
|
|
// { key: textureKey, frame: textureFrame },
|
|
|
|
// { key: textureKey, frame: textureFrame, duration: float },
|
|
|
|
// { key: textureKey, frame: textureFrame, onUpdate: function }
|
|
|
|
// ],
|
|
|
|
// framerate: integer,
|
|
|
|
// duration: float (seconds, optional, ignored if framerate is set),
|
|
|
|
// skipMissedFrames: boolean,
|
|
|
|
// delay: integer
|
|
|
|
// repeat: -1 = forever, otherwise integer
|
|
|
|
// repeatDelay: integer
|
|
|
|
// yoyo: boolean,
|
|
|
|
// onStart: function
|
|
|
|
// onRepeat: function
|
|
|
|
// onComplete: function,
|
|
|
|
|
2017-04-04 15:50:28 +00:00
|
|
|
// Extract all the frame data into the frames array
|
2017-04-04 22:59:37 +00:00
|
|
|
this.frames = GetFrames(manager.textureManager, GetObjectValue(config, 'frames', []));
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Scale the time (make it go faster / slower) >>> move to Animation Component?
|
|
|
|
this.timeScale = 1;
|
|
|
|
|
|
|
|
// The frame rate of playback in frames per second (default 24 if duration is null)
|
|
|
|
this.framerate = GetObjectValue(config, 'framerate', null);
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-05 00:15:53 +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-04 15:32:33 +00:00
|
|
|
this.duration = GetObjectValue(config, 'duration', null);
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
if (this.duration === null && this.framerate === null)
|
2017-04-04 15:32:33 +00:00
|
|
|
{
|
2017-04-05 00:15:53 +00:00
|
|
|
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;
|
2017-04-04 15:32:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-05 00:15:53 +00:00
|
|
|
// No duration, so derive from the framerate
|
|
|
|
// 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-04-05 00:15:53 +00:00
|
|
|
// ms per frame (without including frame specific modifiers)
|
|
|
|
this.msPerFrame = 1000 / this.framerate;
|
|
|
|
|
|
|
|
// Skip frames if the time lags, or always advanced anyway?
|
2017-04-04 15:32:33 +00:00
|
|
|
this.skipMissedFrames = GetObjectValue(config, 'skipMissedFrames', true);
|
|
|
|
|
|
|
|
// Delay before starting playback (in seconds)
|
|
|
|
this.delay = GetObjectValue(config, 'delay', 0);
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Number of times to repeat the animation (-1 for infinity)
|
2017-04-04 15:32:33 +00:00
|
|
|
this.repeat = GetObjectValue(config, 'repeat', 0);
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Delay before the repeat starts (in seconds)
|
2017-04-04 15:32:33 +00:00
|
|
|
this.repeatDelay = GetObjectValue(config, 'repeatDelay', 0);
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Should the animation yoyo? (reverse back down to the start) before repeating?
|
2017-04-04 15:32:33 +00:00
|
|
|
this.yoyo = GetObjectValue(config, 'yoyo', false);
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
// Callbacks (swap for Events?)
|
2017-04-04 15:32:33 +00:00
|
|
|
this.onStart = GetObjectValue(config, 'onStart', false);
|
|
|
|
this.onRepeat = GetObjectValue(config, 'onRepeat', false);
|
|
|
|
this.onComplete = GetObjectValue(config, 'onComplete', false);
|
|
|
|
this.onStop = GetObjectValue(config, 'onStop', false);
|
|
|
|
};
|
|
|
|
|
|
|
|
Animation.prototype.constructor = Animation;
|
|
|
|
|
|
|
|
Animation.prototype = {
|
|
|
|
|
2017-04-04 22:59:37 +00:00
|
|
|
load: function (component, startFrame)
|
2017-04-04 15:32:33 +00:00
|
|
|
{
|
2017-04-04 22:59:37 +00:00
|
|
|
if (startFrame >= this.frames.length)
|
|
|
|
{
|
|
|
|
startFrame = 0;
|
|
|
|
}
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-04 22:59:37 +00:00
|
|
|
component.currentAnim = this;
|
|
|
|
component.currentFrame = this.frames[startFrame];
|
2017-04-04 15:32:33 +00:00
|
|
|
},
|
|
|
|
|
2017-04-04 22:59:37 +00:00
|
|
|
checkFrame: function (index)
|
2017-04-04 15:32:33 +00:00
|
|
|
{
|
2017-04-04 22:59:37 +00:00
|
|
|
return (index < this.frames.length);
|
2017-04-04 15:32:33 +00:00
|
|
|
},
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
getNextTick: function (component)
|
|
|
|
{
|
|
|
|
// When is the next update due?
|
|
|
|
var frame = component.currentFrame;
|
|
|
|
|
|
|
|
component.nextTick = this.msPerFrame + frame.duration;
|
|
|
|
},
|
|
|
|
|
|
|
|
setFrame: function (component)
|
2017-04-04 15:32:33 +00:00
|
|
|
{
|
2017-04-05 00:15:53 +00:00
|
|
|
// Example data:
|
|
|
|
// timestamp = 2356.534000020474
|
|
|
|
// frameDelta = 17.632333353807383 (diff since last timestamp)
|
|
|
|
|
2017-04-04 22:59:37 +00:00
|
|
|
// Work out which frame should be set next on the child, and set it
|
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
var frame = component.currentFrame;
|
|
|
|
|
|
|
|
var diff = component.accumulator - component.nextTick;
|
|
|
|
|
|
|
|
if (frame.nextFrame)
|
|
|
|
{
|
|
|
|
component.currentFrame = frame.nextFrame;
|
|
|
|
|
|
|
|
component.updateFrame();
|
2017-04-04 22:59:37 +00:00
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
component.accumulator = diff;
|
2017-04-04 15:32:33 +00:00
|
|
|
|
2017-04-05 00:15:53 +00:00
|
|
|
this.getNextTick(component);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We're at the end of the animation
|
|
|
|
component.stop();
|
|
|
|
}
|
2017-04-04 15:32:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Animation;
|