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

69 lines
1.6 KiB
JavaScript

/**
* Entity API Object (for storing / loading entities to and from API)
* @param id
* @param name
* @param components GameLib.Component[]
* @param parentEntity GameLib.Entity
* @param parentEntityManager
* @constructor
*/
GameLib.API.Entity = function(
id,
name,
components,
parentEntity,
parentEntityManager
) {
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
if (GameLib.Utils.UndefinedOrNull(parentEntityManager)) {
parentEntityManager = null;
}
this.parentEntityManager = parentEntityManager;
this.activeComponent = null;
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Entity (' + this.id + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(components)) {
components = [];
}
this.components = components;
};
GameLib.API.Entity.prototype = Object.create(GameLib.Component.prototype);
GameLib.API.Entity.prototype.constructor = GameLib.API.Entity;
/**
* Returns an API entity from an Object entity
* @param objectEntity
* @constructor
*/
GameLib.API.Entity.FromObject = function(objectEntity) {
var apiComponents = objectEntity.components.map(
function (objectComponent) {
return GameLib.API.Component.FromObject(objectComponent);
}
);
return new GameLib.API.Entity(
objectEntity.id,
objectEntity.name,
apiComponents,
objectEntity.parentEntity,
objectEntity.parentEntityManager
)
};