ts-game-engine
Version:
Simple WebGL game/render engine written in TypeScript
64 lines (63 loc) • 2.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const GraphicsSystem_1 = require("./Systems/Graphics/GraphicsSystem");
const Materials_1 = require("./Materials");
const Texture2D_1 = require("./Textures/Texture2D");
const Settings_1 = require("./Settings");
const InputSystem_1 = require("./Systems/Input/InputSystem");
class Game {
constructor(canvas) {
this.Update = (now) => {
if (!this.shouldUpdate)
return;
if (this.scene === undefined)
return;
if (this.initialTime < 0)
this.initialTime = now;
let elapsedMilliseconds = now - this.initialTime;
let deltaTime = (elapsedMilliseconds - this.previousFrameElapsedMilliseconds) / 1000;
this.previousFrameElapsedMilliseconds = elapsedMilliseconds;
this.elapsedSeconds = elapsedMilliseconds / 1000;
this.inputSystem.Update();
this.scene.Update(deltaTime);
this.graphicsSystem.Context.clear(WebGL2RenderingContext.COLOR_BUFFER_BIT | WebGL2RenderingContext.DEPTH_BUFFER_BIT);
this.scene.Render();
requestAnimationFrame(this.Update);
};
this.graphicsSystem = new GraphicsSystem_1.GraphicsSystem(canvas);
this.inputSystem = new InputSystem_1.InputSystem(canvas);
this.settings = new Settings_1.Settings();
this.shouldUpdate = false;
this.initialTime = -1;
this.previousFrameElapsedMilliseconds = 0;
this.elapsedSeconds = 0;
}
get GraphicsSystem() { return this.graphicsSystem; }
get InputSystem() { return this.inputSystem; }
get Settings() { return this.settings; }
get Scene() { return this.scene; }
get ElapsedSeconds() { return this.elapsedSeconds; }
LoadScene(scene) {
this.scene = scene;
this.scene.Start();
this.StartUpdating();
}
Dispose() {
this.shouldUpdate = false;
Materials_1.Shader.DisposeAll();
Texture2D_1.Texture2D.DisposeAll();
this.graphicsSystem.Dispose();
}
Resize(width, height) {
var _a;
this.graphicsSystem.Resize(width, height);
(_a = this.scene) === null || _a === void 0 ? void 0 : _a.Resize(width, height);
}
StartUpdating() {
if (this.shouldUpdate)
return;
this.shouldUpdate = true;
requestAnimationFrame(this.Update);
}
}
exports.Game = Game;