itay-game-loop
Version:
A game loop written in typescript.
1 lines • 3.19 kB
JavaScript
define("itay-game-loop/code/FpsMonitor",["require","exports"],function(i,e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});e.FpsMonitor=class{constructor(){this.fps=60,this.sampleIntervalMillisec=500,this.timestamp=0,this.framesThisInterval=0,this.sampleStartTimestamp=0}countFrame(i){this.timestamp+=i,this.hasSampleTimeEnded()&&(this.sampleStartTimestamp=this.timestamp,this.fps=this.framesThisInterval*(1e3/this.sampleIntervalMillisec),this.framesThisInterval=0),this.framesThisInterval++}hasSampleTimeEnded(){return this.timestamp>this.sampleStartTimestamp+this.sampleIntervalMillisec}}}),define("itay-game-loop/code/TimingController",["require","exports"],function(i,e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});e.TimingController=class{constructor(){this._minMillisecondsPerTick=0,this._maxMillisecondsPerTick=Number.MAX_VALUE}get minMillisecondsPerTick(){return this._minMillisecondsPerTick}set minMillisecondsPerTick(i){this._minMillisecondsPerTick=i<0?0:i}get maxMillisecondsPerTick(){return this._maxMillisecondsPerTick}set maxMillisecondsPerTick(i){this._maxMillisecondsPerTick=i<=0?Number.MAX_VALUE:i}get minTicksPerSecond(){return 1e3/this._maxMillisecondsPerTick}set minTicksPerSecond(i){this.maxMillisecondsPerTick=i<=0?0:1e3/i}get maxTicksPerSecond(){return 1e3/this._minMillisecondsPerTick}set maxTicksPerSecond(i){this.minMillisecondsPerTick=i<=0?0:1e3/i}}}),define("itay-game-loop/code/MainLoop",["require","exports","itay-game-loop/code/TimingController"],function(i,e,s){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var t="undefined"!=typeof self&&self||"undefined"!=typeof global&&global||void 0;e.MainLoop=class{constructor(){this.timing=new s.TimingController,this.vebrose=!1,this.frameId=0,this.lastTickTime=0,this._isRunning=!1,this.tickFunc=function(){},this.mainLoop=(i=>{let e=i-this.lastTickTime;if(e<this.timing.minMillisecondsPerTick)return this.frameId=this.requestFrame(this.mainLoop,this.timing.minMillisecondsPerTick-e),void(this.vebrose&&console.log("skip - min: "+this.timing.minMillisecondsPerTick+" delta:"+e));this.frameId=this.requestFrame(this.mainLoop,this.timing.minMillisecondsPerTick),e>this.timing.maxMillisecondsPerTick&&(e=this.timing.maxMillisecondsPerTick),this.lastTickTime=i,this.tickFunc(e)}),this.requestFrame=t.requestAnimationFrame?t.requestAnimationFrame.bind(self):function(i,e){return t.setTimeout(()=>{i(Date.now())},e)},this.cancelFrame=t.requestAnimationFrame?t.cancelAnimationFrame.bind(self):function(i){t.clearTimeout(i)}}get isRunning(){return this._isRunning}start(){this._isRunning||(this._isRunning=!0,this.lastTickTime=0,this.frameId=this.requestFrame(i=>{this.lastTickTime=i-this.timing.minMillisecondsPerTick,this.mainLoop(i)},0))}stop(){this._isRunning&&(this._isRunning=!1,this.cancelFrame(this.frameId))}setTick(i){this.tickFunc=i||function(){}}}}),define("itay-game-loop",["require","exports","itay-game-loop/code/FpsMonitor","itay-game-loop/code/MainLoop","itay-game-loop/code/TimingController"],function(i,e,s,t,n){"use strict";function r(i){for(var s in i)e.hasOwnProperty(s)||(e[s]=i[s])}Object.defineProperty(e,"__esModule",{value:!0}),r(s),r(t),r(n)});