2017-06-28 21:19:41 +00:00
|
|
|
var Class = require('../utils/Class');
|
2017-06-28 16:17:54 +00:00
|
|
|
var TimerEvent = require('./TimerEvent');
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
var Clock = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function Clock (state)
|
|
|
|
{
|
|
|
|
this.state = state;
|
|
|
|
|
|
|
|
this.now = Date.now();
|
|
|
|
|
|
|
|
this.paused = false;
|
|
|
|
|
|
|
|
this._active = [];
|
|
|
|
this._pendingInsertion = [];
|
|
|
|
this._pendingRemoval = [];
|
|
|
|
},
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
addEvent: function (config)
|
|
|
|
{
|
|
|
|
var event = new TimerEvent(config);
|
|
|
|
|
|
|
|
this._pendingInsertion.push(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
|
|
|
delayedCall: function (delay, callback, args, callbackScope)
|
|
|
|
{
|
|
|
|
return this.addEvent({ delay: delay, callback: callback, args: args, callbackScope: callbackScope });
|
|
|
|
},
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
clearPendingEvents: function ()
|
|
|
|
{
|
|
|
|
this._pendingInsertion = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
removeAllEvents: function ()
|
2017-06-28 16:17:54 +00:00
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
this._pendingRemoval = this._pendingRemoval.concat(this._active);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
begin: function ()
|
|
|
|
{
|
|
|
|
var toRemove = this._pendingRemoval.length;
|
|
|
|
var toInsert = this._pendingInsertion.length;
|
|
|
|
|
|
|
|
if (toRemove === 0 && toInsert === 0)
|
|
|
|
{
|
|
|
|
// Quick bail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i;
|
|
|
|
|
2017-06-28 16:17:54 +00:00
|
|
|
// Delete old events
|
2017-06-28 21:19:41 +00:00
|
|
|
for (i = 0; i < toRemove; i++)
|
2017-06-28 16:17:54 +00:00
|
|
|
{
|
2017-06-28 16:39:40 +00:00
|
|
|
var event = this._pendingRemoval[i];
|
|
|
|
|
|
|
|
var index = this._active.indexOf(event);
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
{
|
|
|
|
this._active.splice(index, 1);
|
|
|
|
}
|
2017-06-28 16:39:40 +00:00
|
|
|
|
|
|
|
// Pool them?
|
|
|
|
event.destroy();
|
2017-06-28 16:17:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
for (i = 0; i < toInsert; i++)
|
|
|
|
{
|
|
|
|
var event = this._pendingInsertion[i];
|
|
|
|
|
|
|
|
event.elapsed = event.startAt * event.timeScale;
|
|
|
|
|
|
|
|
this._active.push(event);
|
|
|
|
}
|
|
|
|
|
2017-06-28 16:17:54 +00:00
|
|
|
// Move pending events to the active list
|
2017-06-28 21:19:41 +00:00
|
|
|
// this._active = this._active.concat(this._pendingInsertion.splice(0));
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
// Clear the lists
|
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function (time, delta)
|
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
this.now = time;
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
if (this.paused)
|
2017-06-28 16:17:54 +00:00
|
|
|
{
|
2017-06-28 21:19:41 +00:00
|
|
|
return;
|
2017-06-28 16:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < this._active.length; i++)
|
|
|
|
{
|
|
|
|
var event = this._active[i];
|
|
|
|
|
2017-06-28 16:39:40 +00:00
|
|
|
if (event.paused)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
// Use delta time to increase elapsed.
|
|
|
|
// Avoids needing to adjust for pause / resume.
|
|
|
|
// Automatically smoothed by TimeStep class.
|
|
|
|
// In testing accurate to +- 1ms!
|
|
|
|
event.elapsed += delta * event.timeScale;
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
if (event.elapsed >= event.delay)
|
|
|
|
{
|
|
|
|
var remainder = event.elapsed - event.delay;
|
|
|
|
|
|
|
|
// Limit it, in case it's checked in the callback
|
|
|
|
event.elapsed = event.delay;
|
|
|
|
|
|
|
|
// Process the event
|
2017-06-28 21:19:41 +00:00
|
|
|
if (!event.hasDispatched && event.callback)
|
2017-06-28 16:39:40 +00:00
|
|
|
{
|
|
|
|
event.hasDispatched = true;
|
|
|
|
event.callback.apply(event.callbackScope, event.args);
|
|
|
|
}
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
if (event.loop || event.repeatCount > 0)
|
|
|
|
{
|
|
|
|
event.repeatCount--;
|
|
|
|
|
|
|
|
event.elapsed = remainder;
|
2017-06-28 16:39:40 +00:00
|
|
|
event.hasDispatched = false;
|
2017-06-28 16:17:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._pendingRemoval.push(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 21:19:41 +00:00
|
|
|
},
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this._active.length; i++)
|
|
|
|
{
|
|
|
|
this._active[i].destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._active.length = 0;
|
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
|
|
|
}
|
2017-06-28 16:17:54 +00:00
|
|
|
|
2017-06-28 21:19:41 +00:00
|
|
|
});
|
2017-06-28 16:17:54 +00:00
|
|
|
|
|
|
|
module.exports = Clock;
|