r3-legacy/src/game-lib-entity-manager.js

237 lines
6.0 KiB
JavaScript
Raw Normal View History

2016-12-15 14:53:39 +01:00
/**
* GameLib.EntityManager
2017-01-06 16:53:53 +01:00
* @param graphics GameLib.D3.Graphics
* @param apiEntityManager GameLib.API.EntityManager
2016-12-15 14:53:39 +01:00
* @constructor
*/
GameLib.EntityManager = function(
2017-01-06 16:53:53 +01:00
graphics,
apiEntityManager
) {
2017-01-06 16:53:53 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(apiEntityManager)) {
apiEntityManager = {};
}
2016-12-15 14:53:39 +01:00
GameLib.API.EntityManager.call(
this,
2017-01-19 17:50:11 +01:00
apiEntityManager.id,
apiEntityManager.name,
apiEntityManager.entities,
apiEntityManager.parentEntity
2016-12-15 14:53:39 +01:00
);
2017-01-05 19:34:28 +01:00
this.entities = this.entities.map(
function(apiEntity) {
2017-01-12 04:44:01 +01:00
if (apiEntity instanceof GameLib.API.Entity) {
return new GameLib.Entity(
this.graphics,
apiEntity
)
} else {
console.warn('Entity not of type API.Entity');
throw new Error('Entity not of type API.Entity');
}
2017-01-06 16:53:53 +01:00
}.bind(this)
2017-01-05 19:34:28 +01:00
);
2017-01-19 17:50:11 +01:00
this.buildIdToObject();
2016-12-15 14:53:39 +01:00
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() {
/**
* Fuck the current ECS bullshit on the internet - both tiny-ecs and ecsjs SUCKS ASS
*/
return null;
2016-12-15 14:53:39 +01:00
};
/**
* Creates an GameLib.Entity
* @returns {*}
*/
GameLib.EntityManager.prototype.createEntity = function(name) {
2017-01-19 17:50:11 +01:00
var apiEntity = new GameLib.API.Entity(
null,
name
);
2016-12-15 14:53:39 +01:00
2017-01-19 17:50:11 +01:00
var entity = new GameLib.Entity(
this.graphics,
apiEntity
);
2016-12-15 14:53:39 +01:00
this.entities.push(entity);
return entity;
};
/**
* Removes an entity
* @param entity GameLib.D3.Entity
* @returns boolean true if successful
2016-12-15 14:53:39 +01:00
*/
GameLib.EntityManager.prototype.removeEntity = function(entity) {
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;
};
/**
* Returns all the objects with the following components
* @param components GameLib.Component[]
*/
GameLib.EntityManager.prototype.query = function(components) {
2016-12-19 17:44:15 +01:00
var result = this.entities.reduce(
function(__queryComponents) {
2016-12-19 17:44:15 +01:00
return function(result, entity) {
var results = __queryComponents.reduce(
function(__entity) {
2016-12-19 17:44:15 +01:00
return function(componentResult, queryComponent) {
var components = __entity.components.reduce(
function(__queryComponent) {
2016-12-19 17:44:15 +01:00
return function(__components, entityComponent) {
if (__queryComponent == entityComponent.constructor) {
// arrow should point towards a window or sergej --->
2016-12-19 17:44:15 +01:00
__components[entityComponent.id] = entityComponent;
}
2016-12-19 17:44:15 +01:00
return __components;
}
2016-12-19 17:44:15 +01:00
}(queryComponent),
{}
);
2016-12-19 17:44:15 +01:00
for (var property in components) {
if (components.hasOwnProperty(property)) {
componentResult[property] = components[property];
}
}
return componentResult;
}
2016-12-19 17:44:15 +01:00
}(entity),
{}
);
for (var property in results) {
if (results.hasOwnProperty(property)) {
result[property] = results[property];
}
}
return result;
}
2016-12-19 17:44:15 +01:00
}(components),
{}
);
2016-12-19 17:44:15 +01:00
var array = [];
for (var property in result) {
if (result.hasOwnProperty(property)) {
array.push(result[property]);
}
}
return array;
2016-12-15 14:53:39 +01:00
};
/**
* Converts a GameLib.Entity to GameLib.API.Entity
* @returns {GameLib.API.EntityManager}
2016-12-15 14:53:39 +01:00
*/
2016-12-15 15:28:00 +01:00
GameLib.EntityManager.prototype.toApiEntityManager = function() {
2016-12-15 14:53:39 +01:00
var apiEntities = this.entities.map(
function (entity) {
return entity.toApiEntity();
}
);
var apiEntityManager = new GameLib.API.EntityManager(
2017-01-19 17:50:11 +01:00
this.id,
this.name,
apiEntities,
this.parentEntity
);
return apiEntityManager;
2016-12-15 14:53:39 +01:00
};
/**
2017-01-06 16:53:53 +01:00
* Returns an EntityManager from an Object entity manager
* @param graphics
2017-01-06 16:53:53 +01:00
* @param objectEntityManager Object
2016-12-15 14:53:39 +01:00
* @constructor
*/
2017-01-06 16:53:53 +01:00
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntityManager) {
2017-01-06 16:53:53 +01:00
var apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectEntityManager);
var entityManager = new GameLib.EntityManager(
2017-01-06 16:53:53 +01:00
graphics,
apiEntityManager
);
return entityManager;
2016-12-15 14:53:39 +01:00
};
/**
* Links object Ids to actual objects
* @param idToObject
*/
2017-02-22 16:06:27 +01:00
// GameLib.EntityManager.prototype.linkObjects = function(idToObject) {
//
// this.entities.map(
// function(entity) {
// entity.components.map(
// function (componentId, index, array) {
// if (componentId instanceof GameLib.Component) {
// array[index] = componentId;
// } else {
// array[index] = idToObject[componentId];
// }
//
// Object.keys(array[index].linkedObjects).map(
// function (propertyName) {
// array[index][propertyName] = idToObject[array[index][propertyName]];
// }
// );
//
// array[index].loaded = true;
// }
// )
// }
// );
//
// };