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

390 lines
11 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 = {};
}
if (apiSystem instanceof GameLib.System) {
return apiSystem;
}
2017-01-20 13:40:27 +01:00
GameLib.API.System.call(
2017-01-12 04:44:01 +01:00
this,
apiSystem.id,
apiSystem.name,
apiSystem.systemType,
apiSystem.entityManager,
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-06-13 14:09:18 +02:00
this.saveCallback = null;
this.loadCallback = null;
2017-01-12 04:44:01 +01:00
};
GameLib.System.prototype = Object.create(GameLib.API.System.prototype);
GameLib.System.prototype.constructor = GameLib.System;
2017-05-16 11:50:06 +02:00
GameLib.System.SYSTEM_TYPE_NONE = 0x0;
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;
2017-05-16 11:50:06 +02:00
GameLib.System.SYSTEM_TYPE_STORAGE = 0x8;
2017-05-22 11:34:18 +02:00
GameLib.System.SYSTEM_TYPE_GUI = 0x10;
2017-05-16 11:50:06 +02:00
GameLib.System.SYSTEM_TYPE_ALL = 0xFFFF;
2017-01-12 04:44:01 +01:00
/**
* @callback
* @override
*/
GameLib.System.prototype.start = function() {
2017-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) {
/**
* Hookup all editor input capabilities
*/
var entities = this.entityManager.query([GameLib.D3.Input.Editor]);
entities.map(function(entity){
var component = entity.getFirstComponent(GameLib.D3.Input.Editor);
2017-05-22 15:42:05 +02:00
component.mouseDown = component.onMouseDown(entity, this.entityManager).bind(component);
component.mouseMove = component.onMouseMove(entity).bind(component);
component.keyDown = component.onKeyDown(entity, this.entityManager).bind(component);
component.keyUp = component.onKeyUp(entity, this.entityManager).bind(component);
component.domElement.instance.addEventListener('mousedown', component.mouseDown, false);
component.domElement.instance.addEventListener('mousemove', component.mouseMove, false);
component.domElement.instance.addEventListener('keydown', component.keyDown, false);
component.domElement.instance.addEventListener('keyup', component.keyUp, false);
component.controls = new THREE.EditorControls(
2017-05-16 11:50:06 +02:00
component.camera.instance,
component.domElement.instance
);
/**
* After our mouse 'up' editor controls would have updated our camera
* instance, so we need to update our game-lib camera to reflect these
* changes - we override
*/
component.mouseUp = function(center) {
return function() {
var camera = entity.getFirstComponent(GameLib.D3.Camera);
camera.position.x = camera.instance.position.x;
camera.position.y = camera.instance.position.y;
camera.position.z = camera.instance.position.z;
camera.quaternion.x = camera.instance.quaternion.x;
camera.quaternion.y = camera.instance.quaternion.y;
camera.quaternion.z = camera.instance.quaternion.z;
camera.quaternion.w = camera.instance.quaternion.w;
camera.lookAt.x = center.x;
camera.lookAt.y = center.y;
camera.lookAt.z = center.z;
camera.lookAt.instance.copy(center);
};
}(component.controls.center).bind(component);
/**
* Same applies to our mouse 'scroll' event
*/
component.mouseWheel = function() {
return function() {
var camera = entity.getFirstComponent(GameLib.D3.Camera);
camera.position.x = camera.instance.position.x;
camera.position.y = camera.instance.position.y;
camera.position.z = camera.instance.position.z;
};
}.bind(component);
component.domElement.instance.addEventListener('mousewheel', component.mouseWheel, false);
component.domElement.instance.addEventListener('mouseup', component.mouseUp, false);
}.bind(this))
2017-01-12 04:44:01 +01:00
}
2017-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {
2017-01-12 04:44:01 +01:00
this.renderEntities = this.entityManager.query(
[
GameLib.D3.Viewport,
GameLib.D3.Scene,
GameLib.D3.Renderer,
GameLib.D3.Camera
]
);
2017-05-16 11:50:06 +02:00
this.renderEntities.map(
function(entity) {
var stats = entity.getFirstComponent(GameLib.D3.Stats);
2017-05-22 11:34:18 +02:00
stats.resize();
2017-05-16 11:50:06 +02:00
stats.domElement.instance.parentElement.appendChild(stats.instance.dom);
}
)
2017-05-22 11:34:18 +02:00
}
if (this.systemType === GameLib.System.SYSTEM_TYPE_GUI) {
var guis = this.entityManager.queryComponents(GameLib.GUI);
guis.map(function(gui){
gui.domElement.instance.parentElement.appendChild(gui.instance.domElement);
})
2017-05-16 11:50:06 +02:00
2017-01-12 04:44:01 +01:00
}
2017-05-10 15:56:27 +02:00
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]);
2017-01-12 04:44:01 +01:00
}
2017-06-13 14:09:18 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) {
this.subscribe(
GameLib.Event.SAVE,
this.save
);
this.subscribe(
GameLib.Event.LOAD,
this.load
)
}
this.update();
2017-01-12 04:44:01 +01:00
};
/**
* @callback
* @override
*/
2017-01-13 16:19:51 +01:00
GameLib.System.prototype.update = function(deltaTime) {
2017-01-12 04:44:01 +01:00
2017-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) {
// this.driveInputObjects.forEach(
// function(object) {
// object.update(deltaTime);
// }
// );
2017-01-12 04:44:01 +01:00
}
2017-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) {
2017-01-12 04:44:01 +01:00
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-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {
this.renderEntities.map(
function(renderEntity) {
2017-05-16 11:50:06 +02:00
var stats = renderEntity.getFirstComponent(GameLib.D3.Stats);
stats.instance.begin();
var renderer = renderEntity.getFirstComponent(GameLib.D3.Renderer);
var camera = renderEntity.getFirstComponent(GameLib.D3.Camera);
var viewports = renderEntity.getComponents(GameLib.D3.Viewport);
var scenes = renderEntity.getComponents(GameLib.D3.Scene);
if (viewports.length > 1) {
renderer.instance.autoClear = false;
}
2017-05-11 17:52:33 +02:00
if (scenes.length > 1) {
renderer.instance.autoClear = false;
}
renderer.instance.clear();
viewports.map(
2017-06-06 11:25:02 +02:00
function(viewport){
renderer.instance.setViewport(
viewport.x * renderer.width,
viewport.y * renderer.height,
viewport.width * renderer.width,
viewport.height * renderer.height
);
2017-06-06 11:25:02 +02:00
function renderScene(scene) {
if (scene.activeCamera) {
renderer.instance.render(
scene.instance,
scene.activeCamera.instance
);
} else {
renderer.instance.render(
scene.instance,
camera.instance
);
}
}
if (viewport.scenes && viewport.scenes.length > 0) {
viewport.scenes.map(renderScene);
} else {
scenes.map(renderScene);
}
}
);
2017-05-16 11:50:06 +02:00
stats.instance.end();
}
);
2017-01-13 16:19:51 +01:00
}
2017-01-12 04:44:01 +01:00
};
/**
* @callback
* @override
*/
GameLib.System.prototype.stop = function() {
2017-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_INPUT) {
/**
* Now remove all editor input capabilities
*/
var entities = this.entityManager.query([GameLib.D3.Input.Editor]);
entities.map(function(entity){
var component = entity.getFirstComponent(GameLib.D3.Input.Editor);
component.domElement.instance.removeEventListener('mousedown', component.mouseDown, false);
component.domElement.instance.removeEventListener('mousemove', component.mouseMove, false);
component.domElement.instance.removeEventListener('keydown', component.keyDown, false);
component.domElement.instance.removeEventListener('keyup', component.keyUp, false);
component.controls.dispose();
component.domElement.instance.removeEventListener('mouseup', component.mouseUp, false);
component.domElement.instance.removeEventListener('mousewheel', component.mouseWheel, false);
});
console.log('stopped all input systems');
2017-01-12 04:44:01 +01:00
}
2017-05-22 11:34:18 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_GUI) {
console.log('stopping GUI system');
var guis = this.entityManager.queryComponents(GameLib.GUI);
guis.map(function(gui){
gui.domElement.instance.parentElement.removeChild(gui.instance.domElement);
})
}
2017-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) {
2017-05-22 11:34:18 +02:00
this.renderEntities.map(
function(entity) {
var stats = entity.getFirstComponent(GameLib.D3.Stats);
stats.domElement.instance.parentElement.removeChild(stats.instance.dom);
}
);
this.renderEntities = [];
2017-01-12 04:44:01 +01:00
}
2017-05-10 15:56:27 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) {
// this.pathFollowingObjects = [];
// this.followObjects = [];
// this.meshObjects = [];
// this.lookAtObjects = [];
// this.cameraObjects = [];
// this.lightObjects = [];
2017-01-12 04:44:01 +01:00
}
2017-06-13 14:09:18 +02:00
if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) {
this.unsubscribe(
GameLib.Event.SAVE,
this.save
);
this.unsubscribe(
GameLib.Event.LOAD,
this.load
)
}
2017-01-12 04:44:01 +01:00
};
2017-01-20 13:40:27 +01:00
/**
* Converts runtime vector to API Vector
2017-05-10 15:56:27 +02:00
* @returns {GameLib.API.System}
2017-01-20 13:40:27 +01:00
*/
2017-05-16 14:51:57 +02:00
GameLib.System.prototype.toApiObject = function() {
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),
2017-01-20 13:40:27 +01:00
GameLib.Utils.IdOrNull(this.parentEntity)
);
};