UNPKG

itay-game-loop

Version:

A game loop written in typescript.

72 lines (71 loc) 3.4 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "chai", "./MainLoop"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const MainLoop_1 = require("./MainLoop"); function default_1(suite) { suite.describe("Integration", suite => { suite.test("The loop is ticking.", (test) => __awaiter(this, void 0, void 0, function* () { test.arrange(); let loop = new MainLoop_1.MainLoop(); let hasTicked = false; loop.setTick(() => { hasTicked = true; }); test.act(); loop.start(); yield new Promise(resolve => setTimeout(resolve, 20)); test.assert(); chai_1.expect(hasTicked).to.be.true; })); suite.test("Cancel.", (test) => __awaiter(this, void 0, void 0, function* () { test.arrange(); let loop = new MainLoop_1.MainLoop(); loop.timing.minMillisecondsPerTick = 1; let tickCount = 0; loop.setTick(() => { tickCount++; if (tickCount === 2) { loop.stop(); } }); test.act(); loop.start(); yield new Promise(resolve => setTimeout(resolve, 10)); test.assert(); chai_1.expect(tickCount).to.equal(2); })); suite.describe("Timming.", (suite) => __awaiter(this, void 0, void 0, function* () { suite.test("Min milliseconds per tick. This might fall if cpu is busy.", (test) => __awaiter(this, void 0, void 0, function* () { test.arrange(); let loop = new MainLoop_1.MainLoop(); loop.timing.minMillisecondsPerTick = 10; let tickCount = 0; loop.setTick(() => { tickCount++; }); test.act(); loop.start(); let testMillisec = 150; yield new Promise(resolve => setTimeout(resolve, testMillisec)); let expected = testMillisec / loop.timing.minMillisecondsPerTick; test.assert(); chai_1.expect(expected - tickCount).to.be.lessThan(5); })); })); }); } exports.default = default_1; });