UNPKG

tsbase

Version:

Base class libraries for TypeScript

44 lines 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GameLoop = void 0; const Timer_1 = require("./Timer"); const Observable_1 = require("../../Patterns/Observable/Observable"); const oneSecond = 1000; class GameLoop { constructor() { this.framesRendered = 0; this.gameLoopTimer = new Timer_1.Timer(); this.framerateTimer = new Timer_1.Timer(); this.Framerate = new Observable_1.Observable(); this.GameEvents = new Array(); } Start(framerate) { this.startGameLoopTimer(framerate); this.startFramerateTimer(); } Stop() { this.gameLoopTimer.Stop(); this.framerateTimer.Stop(); } startGameLoopTimer(framerate) { const interval = oneSecond / framerate; this.gameLoopTimer.Interval = interval; this.gameLoopTimer.AutoReset = true; this.gameLoopTimer.Elapsed = [ () => this.GameEvents.forEach(e => e.Publish()), () => this.framesRendered++ ]; this.gameLoopTimer.Start(); } startFramerateTimer() { this.framerateTimer.Interval = oneSecond; this.framerateTimer.AutoReset = true; this.framerateTimer.Elapsed = [ () => this.Framerate.Publish(this.framesRendered), () => this.framesRendered = 0 ]; this.framerateTimer.Start(); } } exports.GameLoop = GameLoop; //# sourceMappingURL=GameLoop.js.map