Updated to use TweenBuilder and modified update loop.

This commit is contained in:
Richard Davey 2017-05-17 02:44:17 +01:00
parent fd09dba200
commit bef2602c03
2 changed files with 150 additions and 10 deletions

View file

@ -194,3 +194,114 @@ var TweenBuilder = function (manager, config)
};
module.exports = TweenBuilder;
/*
The following are all the same
var tween = this.tweens.add({
targets: player,
x: 200,
duration: 2000,
ease: 'Power1',
yoyo: true
});
var tween = this.tweens.add({
targets: player,
props: {
x: 200
}
duration: 2000,
ease: 'Power1',
yoyo: true
});
var tween = this.tweens.add({
targets: player,
x: { value: 200, duration: 2000, ease: 'Power1', yoyo: true }
});
var tween = this.tweens.add({
targets: player,
props: {
x: { value: 200, duration: 2000, ease: 'Power1', yoyo: true }
}
});
// Chained property tweens:
// Each tween uses the same duration and ease because they've been 'globally' defined, except the middle one,
// which uses its own duration as it overrides the global one
var tween = this.tweens.add({
targets: player,
x: [ { value: 200 }, { value: 300, duration: 50 }, { value: 400 } ],
duration: 2000,
ease: 'Power1',
yoyo: true
});
// Multiple property tweens:
var tween = this.tweens.add({
targets: player,
x: { value: 400, duration: 2000, ease: 'Power1' },
y: { value: 300, duration: 1000, ease: 'Sine' }
});
var tween = this.tweens.add({
targets: player,
props: {
x: { value: 400, duration: 2000, ease: 'Power1' },
y: { value: 300, duration: 1000, ease: 'Sine' }
}
});
// Multiple Targets + Multiple property tweens:
var tween = this.tweens.add({
targets: [ alien1, alien2, alien3, alienBoss ],
props: {
x: { value: 400, duration: 2000 },
y: { value: 300, duration: 1000 }
},
ease: 'Sine'
});
// Multiple Targets + Multiple properties + Multi-state Property tweens:
var tween = this.tweens.add({
targets: [ alien1, alien2, alien3, alienBoss ],
props: {
x: [ { value: 200, duration: 100 }, { value: 300, duration: 50 }, { value: 400 } ],
y: { value: 300, duration: 1000 }
},
ease: 'Sine'
});
// Multi-value Tween Property with static values
var tween = this.tweens.add({
targets: [ alien1, alien2, alien3, alienBoss ],
props: {
x: [ 200, 300, 400 ],
y: [ '+100', '-100', '+100' ]
},
duration: 1000,
ease: 'Sine'
});
// Timeline concept
var tween = this.tweens.add({
targets: player,
timeline: [
{ x: 400 },
{ y: 400 },
{ x: 100 },
{ y: 100 }
],
duration: 1000,
ease: 'Sine'
});
*/

View file

@ -1,6 +1,6 @@
var EventDispatcher = require('../events/EventDispatcher');
var Tween = require('./Tween');
var TweenBuilder = require('./TweenBuilder');
var TweenManager = function (state)
{
@ -12,8 +12,10 @@ var TweenManager = function (state)
*/
this.events = new EventDispatcher(); // should use State event dispatcher?
this.pending = [];
this.active = [];
this._add = [];
this._pending = [];
this._active = [];
this._destroy = [];
};
TweenManager.prototype.constructor = TweenManager;
@ -27,9 +29,21 @@ TweenManager.prototype = {
add: function (config)
{
var tween = new Tween(this, config);
var tweens = TweenBuilder(this, config);
this.pending.push(tween);
if (tweens.length === 1)
{
return tweens[0];
}
else
{
return tweens;
}
},
queue: function (tween)
{
this._add.push(tween);
return tween;
},
@ -46,27 +60,42 @@ TweenManager.prototype = {
update: function (timestamp, delta)
{
var list = this.pending;
var list = this._add;
var i;
var tween;
// Process the pending list first
// Process the addition list first
// This stops callbacks and out of sync events from populating the active array mid-way during the update
if (list.length)
{
for (i = 0; i < list.length; i++)
{
list[i].init(timestamp, delta);
tween = list[i];
// Return true if the Tween should be started right away, otherwise false
if (tween.init())
{
tween.start(timestamp);
this._active.push(tween);
}
else
{
this._pending.push(tween);
}
}
list.length = 0;
}
// Process active tweens
list = this.active;
list = this._active;
for (i = 0; i < list.length; i++)
{
list[i].update(timestamp, delta);
tween = list[i];
tween.update(timestamp, delta);
}
},