2016-12-15 14:53:39 +01:00
|
|
|
/**
|
|
|
|
* GameLib.EntityManager
|
|
|
|
* @param entities GameLib.D3.Entity[]
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager = function(entities) {
|
|
|
|
|
|
|
|
GameLib.API.EntityManager.call(
|
|
|
|
this,
|
|
|
|
entities
|
|
|
|
);
|
|
|
|
|
|
|
|
this.instance = this.createInstance();
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.EntityManager.prototype = Object.create(GameLib.API.EntityManager.prototype);
|
|
|
|
GameLib.EntityManager.prototype.constructor = GameLib.EntityManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an Entity Manager instance
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.createInstance = function() {
|
|
|
|
|
|
|
|
var instance = null;
|
|
|
|
|
|
|
|
if (typeof 'require' != 'undefined') {
|
|
|
|
instance = require('tiny-ecs').EntityManager;
|
|
|
|
} else {
|
|
|
|
instance = EntityManager.EntityManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an GameLib.Entity
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.createEntity = function() {
|
|
|
|
|
|
|
|
var entity = new GameLib.Entity(this);
|
|
|
|
|
|
|
|
this.entities.push(entity);
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an entity
|
|
|
|
* @param entity GameLib.D3.Entity
|
|
|
|
* @returns bool true if successful
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.removeEntity = function(entity) {
|
|
|
|
|
|
|
|
entity.instance.remove();
|
|
|
|
|
|
|
|
var index = this.entities.indexOf(entity);
|
|
|
|
|
|
|
|
if (index == -1) {
|
|
|
|
console.log('failed to remove entity : ', entity);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entities.splice(index, 1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a component to an entity
|
|
|
|
* @param entity GameLib.D3.Entity
|
|
|
|
* @param component Object.constructor
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.entityAddComponent = function(entity, component) {
|
|
|
|
this.instance.entityAddComponent(entity.instance, component);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the objects with the following components
|
|
|
|
* @param components GameLib.Component[]
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.queryComponents = function(components) {
|
|
|
|
return this.instance.queryComponents(components);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a GameLib.Entity to GameLib.API.Entity
|
|
|
|
* @returns {GameLib.API.Entity}
|
|
|
|
*/
|
2016-12-15 15:28:00 +01:00
|
|
|
GameLib.EntityManager.prototype.toApiEntityManager = function() {
|
2016-12-15 14:53:39 +01:00
|
|
|
|
|
|
|
//TODO: refactor / fix
|
|
|
|
// var apiEntity = new GameLib.API.Entity(
|
|
|
|
// this.id,
|
|
|
|
// this.name,
|
|
|
|
// GameLib.Utils.IdArrayOrEmptyArray(this.components),
|
|
|
|
// this.position.toApiVector(),
|
|
|
|
// this.quaternion.toApiQuaternion(),
|
|
|
|
// this.scale.toApiVector(),
|
|
|
|
// GameLib.Utils.IdOrNull(this.parentScene),
|
|
|
|
// GameLib.Utils.IdOrNull(this.mesh)
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// return apiEntity;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param graphics GameLib.D3.Graphics
|
|
|
|
* @param objectEntity Object
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntity) {
|
|
|
|
|
|
|
|
//TODO: refactor /fix
|
|
|
|
// var apiEntity = new GameLib.API.Entity(
|
|
|
|
// objectEntity.id,
|
|
|
|
// objectEntity.name,
|
|
|
|
// objectEntity.components,
|
|
|
|
// new GameLib.API.Vector3(
|
|
|
|
// objectEntity.position.x,
|
|
|
|
// objectEntity.position.y,
|
|
|
|
// objectEntity.position.z
|
|
|
|
// ),
|
|
|
|
// new GameLib.API.Quaternion(
|
|
|
|
// objectEntity.quaternion.x,
|
|
|
|
// objectEntity.quaternion.y,
|
|
|
|
// objectEntity.quaternion.z,
|
|
|
|
// objectEntity.quaternion.w,
|
|
|
|
// new GameLib.API.Vector3(
|
|
|
|
// objectEntity.quaternion.axis.x,
|
|
|
|
// objectEntity.quaternion.axis.y,
|
|
|
|
// objectEntity.quaternion.axis.z
|
|
|
|
// )
|
|
|
|
// ),
|
|
|
|
// new GameLib.API.Vector3(
|
|
|
|
// objectEntity.scale.x,
|
|
|
|
// objectEntity.scale.y,
|
|
|
|
// objectEntity.scale.z
|
|
|
|
// ),
|
|
|
|
// objectEntity.parentScene,
|
|
|
|
// objectEntity.mesh
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// return new GameLib.Entity(
|
|
|
|
// graphics,
|
|
|
|
// apiEntity
|
|
|
|
// );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Links object Ids to actual objects
|
|
|
|
* @param idToObject
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.linkObjects = function(idToObject) {
|
|
|
|
|
|
|
|
// TODO: fix
|
|
|
|
// this.components.forEach(
|
|
|
|
// function(currentValue, index, array) {
|
|
|
|
//
|
|
|
|
// if (!idToObject[currentValue]) {
|
|
|
|
// //throw new Error('Unable to locate component with ID: ' + currentValue);
|
|
|
|
// console.log('Unable to locate component with ID: ' + currentValue + ' - it must have been deleted before');
|
|
|
|
// array.splice(index, 1);
|
|
|
|
// } else {
|
|
|
|
// array[index] = idToObject[currentValue];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// }.bind(this)
|
|
|
|
// );
|
|
|
|
|
|
|
|
};
|