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

112 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-11-28 15:05:02 +01:00
/**
* Entity Runtime
* @constructor
*/
2016-12-15 14:53:39 +01:00
GameLib.Entity = function RuntimeEntity(
entityManager,
parentObject,
2016-12-09 20:32:09 +01:00
apiEntity
2016-11-28 15:05:02 +01:00
) {
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(entityManager)) {
throw new Error('You cannot create entities without an entity manager')
2016-11-28 15:05:02 +01:00
}
2016-12-15 14:53:39 +01:00
this.entityManager = entityManager;
2016-11-28 15:05:02 +01:00
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
2016-12-09 20:32:09 +01:00
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,
apiEntity.components
2016-12-09 20:32:09 +01:00
);
2016-12-15 14:53:39 +01:00
this.instance = this.createInstance();
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() {
var instance = this.entityManager.createEntity();
return instance;
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() {
this.instance = this.createInstance(true);
};
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}
*/
GameLib.Entity.prototype.toApiEntity = function() {
//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;
2016-12-09 20:32:09 +01:00
};
/**
*
* @param graphics GameLib.D3.Graphics
* @param objectEntity Object
* @constructor
*/
2016-12-15 14:53:39 +01:00
GameLib.Entity.FromObjectEntity = 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
// );
2016-11-01 09:43:36 +01:00
};