2016-11-28 15:05:02 +01:00
|
|
|
/**
|
2017-01-06 16:53:53 +01:00
|
|
|
* Runtime Entity
|
2017-01-05 19:34:28 +01:00
|
|
|
* @param apiEntity GameLib.D3.API.Entity
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-01-06 16:53:53 +01:00
|
|
|
GameLib.Entity = function (
|
2016-12-09 20:32:09 +01:00
|
|
|
apiEntity
|
2016-11-28 15:05:02 +01:00
|
|
|
) {
|
2017-01-17 13:24:45 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiEntity)) {
|
|
|
|
apiEntity = {};
|
|
|
|
}
|
|
|
|
|
2017-05-11 04:48:02 +02:00
|
|
|
if (apiEntity instanceof GameLib.Entity) {
|
|
|
|
return apiEntity;
|
|
|
|
}
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.API.Entity.call(
|
2016-12-09 20:32:09 +01:00
|
|
|
this,
|
2016-12-15 14:53:39 +01:00
|
|
|
apiEntity.id,
|
|
|
|
apiEntity.name,
|
2017-05-22 15:42:05 +02:00
|
|
|
apiEntity.components,
|
|
|
|
apiEntity.parentEntity,
|
|
|
|
apiEntity.parentEntityManager
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
|
|
|
|
2017-01-19 17:50:11 +01:00
|
|
|
this.componentToCreate = 0;
|
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
GameLib.Component.call(
|
|
|
|
this,
|
|
|
|
GameLib.Component.COMPONENT_ENTITY,
|
|
|
|
{
|
|
|
|
'components' : [GameLib.Component],
|
|
|
|
'activeComponent' : GameLib.Component
|
|
|
|
}
|
|
|
|
);
|
2016-12-12 17:24:05 +01:00
|
|
|
};
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Entity.prototype = Object.create(GameLib.API.Entity.prototype);
|
|
|
|
GameLib.Entity.prototype.constructor = GameLib.Entity;
|
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
/**
|
2016-12-15 14:53:39 +01:00
|
|
|
* Creates an entity instance
|
2016-12-12 17:24:05 +01:00
|
|
|
*/
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Entity.prototype.createInstance = function() {
|
2016-12-16 16:03:48 +01:00
|
|
|
/**
|
|
|
|
* FUCK ecsjs and tiny-ecs - no client-side support and shitty code (only takes constructors as args)
|
|
|
|
*/
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance = true;
|
|
|
|
GameLib.Component.prototype.createInstance.call(this);
|
2016-12-16 16:03:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a component to this entity through the instance (should notify the entity manager instance)
|
|
|
|
* @param component
|
|
|
|
*/
|
2017-05-11 13:30:20 +02:00
|
|
|
GameLib.Entity.prototype.addComponent = function(component) {
|
2017-06-19 14:53:52 +02:00
|
|
|
|
2017-06-30 12:06:55 +02:00
|
|
|
GameLib.Utils.PushUnique(this.components, component);
|
2017-05-16 11:50:06 +02:00
|
|
|
|
2017-09-12 21:09:15 +02:00
|
|
|
if (component instanceof GameLib.D3.Mesh) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For meshes, simply get the children components
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
|
|
|
component.getChildrenComponents().map(function(childComponent){
|
|
|
|
GameLib.Utils.PushUnique(this.components, childComponent);
|
|
|
|
childComponent.parentEntity = this;
|
|
|
|
}.bind(this))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Here we will dig into this component - find all its 'parentEntity' members - and update them accordingly
|
|
|
|
*/
|
|
|
|
component.buildIdToObject();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also add the child components of this component as components of this entity
|
|
|
|
*/
|
|
|
|
for (var property in component.idToObject) {
|
|
|
|
if (component.idToObject.hasOwnProperty(property) &&
|
|
|
|
component.idToObject[property] !== component &&
|
|
|
|
component.idToObject[property] instanceof GameLib.Component
|
|
|
|
) {
|
|
|
|
GameLib.Utils.PushUnique(this.components, component.idToObject[property]);
|
|
|
|
component.idToObject[property].parentEntity = this;
|
|
|
|
}
|
2017-06-19 14:53:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 21:09:15 +02:00
|
|
|
|
2017-06-19 14:53:52 +02:00
|
|
|
/**
|
|
|
|
* Finally, we are the boss component - update my parent entity
|
|
|
|
* @type {GameLib.Entity}
|
|
|
|
*/
|
|
|
|
component.parentEntity = this;
|
2017-05-16 11:50:06 +02:00
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all components of type 'constructor'
|
|
|
|
* @param constructor
|
|
|
|
*/
|
2017-05-16 11:50:06 +02:00
|
|
|
GameLib.Entity.prototype.getComponents = function(constructor) {
|
2017-05-11 13:30:20 +02:00
|
|
|
|
|
|
|
var components = this.components.reduce(
|
|
|
|
function(result, component) {
|
|
|
|
if (component instanceof constructor) {
|
|
|
|
result.push(component);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
return components;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first component or null
|
|
|
|
* @param constructor
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.getFirstComponent = function(constructor) {
|
|
|
|
|
2017-05-16 11:50:06 +02:00
|
|
|
var components = this.getComponents(constructor);
|
2017-01-19 17:50:11 +01:00
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
if (components.length > 0) {
|
|
|
|
return components[0];
|
2017-01-19 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true when this entity has a certain component, false otherwise
|
|
|
|
* @param constructor
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.hasComponent = function(constructor) {
|
|
|
|
|
|
|
|
var has = this.components.reduce(
|
|
|
|
function(result, component) {
|
|
|
|
if (component instanceof constructor) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
return has;
|
2017-01-02 17:05:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param component
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.removeComponent = function(component) {
|
|
|
|
|
2017-06-19 14:53:52 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(component)) {
|
|
|
|
component = this.activeComponent;
|
|
|
|
}
|
|
|
|
|
2017-10-27 15:03:16 +02:00
|
|
|
var childIndex = this.components.indexOf(component);
|
|
|
|
if (childIndex !== -1) {
|
|
|
|
this.components.splice(childIndex, 1);
|
|
|
|
} else {
|
|
|
|
console.error('component not found');
|
|
|
|
}
|
2017-06-06 11:25:02 +02:00
|
|
|
|
2017-06-26 18:20:34 +02:00
|
|
|
/**
|
2017-10-27 15:03:16 +02:00
|
|
|
* Break the dependency to the parent
|
2017-06-26 18:20:34 +02:00
|
|
|
*/
|
|
|
|
component.parentEntity = null;
|
2017-01-02 17:05:40 +01:00
|
|
|
|
|
|
|
return true;
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-12-15 14:53:39 +01:00
|
|
|
* Updates an entity instance
|
2016-12-09 20:32:09 +01:00
|
|
|
*/
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Entity.prototype.updateInstance = function() {
|
2017-10-23 14:52:35 +02:00
|
|
|
console.log('entity update instance called');
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
2016-12-12 17:24:05 +01:00
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
/**
|
|
|
|
* Converts a GameLib.Entity to GameLib.API.Entity
|
|
|
|
* @returns {GameLib.API.Entity}
|
|
|
|
*/
|
2017-05-16 14:51:57 +02:00
|
|
|
GameLib.Entity.prototype.toApiObject = function() {
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2017-11-09 00:47:23 +01:00
|
|
|
var apiComponents = this.components.map(
|
|
|
|
function(component) {
|
|
|
|
return GameLib.Utils.IdOrNull(component);
|
|
|
|
}
|
2016-12-16 16:03:48 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return new GameLib.API.Entity(
|
|
|
|
this.id,
|
|
|
|
this.name,
|
2017-05-22 15:42:05 +02:00
|
|
|
apiComponents,
|
2017-06-02 13:52:29 +02:00
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity),
|
|
|
|
GameLib.Utils.IdOrNull(this.parentEntityManager)
|
2016-12-16 16:03:48 +01:00
|
|
|
);
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-06-25 13:31:24 +02:00
|
|
|
* Entity from Object
|
2016-12-09 20:32:09 +01:00
|
|
|
* @param objectEntity Object
|
2017-06-25 13:31:24 +02:00
|
|
|
* @param parentEntityManager GameLib.D3.EntityManager
|
|
|
|
* @returns {GameLib.Entity}
|
2016-12-09 20:32:09 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2017-06-25 13:31:24 +02:00
|
|
|
GameLib.Entity.FromObject = function(objectEntity, parentEntityManager) {
|
2016-12-16 16:03:48 +01:00
|
|
|
|
2017-06-14 14:21:57 +02:00
|
|
|
var apiEntity = GameLib.API.Entity.FromObject(objectEntity);
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2017-06-25 13:31:24 +02:00
|
|
|
var entity = new GameLib.Entity(
|
2016-12-16 16:03:48 +01:00
|
|
|
apiEntity
|
|
|
|
);
|
2017-06-25 13:31:24 +02:00
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(parentEntityManager)) {
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentEntityManager.addEntity(entity);
|
|
|
|
|
|
|
|
return entity;
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|