UNPKG

itay-game-loop

Version:

A game loop written in typescript.

160 lines (159 loc) 6.13 kB
define("itay-game-loop/code/FpsMonitor", ["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class FpsMonitor { constructor() { this.fps = 60; this.sampleIntervalMillisec = 500; this.timestamp = 0; this.framesThisInterval = 0; this.sampleStartTimestamp = 0; } countFrame(deltaMillisec) { this.timestamp += deltaMillisec; if (this.hasSampleTimeEnded()) { this.sampleStartTimestamp = this.timestamp; this.fps = this.framesThisInterval * (1000 / this.sampleIntervalMillisec); this.framesThisInterval = 0; } this.framesThisInterval++; } hasSampleTimeEnded() { return this.timestamp > this.sampleStartTimestamp + this.sampleIntervalMillisec; } } exports.FpsMonitor = FpsMonitor; }); define("itay-game-loop/code/TimingController", ["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class TimingController { constructor() { this._minMillisecondsPerTick = 0; this._maxMillisecondsPerTick = Number.MAX_VALUE; } get minMillisecondsPerTick() { return this._minMillisecondsPerTick; } set minMillisecondsPerTick(value) { if (value < 0) { this._minMillisecondsPerTick = 0; } else { this._minMillisecondsPerTick = value; } } get maxMillisecondsPerTick() { return this._maxMillisecondsPerTick; } set maxMillisecondsPerTick(value) { if (value <= 0) { this._maxMillisecondsPerTick = Number.MAX_VALUE; } else { this._maxMillisecondsPerTick = value; } } get minTicksPerSecond() { return 1000 / this._maxMillisecondsPerTick; } set minTicksPerSecond(value) { if (value <= 0) { this.maxMillisecondsPerTick = 0; } else { this.maxMillisecondsPerTick = 1000 / value; } } get maxTicksPerSecond() { return 1000 / this._minMillisecondsPerTick; } set maxTicksPerSecond(value) { if (value <= 0) { this.minMillisecondsPerTick = 0; } else { this.minMillisecondsPerTick = 1000 / value; } } } exports.TimingController = TimingController; }); define("itay-game-loop/code/MainLoop", ["require", "exports", "itay-game-loop/code/TimingController"], function (require, exports, TimingController_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _self = (typeof self !== "undefined" && self) || (typeof global !== "undefined" && global) || undefined; class MainLoop { constructor() { this.timing = new TimingController_1.TimingController(); this.vebrose = false; this.frameId = 0; this.lastTickTime = 0; this._isRunning = false; this.tickFunc = function () { }; this.mainLoop = time => { let deltaMillisec = time - this.lastTickTime; if (deltaMillisec < this.timing.minMillisecondsPerTick) { this.frameId = this.requestFrame(this.mainLoop, this.timing.minMillisecondsPerTick - deltaMillisec); if (this.vebrose) { console.log("skip - min: " + this.timing.minMillisecondsPerTick + " delta:" + deltaMillisec); } return; } this.frameId = this.requestFrame(this.mainLoop, this.timing.minMillisecondsPerTick); if (deltaMillisec > this.timing.maxMillisecondsPerTick) { // TODO: Trigger event. deltaMillisec = this.timing.maxMillisecondsPerTick; } this.lastTickTime = time; this.tickFunc(deltaMillisec); }; this.requestFrame = _self.requestAnimationFrame ? _self.requestAnimationFrame.bind(self) : function (callback, waitMillisec) { return _self.setTimeout(() => { callback(Date.now()); }, waitMillisec); }; this.cancelFrame = _self.requestAnimationFrame ? _self.cancelAnimationFrame.bind(self) : function (handle) { _self.clearTimeout(handle); }; } get isRunning() { return this._isRunning; } start() { if (this._isRunning) { return; } this._isRunning = true; this.lastTickTime = 0; this.frameId = this.requestFrame(time => { this.lastTickTime = time - this.timing.minMillisecondsPerTick; this.mainLoop(time); }, 0); } stop() { if (!this._isRunning) { return; } this._isRunning = false; this.cancelFrame(this.frameId); } setTick(tickFunc) { this.tickFunc = tickFunc || function () { }; } } exports.MainLoop = MainLoop; }); define("itay-game-loop", ["require", "exports", "itay-game-loop/code/FpsMonitor", "itay-game-loop/code/MainLoop", "itay-game-loop/code/TimingController"], function (require, exports, FpsMonitor_1, MainLoop_1, TimingController_2) { "use strict"; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); __export(FpsMonitor_1); __export(MainLoop_1); __export(TimingController_2); });