Created
December 17, 2016 13:16
-
-
Save niklaskorz/2ef312693977e02d3fb4751b28f7d435 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***/ 121: | |
/***/ function(module, exports, __webpack_require__) { | |
"use strict"; | |
var GameLoop = (function () { | |
function GameLoop() { | |
this.simulationStep = 1000 / 60; | |
this.frameDelta = 0; | |
this.lastFrameTimeMs = 0; | |
this.fps = 60; | |
this.lastFpsUpdate = 0; | |
this.framesThisSecond = 0; | |
this.numUpdateSteps = 0; | |
this.minFrameDelay = 0; | |
this.running = false; | |
this.started = false; | |
this.panic = false; | |
this.begin = function (time, delta) { }; | |
this.end = function (fps, panic) { }; | |
this.update = function (delta) { }; | |
this.render = function (interpolation) { }; | |
this.animate = this.animate.bind(this); | |
} | |
Object.defineProperty(GameLoop.prototype, "maxAllowedFps", { | |
get: function () { | |
return 1000 / this.minFrameDelay; | |
}, | |
set: function (fps) { | |
if (fps == null) { | |
fps = Infinity; | |
} | |
if (fps === 0) { | |
this.stop(); | |
} | |
else { | |
this.minFrameDelay = 1000 / fps; | |
} | |
}, | |
enumerable: true, | |
configurable: true | |
}); | |
GameLoop.prototype.resetFrameDelta = function () { | |
var frameDelta = this.frameDelta; | |
this.frameDelta = 0; | |
return frameDelta; | |
}; | |
GameLoop.prototype.start = function () { | |
var _this = this; | |
if (this.started) { | |
return; | |
} | |
this.started = true; | |
this.rafHandle = requestAnimationFrame(function (timestamp) { | |
_this.render(1); | |
_this.running = true; | |
_this.lastFrameTimeMs = timestamp; | |
_this.lastFpsUpdate = timestamp; | |
_this.framesThisSecond = 0; | |
_this.rafHandle = requestAnimationFrame(_this.animate); | |
}); | |
}; | |
GameLoop.prototype.stop = function () { | |
this.running = false; | |
this.started = false; | |
cancelAnimationFrame(this.rafHandle); | |
}; | |
GameLoop.prototype.animate = function (time) { | |
this.rafHandle = requestAnimationFrame(this.animate); | |
if (time < this.lastFrameTimeMs + this.minFrameDelay) { | |
return; | |
} | |
this.frameDelta += time - this.lastFrameTimeMs; | |
this.lastFrameTimeMs = time; | |
this.begin(time, this.frameDelta); | |
if (time > this.lastFpsUpdate + 1000) { | |
this.fps = 0.25 * this.framesThisSecond + 0.75 * this.fps; | |
this.lastFpsUpdate = time; | |
this.framesThisSecond = 0; | |
} | |
++this.framesThisSecond; | |
this.numUpdateSteps = 0; | |
while (this.frameDelta >= this.simulationStep) { | |
this.update(this.simulationStep); | |
this.frameDelta -= this.simulationStep; | |
if (++this.numUpdateSteps >= 240) { | |
this.panic = true; | |
break; | |
} | |
} | |
this.render(this.frameDelta / this.simulationStep); | |
this.end(this.fps, this.panic); | |
this.panic = false; | |
}; | |
return GameLoop; | |
}()); | |
/* harmony default export */ exports["a"] = GameLoop; | |
/***/ }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GameLoop { | |
simulationStep = 1000 / 60; | |
frameDelta = 0; | |
lastFrameTimeMs = 0; | |
fps = 60; | |
lastFpsUpdate = 0; | |
framesThisSecond = 0; | |
numUpdateSteps = 0; | |
minFrameDelay = 0; | |
running = false; | |
started = false; | |
panic = false; | |
rafHandle: number; | |
begin = (time: number, delta: number) => {}; | |
end = (fps: number, panic: boolean) => {}; | |
update = (delta: number) => {}; | |
render = (interpolation: number) => {}; | |
constructor() { | |
this.animate = this.animate.bind(this); | |
} | |
get maxAllowedFps() { | |
return 1000 / this.minFrameDelay; | |
} | |
set maxAllowedFps(fps) { | |
if (fps == null) { | |
fps = Infinity; | |
} | |
if (fps === 0) { | |
this.stop(); | |
} else { | |
this.minFrameDelay = 1000 / fps; | |
} | |
} | |
resetFrameDelta() { | |
const frameDelta = this.frameDelta; | |
this.frameDelta = 0; | |
return frameDelta; | |
} | |
start() { | |
if (this.started) { | |
return; | |
} | |
this.started = true; | |
this.rafHandle = requestAnimationFrame((timestamp) => { | |
this.render(1); | |
this.running = true; | |
this.lastFrameTimeMs = timestamp; | |
this.lastFpsUpdate = timestamp; | |
this.framesThisSecond = 0; | |
this.rafHandle = requestAnimationFrame(this.animate); | |
}) | |
} | |
stop() { | |
this.running = false; | |
this.started = false; | |
cancelAnimationFrame(this.rafHandle); | |
} | |
animate(time: number) { | |
this.rafHandle = requestAnimationFrame(this.animate); | |
if (time < this.lastFrameTimeMs + this.minFrameDelay) { | |
return; | |
} | |
this.frameDelta += time - this.lastFrameTimeMs; | |
this.lastFrameTimeMs = time; | |
this.begin(time, this.frameDelta); | |
if (time > this.lastFpsUpdate + 1000) { | |
this.fps = 0.25 * this.framesThisSecond + 0.75 * this.fps; | |
this.lastFpsUpdate = time; | |
this.framesThisSecond = 0; | |
} | |
++this.framesThisSecond; | |
this.numUpdateSteps = 0; | |
while (this.frameDelta >= this.simulationStep) { | |
this.update(this.simulationStep); | |
this.frameDelta -= this.simulationStep; | |
if (++this.numUpdateSteps >= 240) { | |
this.panic = true; | |
break; | |
} | |
} | |
this.render(this.frameDelta / this.simulationStep); | |
this.end(this.fps, this.panic); | |
this.panic = false; | |
} | |
} | |
export default GameLoop; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment