Merged Animation component.

This commit is contained in:
Richard Davey 2018-01-16 15:39:18 +00:00
parent b0f22127d2
commit 89c04fc193
22 changed files with 341 additions and 413 deletions

View file

@ -1,6 +1,4 @@
var Class = require('../../utils/Class'); var Class = require('../../utils/Class');
var Components = require('./animation/');
// Game Object Animation Controller // Game Object Animation Controller
@ -79,32 +77,350 @@ var Animation = new Class({
this._updateParams = []; this._updateParams = [];
}, },
// Gets or sets the amount of time in seconds between repeats.
// For example, if repeat is 2 and repeatDelay is 1, the animation will play initially,
// then wait for 1 second before it repeats, then play again, then wait 1 second again
// before doing its final repeat.
delay: function (value)
{
if (value === undefined)
{
return this._delay;
}
else
{
this._delay = value;
return this;
}
},
delayedPlay: function (delay, key, startFrame)
{
this.play(key, true, startFrame);
this.nextTick += (delay * 1000);
return this;
},
getCurrentKey: function ()
{
if (this.currentAnim)
{
return this.currentAnim.key;
}
},
load: function (key, startFrame)
{
if (startFrame === undefined) { startFrame = 0; }
if (this.isPlaying)
{
this.stop();
}
// Load the new animation in
this.animationManager.load(this, key, startFrame);
return this;
},
pause: function (atFrame)
{
if (!this._paused)
{
this._paused = true;
this._wasPlaying = this.isPlaying;
this.isPlaying = false;
}
if (atFrame !== undefined)
{
this.updateFrame(atFrame);
}
return this;
},
paused: function (value)
{
if (value !== undefined)
{
// Setter
if (value)
{
return this.pause();
}
else
{
return this.resume();
}
}
else
{
return this._paused;
}
},
play: function (key, ignoreIfPlaying, startFrame)
{
if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false; }
if (startFrame === undefined) { startFrame = 0; }
if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key)
{
return this;
}
this.load(key, startFrame);
var anim = this.currentAnim;
// Should give us 9,007,199,254,740,991 safe repeats
this.repeatCounter = (this._repeat === -1) ? Number.MAX_SAFE_INTEGER : this._repeat;
anim.getFirstTick(this);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
if (anim.showOnStart)
{
this.parent.visible = true;
}
if (anim.onStart)
{
anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams));
}
return this;
},
// Value between 0 and 1. How far this animation is through, ignoring repeats and yoyos.
// If the animation has a non-zero repeat defined, progress and totalProgress will be different
// because progress doesn't include any repeats or repeatDelays whereas totalProgress does.
progress: function (value)
{
if (value === undefined)
{
var p = this.currentFrame.progress;
if (!this.forward)
{
p = 1 - p;
}
return p;
}
else
{
// TODO: Set progress
return this;
}
},
remove: function (event)
{
if (event === undefined) { event = this.currentAnim; }
if (this.isPlaying && event.key === this.currentAnim.key)
{
this.stop();
var sprite = this.parent;
var frame = this.currentAnim.frames[0];
this.currentFrame = frame;
sprite.texture = frame.frame.texture;
sprite.frame = frame.frame;
}
},
// Gets or sets the number of times that the animation should repeat
// after its first iteration. For example, if repeat is 1, the animation will
// play a total of twice (the initial play plus 1 repeat).
// To repeat indefinitely, use -1. repeat should always be an integer.
repeat: function (value)
{
if (value === undefined)
{
return this._repeat;
}
else
{
this._repeat = value;
this.repeatCounter = 0;
return this;
}
},
// Gets or sets the amount of time in seconds between repeats.
// For example, if repeat is 2 and repeatDelay is 1, the animation will play initially,
// then wait for 1 second before it repeats, then play again, then wait 1 second again
// before doing its final repeat.
repeatDelay: function (value)
{
if (value === undefined)
{
return this._repeatDelay;
}
else
{
this._repeatDelay = value;
return this;
}
},
restart: function (includeDelay)
{
if (includeDelay === undefined) { includeDelay = false; }
this.currentAnim.getFirstTick(this, includeDelay);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
this._paused = false;
// Set frame
this.updateFrame(this.currentAnim.frames[0]);
return this;
},
resume: function (fromFrame)
{
if (this._paused)
{
this._paused = false;
this.isPlaying = this._wasPlaying;
}
if (fromFrame !== undefined)
{
this.updateFrame(fromFrame);
}
return this;
},
stop: function (dispatchCallbacks)
{
if (dispatchCallbacks === undefined) { dispatchCallbacks = false; }
this.isPlaying = false;
var anim = this.currentAnim;
if (dispatchCallbacks && anim.onComplete)
{
anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams));
}
return this;
},
timeScale: function (value)
{
if (value === undefined)
{
return this._timeScale;
}
else
{
this._timeScale = value;
return this;
}
},
totalFrames: function ()
{
return this.currentAnim.frames.length;
},
// Value between 0 and 1. How far this animation is through, including things like delays
// repeats, custom frame durations, etc. If the animation is set to repeat -1 it can never
// have a duration, therefore this will return -1.
totalProgres: function ()
{
// TODO
},
update: function (timestamp, delta)
{
if (!this.isPlaying || this.currentAnim.paused)
{
return;
}
this.accumulator += delta * this._timeScale;
if (this.accumulator >= this.nextTick)
{
this.currentAnim.setFrame(this);
}
},
updateFrame: function (animationFrame)
{
var sprite = this.parent;
this.currentFrame = animationFrame;
sprite.texture = animationFrame.frame.texture;
sprite.frame = animationFrame.frame;
if (this.isPlaying)
{
if (animationFrame.setAlpha)
{
sprite.alpha = animationFrame.alpha;
}
var anim = this.currentAnim;
if (anim.onUpdate)
{
anim.onUpdate.apply(anim.callbackScope, this._updateParams);
}
if (animationFrame.onUpdate)
{
animationFrame.onUpdate(sprite, animationFrame);
}
}
},
yoyo: function (value)
{
if (value === undefined)
{
return this._yoyo;
}
else
{
this._yoyo = value;
return this;
}
},
destroy: function () destroy: function ()
{ {
// TODO
}, }
delay: Components.Delay,
delayedPlay: Components.DelayedPlay,
getCurrentKey: Components.GetCurrentKey,
load: Components.Load,
pause: Components.Pause,
paused: Components.Paused,
play: Components.Play,
progress: Components.Progress,
remove: Components.Remove,
repeat: Components.Repeat,
repeatDelay: Components.RepeatDelay,
restart: Components.Restart,
resume: Components.Resume,
stop: Components.Stop,
timeScale: Components.TimeScale,
totalFrames: Components.TotalFrames,
totalProgress: Components.TotalProgress,
update: Components.Update,
updateFrame: Components.UpdateFrame,
yoyo: Components.Yoyo
}); });

