phaser/wip/Ticker.js

173 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-10-04 22:48:16 +00:00
/**
* @ignore
*/
// Currently un-used
// Currently un-used
// Currently un-used
// Currently un-used
// Currently un-used
// Currently un-used
// Currently un-used
var NOOP = require('../utils/NOOP');
2017-04-26 16:13:56 +00:00
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
var Ticker = function (game, framerate)
2017-04-26 16:13:56 +00:00
{
this.game = game;
this.raf = new RequestAnimationFrame();
this.started = false;
this.running = false;
this.lastUpdate = 0;
this.gap = 1 / (framerate || 60);
2017-04-26 16:13:56 +00:00
this.startTime = 0;
this.elapsed = 0;
this.time = 0;
this.nextTime = 0;
this.frame = 0;
this.overlap = 0;
this.fps = framerate;
this.callback = NOOP;
2017-04-26 16:13:56 +00:00
this.lagThreshold = 500;
this.adjustedLag = 33;
this.useRAF = true;
};
Ticker.prototype.constructor = Ticker;
2017-04-26 16:13:56 +00:00
Ticker.prototype = {
2017-04-26 16:13:56 +00:00
toString: function ()
{
return 'time: ' + this.time + ' elapsed: ' + this.elapsed + ' overlap: ' + this.overlap;
},
start: function (useRAF, callback)
2017-04-26 16:13:56 +00:00
{
if (this.started)
{
return this;
}
this.started = true;
this.running = true;
2017-04-28 02:15:08 +00:00
var now = window.performance.now();
this.startTime = now;
this.lastUpdate = now;
2017-04-26 16:13:56 +00:00
this.useRAF = useRAF;
this.callback = callback;
2017-04-26 16:13:56 +00:00
this.raf.start(this.step.bind(this), useRAF);
},
2017-04-28 02:15:08 +00:00
step: function (time, manual)
{
2017-04-28 02:15:08 +00:00
var elapsed = time - this.lastUpdate;
2017-04-26 16:13:56 +00:00
if (elapsed > this.lagThreshold)
{
this.startTime += elapsed - this.adjustedLag;
}
this.lastUpdate += elapsed;
2017-04-28 02:15:08 +00:00
var dt = (this.lastUpdate - this.startTime) / 1000;
2017-04-26 16:13:56 +00:00
this.elapsed = elapsed;
2017-04-28 02:15:08 +00:00
var overlap = dt - this.nextTime;
2017-04-26 16:13:56 +00:00
this.overlap = overlap;
2017-04-26 16:13:56 +00:00
2017-04-28 02:15:08 +00:00
this.time = dt;
if (overlap > 0 || manual)
2017-04-26 16:13:56 +00:00
{
this.frame++;
this.nextTime += overlap + ((overlap >= this.gap) ? 0.004 : this.gap - overlap);
this.callback(elapsed);
2017-04-26 16:13:56 +00:00
}
},
tick: function ()
{
this.step(true);
},
lagSmoothing: function (threshold, adjustedLag)
{
this.lagThreshold = threshold || (1 / 0.0000000001); //zero should be interpreted as basically unlimited
this.adjustedLag = Math.min(adjustedLag, this.lagThreshold, 0);
},
sleep: function ()
{
if (this.running)
{
this.raf.stop();
this.running = false;
}
},
wake: function (seamless)
{
if (this.running)
{
this.sleep();
}
else if (seamless)
{
this.startTime += -this.lastUpdate + (this.lastUpdate = Date.now());
}
else if (this.frame > 10)
{
this.lastUpdate = Date.now() - this.lagThreshold + 5;
}
this.raf.start(this.step.bind(this), this.useRAF);
2017-04-26 16:13:56 +00:00
this.running = true;
this.step(true);
},
setFps: function (value)
{
this.fps = value;
this.gap = 1 / (value || 60);
this.nextTime = this.time + this.gap;
this.wake();
},
getFps: function ()
{
return this.fps;
},
stop: function ()
{
this.running = false;
this.started = false;
this.raf.stop();
return this;
}
};
module.exports = Ticker;