ts-game-engine
Version:
Simple WebGL game/render engine written in TypeScript
85 lines (84 loc) • 3.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const gl_matrix_1 = require("gl-matrix");
const Entities_1 = require("./Entities");
const Constants_1 = require("./Constants");
class Scene {
constructor(game) {
this.entities = new Set();
this.meshRenderers = new Set();
this.lights = new Set();
this.pointLightsData = new Float32Array(Constants_1.MAX_LIGHTS * Constants_1.LIGHT_DATA_SIZE * Constants_1.FLOAT_SIZE);
this.game = game;
this.game.GraphicsSystem.PipelineState.FaceCulling = true;
this.game.GraphicsSystem.PipelineState.FaceCullingMode = 1029 /* Back */;
this.game.GraphicsSystem.PipelineState.DepthTest = true;
this.game.GraphicsSystem.PipelineState.DepthFunction = 515 /* LEqual */;
this.camera = new Entities_1.Camera(this, "Main Camera");
this.ClearColor = gl_matrix_1.vec4.fromValues(0, 0, 0, 0);
this.ambientLight = gl_matrix_1.vec3.create();
}
get Game() { return this.game; }
get Camera() { return this.camera; }
get ClearColor() { return this.game.GraphicsSystem.PipelineState.ClearColor; }
set ClearColor(clearColor) { this.game.GraphicsSystem.PipelineState.ClearColor = clearColor; }
get AmbientLight() { return this.ambientLight; }
set AmbientLight(ambientLight) { this.ambientLight = ambientLight; }
get PointLightsData() { return this.pointLightsData; }
get PointLightsCount() { return Math.min(this.lights.size, Constants_1.MAX_LIGHTS); }
Dispose() {
this.entities.clear();
this.meshRenderers.clear();
this.lights.clear();
}
Start() { }
Update(deltaTime) {
for (let entity of this.entities) {
entity.Update(deltaTime);
}
this.camera.Update(deltaTime);
}
Render() {
this.BuildLightsData();
for (let meshRenderer of this.meshRenderers) {
meshRenderer.Render();
}
}
Resize(width, height) {
this.camera.Resize(width, height);
}
AddEntity(entity) {
this.entities.add(entity);
if (entity instanceof Entities_1.MeshRenderer)
this.meshRenderers.add(entity);
if (entity instanceof Entities_1.Light)
this.lights.add(entity);
}
RemoveEntity(entity) {
this.entities.delete(entity);
if (entity instanceof Entities_1.MeshRenderer)
this.meshRenderers.delete(entity);
if (entity instanceof Entities_1.Light)
this.lights.delete(entity);
}
BuildLightsData() {
const stride = Constants_1.LIGHT_DATA_SIZE * Constants_1.FLOAT_SIZE;
let count = 0;
// Fill point light data into the list
for (let light of this.lights) {
let index = count * stride;
this.pointLightsData[index + 0] = light.Transform.Position[0];
this.pointLightsData[index + 1] = light.Transform.Position[1];
this.pointLightsData[index + 2] = light.Transform.Position[2];
this.pointLightsData[index + 3] = light.Intensity;
this.pointLightsData[index + 4] = light.Color[0];
this.pointLightsData[index + 5] = light.Color[1];
this.pointLightsData[index + 6] = light.Color[2];
this.pointLightsData[index + 7] = 0;
count++;
if (count >= Constants_1.MAX_LIGHTS)
break; // Render MAX_LIGHTS lights at most
}
}
}
exports.Scene = Scene;