View file

@ -1,20 +0,0 @@
// Gets or sets the amount of time in seconds between repeats.
// For example, if repeat is 2 and repeatDelay is 1, the animation will play initially,
// then wait for 1 second before it repeats, then play again, then wait 1 second again
// before doing its final repeat.
var Delay = function (value)
{
if (value === undefined)
{
return this._delay;
}
else
{
this._delay = value;
return this;
}
};
module.exports = Delay;

View file

@ -1,10 +0,0 @@
var DelayedPlay = function (delay, key, startFrame)
{
this.play(key, true, startFrame);
this.nextTick += (delay * 1000);
return this;
};
module.exports = DelayedPlay;

View file

@ -1,9 +0,0 @@
var GetCurrentKey = function ()
{
if (this.currentAnim)
{
return this.currentAnim.key;
}
};
module.exports = GetCurrentKey;

View file

@ -1,16 +0,0 @@
var Load = function (key, startFrame)
{
if (startFrame === undefined) { startFrame = 0; }
if (this.isPlaying)
{
this.stop();
}
// Load the new animation in
this.animationManager.load(this, key, startFrame);
return this;
};
module.exports = Load;

View file

@ -1,18 +0,0 @@
var Pause = function (atFrame)
{
if (!this._paused)
{
this._paused = true;
this._wasPlaying = this.isPlaying;
this.isPlaying = false;
}
if (atFrame !== undefined)
{
this.updateFrame(atFrame);
}
return this;
};
module.exports = Pause;

View file

@ -1,21 +0,0 @@
var Paused = function (value)
{
if (value !== undefined)
{
// Setter
if (value)
{
return this.pause();
}
else
{
return this.resume();
}
}
else
{
return this._paused;
}
};
module.exports = Paused;

View file

@ -1,37 +0,0 @@
var Play = function (key, ignoreIfPlaying, startFrame)
{
if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false; }
if (startFrame === undefined) { startFrame = 0; }
if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key)
{
return this;
}
this.load(key, startFrame);
var anim = this.currentAnim;
// Should give us 9,007,199,254,740,991 safe repeats
this.repeatCounter = (this._repeat === -1) ? Number.MAX_SAFE_INTEGER : this._repeat;
anim.getFirstTick(this);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
if (anim.showOnStart)
{
this.parent.visible = true;
}
if (anim.onStart)
{
anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams));
}
return this;
};
module.exports = Play;

View file

@ -1,25 +0,0 @@
// Value between 0 and 1. How far this animation is through, ignoring repeats and yoyos.
// If the animation has a non-zero repeat defined, progress and totalProgress will be different
// because progress doesn't include any repeats or repeatDelays whereas totalProgress does.
var Progress = function (value)
{
if (value === undefined)
{
var p = this.currentFrame.progress;
if (!this.forward)
{
p = 1 - p;
}
return p;
}
else
{
// TODO: Set progress
return this;
}
};
module.exports = Progress;

View file

