r3-legacy/src/game-lib-system.js

177 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-01-12 04:44:01 +01:00
/**
* System takes care of updating all the entities (based on their component data)
* @param apiSystem GameLib.API.System
* @param domElement
* @param domStats
* @constructor
*/
GameLib.System = function(
apiSystem,
domElement,
domStats
) {
GameLib.API.System.call(
this,
apiSystem.id,
apiSystem.name,
apiSystem.systemType,
apiSystem.entityManager,
apiSystem.parentEntity
);
if (GameLib.Utils.UndefinedOrNull(domElement)){
domElement = null;
}
this.domElement = domElement;
if (GameLib.Utils.UndefinedOrNull(domStats)){
domStats = null;
}
this.domStats = domStats;
};
GameLib.System.prototype = Object.create(GameLib.API.System.prototype);
GameLib.System.prototype.constructor = GameLib.System;
GameLib.System.SYSTEM_TYPE_RENDER = 0x1;
GameLib.System.SYSTEM_TYPE_ANIMATION = 0x2;
GameLib.System.SYSTEM_TYPE_INPUT = 0x3;
GameLib.System.SYSTEM_TYPE_ALL = 0x4;
/**
* @callback
* @override
*/
GameLib.System.prototype.start = function() {
if (this.systemType == GameLib.System.SYSTEM_TYPE_INPUT) {
this.driveInputObjects = this.entityManager.query([GameLib.D3.Input.Drive]);
}
if (this.systemType == GameLib.System.SYSTEM_TYPE_RENDER) {
if (GameLib.Utils.UndefinedOrNull(this.domElement)) {
console.warn('Cannot start a rendering system without a valid DOM element');
throw new Error('Cannot start a rendering system without a valid DOM element');
}
this.domElement.innerHTML = '';
if (GameLib.Utils.UndefinedOrNull(this.domStats)) {
console.warn('No stats DOM - will run the render process without statistics information');
} else {
this.domElement.appendChild(this.domStats);
}
this.renderers = this.entityManager.query([GameLib.D3.Renderer]);
this.renderers.forEach(
function (renderer) {
this.domElement.appendChild(renderer.instance.domElement);
}.bind(this)
);
this.viewports = this.entityManager.query([GameLib.D3.Viewport]);
}
if (this.systemType == GameLib.System.SYSTEM_TYPE_ANIMATION) {
this.pathFollowingObjects = this.entityManager.query([GameLib.D3.PathFollowing]);
this.followObjects = this.entityManager.query([GameLib.D3.Follow]);
this.meshObjects = this.entityManager.query([GameLib.D3.Mesh]);
this.lookAtObjects = this.entityManager.query([GameLib.D3.LookAt]);
this.cameraObjects = this.entityManager.query([GameLib.D3.Camera]);
this.lightObjects = this.entityManager.query([GameLib.D3.Light]);
}
};
/**
* @callback
* @override
*/
GameLib.System.prototype.update = function() {
if (this.systemType == GameLib.System.SYSTEM_TYPE_INPUT) {
this.driveInputObjects.forEach(
function(object) {
object.update(deltaTime);
}
);
}
if (this.systemType == GameLib.System.SYSTEM_TYPE_RENDER) {
this.viewports.forEach(
function (viewport) {
viewport.update(deltaTime);
}
);
}
if (this.systemType == GameLib.System.SYSTEM_TYPE_ANIMATION) {
this.pathFollowingObjects.forEach(
function(object) {
object.update(deltaTime);
}
);
this.followObjects.forEach(
function(object) {
object.update(deltaTime);
}
);
this.lookAtObjects.forEach(
function(object) {
object.update(deltaTime);
}
);
this.meshObjects.forEach(
function(object) {
object.updateInstance();
}
);
this.cameraObjects.forEach(
function(object) {
object.updateInstance();
}
);
this.lightObjects.forEach(
function(object) {
object.updateInstance();
}
);
}
};
/**
* @callback
* @override
*/
GameLib.System.prototype.stop = function() {
if (this.systemType == GameLib.System.SYSTEM_TYPE_INPUT) {
this.driveInputObjects = [];
}
if (this.systemType == GameLib.System.SYSTEM_TYPE_RENDER) {
this.domElement.innerHTML = 'Rendering System Stopped';
this.renderers = [];
this.viewports = [];
}
if (this.systemType == GameLib.System.SYSTEM_TYPE_ANIMATION) {
this.pathFollowingObjects = [];
this.followObjects = [];
this.meshObjects = [];
this.lookAtObjects = [];
this.cameraObjects = [];
this.lightObjects = [];
}
};