phaser/v3/src/boot/TickerLoop.js

159 lines
3.1 KiB
JavaScript
Raw Normal View History

var NOOP = require('../utils/NOOP');
2017-04-26 17:13:56 +01:00
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
var TickerLoop = function (game, framerate)
{
this.game = game;
this.raf = new RequestAnimationFrame();
this.started = false;
this.running = false;
this.lastUpdate = 0;
this.gap = 1 / (framerate || 60);
2017-04-26 17:13:56 +01: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 17:13:56 +01:00
this.lagThreshold = 500;
this.adjustedLag = 33;
this.useRAF = true;
};
TickerLoop.prototype.constructor = TickerLoop;
TickerLoop.prototype = {
toString: function ()
{
return 'time: ' + this.time + ' elapsed: ' + this.elapsed + ' overlap: ' + this.overlap;
},
start: function (useRAF, callback)
2017-04-26 17:13:56 +01:00
{
if (this.started)
{
return this;
}
this.started = true;
this.running = true;
this.startTime = Date.now();
this.lastUpdate = Date.now();
this.useRAF = useRAF;
this.callback = callback;
2017-04-26 17:13:56 +01:00
this.raf.start(this.step.bind(this), useRAF);
},
step: function (manual)
{
2017-04-26 17:13:56 +01:00
var elapsed = Date.now() - this.lastUpdate;
if (elapsed > this.lagThreshold)
{
this.startTime += elapsed - this.adjustedLag;
}
this.lastUpdate += elapsed;
var time = (this.lastUpdate - this.startTime) / 1000;
2017-04-26 17:13:56 +01:00
this.elapsed = elapsed;
var overlap = time - this.nextTime;
2017-04-26 17:13:56 +01:00
this.overlap = overlap;
2017-04-26 17:13:56 +01:00
this.time = time;
if (overlap > 0 || manual)
2017-04-26 17:13:56 +01:00
{
this.frame++;
this.nextTime += overlap + ((overlap >= this.gap) ? 0.004 : this.gap - overlap);
this.callback(elapsed);
2017-04-26 17:13:56 +01: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 17:13:56 +01: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 = TickerLoop;