@ -1,19 +0,0 @@
var Remove = function (event)
{
if (event === undefined) { event = this.currentAnim; }
if (this.isPlaying && event.key === this.currentAnim.key)
{
this.stop();
var sprite = this.parent;
var frame = this.currentAnim.frames[0];
this.currentFrame = frame;
sprite.texture = frame.frame.texture;
sprite.frame = frame.frame;
}
};
module.exports = Remove;

View file

@ -1,21 +0,0 @@
// Gets or sets the number of times that the animation should repeat
// after its first iteration. For example, if repeat is 1, the animation will
// play a total of twice (the initial play plus 1 repeat).
// To repeat indefinitely, use -1. repeat should always be an integer.
var Repeat = function (value)
{
if (value === undefined)
{
return this._repeat;
}
else
{
this._repeat = value;
this.repeatCounter = 0;
return this;
}
};
module.exports = Repeat;

View file

@ -1,20 +0,0 @@
// Gets or sets the amount of time in seconds between repeats.
// For example, if repeat is 2 and repeatDelay is 1, the animation will play initially,
// then wait for 1 second before it repeats, then play again, then wait 1 second again
// before doing its final repeat.
var RepeatDelay = function (value)
{
if (value === undefined)
{
return this._repeatDelay;
}
else
{
this._repeatDelay = value;
return this;
}
};
module.exports = RepeatDelay;

View file

@ -1,18 +0,0 @@
var Restart = function (includeDelay)
{
if (includeDelay === undefined) { includeDelay = false; }
this.currentAnim.getFirstTick(this, includeDelay);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
this._paused = false;
// Set frame
this.updateFrame(this.currentAnim.frames[0]);
return this;
};
module.exports = Restart;

View file

@ -1,17 +0,0 @@
var Resume = function (fromFrame)
{
if (this._paused)
{
this._paused = false;
this.isPlaying = this._wasPlaying;
}
if (fromFrame !== undefined)
{
this.updateFrame(fromFrame);
}
return this;
};
module.exports = Resume;

View file

@ -1,17 +0,0 @@
var Stop = function (dispatchCallbacks)
{
if (dispatchCallbacks === undefined) { dispatchCallbacks = false; }
this.isPlaying = false;
var anim = this.currentAnim;
if (dispatchCallbacks && anim.onComplete)
{
anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams));
}
return this;
};
module.exports = Stop;

View file

@ -1,15 +0,0 @@
var TimeScale = function (value)
{
if (value === undefined)
{
return this._timeScale;
}
else
{
this._timeScale = value;
return this;
}
};
module.exports = TimeScale;

View file

@ -1,6 +0,0 @@
var TotalFrames = function ()
{
return this.currentAnim.frames.length;
};
module.exports = TotalFrames;

View file

@ -1,9 +0,0 @@
// Value between 0 and 1. How far this animation is through, including things like delays
// repeats, custom frame durations, etc. If the animation is set to repeat -1 it can never
// have a duration, therefore this will return -1.
var TotalProgres = function ()
{
};
module.exports = TotalProgres;

View file

@ -1,16 +0,0 @@
var Update = function (timestamp, delta)
{
if (!this.isPlaying || this.currentAnim.paused)
{
return;
}
this.accumulator += delta * this._timeScale;
if (this.accumulator >= this.nextTick)
{
this.currentAnim.setFrame(this);
}
};
module.exports = Update;

View file

@ -1,31 +0,0 @@
var UpdateFrame = function (animationFrame)
{
var sprite = this.parent;
this.currentFrame = animationFrame;
sprite.texture = animationFrame.frame.texture;
sprite.frame = animationFrame.frame;
if (this.isPlaying)
{
if (animationFrame.setAlpha)
{
sprite.alpha = animationFrame.alpha;
}
var anim = this.currentAnim;
if (anim.onUpdate)
{
anim.onUpdate.apply(anim.callbackScope, this._updateParams);
}
if (animationFrame.onUpdate)
{
animationFrame.onUpdate(sprite, animationFrame);
}
}
};
module.exports = UpdateFrame;

View file

@ -1,15 +0,0 @@
var Yoyo = function (value)
{
if (value === undefined)
{
return this._yoyo;
}
else
{
this._yoyo = value;
return this;
}
};
module.exports = Yoyo;

View file

@ -1,28 +0,0 @@
// Phaser.Animation.Frame.Components
// Used by the GameObject Animation Controller, not the Animation itself
module.exports = {
Delay: require('./Delay'),
DelayedPlay: require('./DelayedPlay'),
GetCurrentKey: require('./GetCurrentKey'),
Load: require('./Load'),
Pause: require('./Pause'),
Paused: require('./Paused'),
Play: require('./Play'),
Progress: require('./Progress'),
Remove: require('./Remove'),
Repeat: require('./Repeat'),
RepeatDelay: require('./RepeatDelay'),
Restart: require('./Restart'),
Resume: require('./Resume'),
Stop: require('./Stop'),
TimeScale: require('./TimeScale'),
TotalFrames: require('./TotalFrames'),
TotalProgress: require('./TotalProgress'),
Update: require('./Update'),
UpdateFrame: require('./UpdateFrame'),
Yoyo: require('./Yoyo')
};