phaser/v3/src/animation/frame/Animation.js

148 lines
4.5 KiB
JavaScript
Raw Normal View History

var GetObjectValue = require('../../utils/object/GetObjectValue');
var GetFrames = require('./GetFrames');
2017-04-04 15:50:28 +00:00
var Animation = function (manager, key, config)
{
this.manager = manager;
2017-04-04 15:50:28 +00:00
this.key = key;
// 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
this.frames = GetFrames(manager.textureManager, GetObjectValue(config, 'frames', []));
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-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
this.duration = GetObjectValue(config, 'duration', null);
2017-04-05 00:15:53 +00:00
if (this.duration === null && this.framerate === null)
{
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;
}
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-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?
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)
this.repeat = GetObjectValue(config, 'repeat', 0);
2017-04-05 00:15:53 +00:00
// Delay before the repeat starts (in seconds)
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?
this.yoyo = GetObjectValue(config, 'yoyo', false);
2017-04-05 00:15:53 +00:00
// Callbacks (swap for Events?)
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 = {
load: function (component, startFrame)
{
if (startFrame >= this.frames.length)
{
startFrame = 0;
}
component.currentAnim = this;
component.currentFrame = this.frames[startFrame];
},
checkFrame: function (index)
{
return (index < this.frames.length);
},
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-05 00:15:53 +00:00
// Example data:
// timestamp = 2356.534000020474
// frameDelta = 17.632333353807383 (diff since last timestamp)
// 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-05 00:15:53 +00:00
component.accumulator = diff;
2017-04-05 00:15:53 +00:00
this.getNextTick(component);
}
else
{
// We're at the end of the animation
component.stop();
}
},
destroy: function ()
{
}
};
module.exports = Animation;