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

56 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-11-29 12:54:25 +01:00
/**
2016-12-12 17:24:05 +01:00
* Entity API Object (for storing / loading entities to and from API)
2016-11-29 12:54:25 +01:00
* @param id
* @param name
2016-12-15 14:53:39 +01:00
* @param components GameLib.Component[]
2016-11-29 12:54:25 +01:00
* @constructor
*/
2016-12-15 14:53:39 +01:00
GameLib.API.Entity = function(
2016-11-29 12:54:25 +01:00
id,
name,
2016-12-15 14:53:39 +01:00
components
2016-11-29 12:54:25 +01:00
) {
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
2016-11-29 12:54:25 +01:00
}
this.id = id;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(name)) {
2016-11-29 12:54:25 +01:00
name = this.constructor.name;
}
this.name = name;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(components)) {
2016-12-12 17:24:05 +01:00
components = [];
2016-11-29 12:54:25 +01:00
}
2016-12-12 17:24:05 +01:00
this.components = components;
2016-12-15 14:53:39 +01:00
};
2016-11-29 12:54:25 +01:00
2016-12-15 14:53:39 +01:00
/**
* Adds a components to the entity
* @param component GameLib.Component
*/
GameLib.API.Entity.prototype.addComponent = function(component) {
this.components.push(component);
this.instance.addComponent(component)
};
2016-11-29 12:54:25 +01:00
2016-12-15 14:53:39 +01:00
/**
* Removes a component from this entity
* @param component GameLib.Component
*/
GameLib.API.Entity.prototype.removeComponent = function(component) {
2016-11-29 12:54:25 +01:00
2016-12-15 14:53:39 +01:00
this.instance.removeComponent(component);
2016-11-29 12:54:25 +01:00
2016-12-15 14:53:39 +01:00
var index = this.components.indexOf(component);
if (index == -1) {
console.log('failed to remove component');
return false;
2016-11-29 12:54:25 +01:00
}
2016-12-15 14:53:39 +01:00
this.components.splice(index, 1);
return true;
};