2017-05-17 01:46:58 +00:00
|
|
|
var Tween = function (manager, target, key, value)
|
2017-05-09 19:24:39 +00:00
|
|
|
{
|
|
|
|
this.manager = manager;
|
2017-05-09 19:36:29 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.target = target;
|
|
|
|
|
|
|
|
this.key = key;
|
|
|
|
|
|
|
|
// A function to call when starting the tween, to populate the 'start' and 'end' values with
|
|
|
|
this.value = value;
|
|
|
|
|
|
|
|
this.start;
|
|
|
|
this.current;
|
|
|
|
this.end;
|
|
|
|
|
|
|
|
this.ease;
|
|
|
|
this.duration = 1000;
|
|
|
|
this.yoyo = false;
|
|
|
|
this.repeat = 0;
|
|
|
|
this.loop = false;
|
|
|
|
this.delay = 0;
|
|
|
|
this.onCompleteDelay = 0;
|
|
|
|
this.elasticity = 0;
|
|
|
|
|
|
|
|
// this.startAt
|
|
|
|
|
|
|
|
this.progress = 0;
|
|
|
|
this.startTime = 0;
|
|
|
|
this.elapsed = 0;
|
|
|
|
|
|
|
|
// 0 = forward, 1 = reverse
|
|
|
|
this.direction = 0;
|
|
|
|
|
|
|
|
this.useFrames = false;
|
|
|
|
this.paused = false;
|
|
|
|
this.running = false;
|
|
|
|
this.pending = true;
|
2017-05-10 03:30:00 +00:00
|
|
|
|
2017-05-10 16:10:21 +00:00
|
|
|
// Callbacks
|
2017-05-10 03:30:00 +00:00
|
|
|
|
2017-05-09 19:36:29 +00:00
|
|
|
this.onStart;
|
|
|
|
this.onStartScope;
|
|
|
|
this.onStartParams;
|
|
|
|
|
|
|
|
this.onUpdate;
|
|
|
|
this.onUpdateScope;
|
|
|
|
this.onUpdateParams;
|
|
|
|
|
|
|
|
this.onRepeat;
|
|
|
|
this.onRepeatScope;
|
|
|
|
this.onRepeatParams;
|
|
|
|
|
|
|
|
this.onComplete;
|
|
|
|
this.onCompleteScope;
|
|
|
|
this.onCompleteParams;
|
|
|
|
|
|
|
|
this.callbackScope;
|
2017-05-09 19:24:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tween.prototype.constructor = Tween;
|
|
|
|
|
|
|
|
Tween.prototype = {
|
2017-05-09 19:36:29 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
init: function ()
|
2017-05-11 16:19:18 +00:00
|
|
|
{
|
2017-05-17 01:46:58 +00:00
|
|
|
console.log('Tween init', (!this.paused));
|
2017-05-11 16:19:18 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
return (!this.paused);
|
2017-05-15 23:37:13 +00:00
|
|
|
},
|
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
start: function (timestep)
|
2017-05-15 23:37:13 +00:00
|
|
|
{
|
2017-05-17 01:46:58 +00:00
|
|
|
console.log('Tween started');
|
2017-05-15 23:37:13 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.startTime = timestep;
|
2017-05-15 23:37:13 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.start = this.target[this.key];
|
|
|
|
this.current = this.start;
|
|
|
|
this.end = this.value();
|
2017-05-10 01:25:46 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.paused = false;
|
|
|
|
this.running = true;
|
2017-05-10 01:25:46 +00:00
|
|
|
},
|
2017-05-11 16:19:18 +00:00
|
|
|
|
2017-05-10 01:25:46 +00:00
|
|
|
update: function (timestep, delta)
|
|
|
|
{
|
2017-05-10 03:30:00 +00:00
|
|
|
if (!this.running)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.elapsed += delta;
|
|
|
|
|
|
|
|
if (this.elapsed > this.duration)
|
2017-05-10 03:30:00 +00:00
|
|
|
{
|
2017-05-17 01:46:58 +00:00
|
|
|
this.elapsed = this.duration;
|
2017-05-10 03:30:00 +00:00
|
|
|
}
|
2017-05-10 01:25:46 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.progress = this.elapsed / this.duration;
|
2017-05-10 01:25:46 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
var p = this.ease(this.progress);
|
2017-05-11 16:19:18 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.current = this.start + ((this.end - this.start) * p);
|
2017-05-09 23:43:28 +00:00
|
|
|
|
2017-05-17 01:46:58 +00:00
|
|
|
this.target[this.key] = this.current;
|
|
|
|
|
|
|
|
if (this.progress === 1)
|
2017-05-09 23:43:28 +00:00
|
|
|
{
|
2017-05-17 01:46:58 +00:00
|
|
|
this.running = false;
|
2017-05-09 23:43:28 +00:00
|
|
|
}
|
2017-05-09 19:36:29 +00:00
|
|
|
},
|
|
|
|
|
2017-05-09 23:43:28 +00:00
|
|
|
eventCallback: function (type, callback, params, scope)
|
|
|
|
{
|
|
|
|
var types = [ 'onStart', 'onUpdate', 'onRepeat', 'onComplete' ];
|
|
|
|
|
|
|
|
if (types.indexOf(type) !== -1)
|
|
|
|
{
|
|
|
|
this[type] = callback;
|
|
|
|
this[type + 'Params'] = params;
|
|
|
|
this[type + 'Scope'] = scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
timeScale: function ()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-09 19:24:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Tween;
|