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

214 lines
5.1 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)
2017-01-20 13:40:27 +01:00
* @param graphics
2017-01-12 04:44:01 +01:00
* @param apiSystem GameLib.API.System
* @constructor
*/
GameLib.System = function(
2017-01-20 13:40:27 +01:00
graphics,
apiSystem
2017-01-12 04:44:01 +01:00
) {
2017-01-20 13:40:27 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiSystem)) {
apiSystem = {};
}
GameLib.API.System.call(
2017-01-12 04:44:01 +01:00
this,
apiSystem.id,
apiSystem.name,
apiSystem.systemType,
apiSystem.entityManager,
2017-01-20 13:40:27 +01:00
apiSystem.domElement,
apiSystem.domStats,
2017-01-12 04:44:01 +01:00
apiSystem.parentEntity
);
2017-02-21 18:55:18 +01:00
if (this.entityManager instanceof GameLib.API.EntityManager) {
this.entityManager = new GameLib.EntityManager(
this.graphics,
this.entityManager
);
}
2017-01-12 04:44:01 +01:00
2017-02-21 18:55:18 +01:00
if (this.domElement instanceof GameLib.API.DomElement) {
this.domElement = new GameLib.DomElement(
this.graphics,
this.domElement
);
}
2017-01-12 04:44:01 +01:00
};
GameLib.System.prototype = Object.create(GameLib.API.System.prototype);
GameLib.System.prototype.constructor = GameLib.System;
2017-01-12 17:40:17 +01:00
GameLib.System.SYSTEM_TYPE_RENDER = 0x1;
GameLib.System.SYSTEM_TYPE_ANIMATION = 0x2;
GameLib.System.SYSTEM_TYPE_INPUT = 0x4;
GameLib.System.SYSTEM_TYPE_ALL = 0x7;
2017-01-12 04:44:01 +01:00
/**
* @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
*/
2017-01-13 16:19:51 +01:00
GameLib.System.prototype.update = function(deltaTime) {
2017-01-12 04:44:01 +01:00
if (this.systemType == GameLib.System.SYSTEM_TYPE_INPUT) {
this.driveInputObjects.forEach(
function(object) {
object.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();
}
);
}
2017-01-13 16:19:51 +01:00
if (this.systemType == GameLib.System.SYSTEM_TYPE_RENDER) {
this.viewports.forEach(
function (viewport) {
viewport.update(deltaTime);
}
);
}
2017-01-12 04:44:01 +01:00
};
/**
* @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 = [];
}
};
2017-01-20 13:40:27 +01:00
/**
* Converts runtime vector to API Vector
* @returns {GameLib.API.Mouse}
*/
GameLib.System.prototype.toApiSystem = function() {
2017-02-21 18:55:18 +01:00
var apiDomElement = null;
if (this.domElement instanceof GameLib.DomElement) {
apiDomElement = this.domElement.toApiDomElement();
}
var apiDomStats = null;
if (this.domStats instanceof GameLib.DomElement) {
apiDomStats = this.domStats.toApiDomElement();
}
2017-01-20 13:40:27 +01:00
return new GameLib.API.System(
this.id,
this.name,
this.systemType,
2017-02-21 18:55:18 +01:00
GameLib.Utils.IdOrNull(this.entityManager),
apiDomElement,
apiDomStats,
2017-01-20 13:40:27 +01:00
GameLib.Utils.IdOrNull(this.parentEntity)
);
};