2016-11-28 15:05:02 +01:00
|
|
|
/**
|
|
|
|
* Entity Runtime
|
2016-12-09 20:32:09 +01:00
|
|
|
* @param graphics GameLib.D3.Graphics
|
|
|
|
* @param apiEntity GameLib.D3.API.Entity
|
2016-11-28 15:05:02 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-12-07 15:20:41 +01:00
|
|
|
GameLib.D3.Entity = function Entity(
|
2016-12-09 20:32:09 +01:00
|
|
|
graphics,
|
|
|
|
apiEntity
|
2016-11-28 15:05:02 +01:00
|
|
|
) {
|
|
|
|
for (var property in apiEntity) {
|
|
|
|
if (apiEntity.hasOwnProperty(property)) {
|
|
|
|
this[property] = apiEntity[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
|
|
|
|
this.position = new GameLib.D3.Vector3(
|
|
|
|
this.graphics,
|
|
|
|
this,
|
|
|
|
this.position
|
|
|
|
);
|
|
|
|
|
|
|
|
this.quaternion = new GameLib.D3.Quaternion(
|
|
|
|
this.graphics,
|
|
|
|
this,
|
|
|
|
this.quaternion
|
|
|
|
);
|
|
|
|
|
|
|
|
this.scale = new GameLib.D3.Vector3(
|
|
|
|
this.graphics,
|
|
|
|
this,
|
|
|
|
this.scale
|
|
|
|
);
|
2016-12-12 17:24:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the Entity and it's components
|
|
|
|
* @param deltaTime Number
|
|
|
|
* @param late boolean to indicate whether or not this is a late update
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.prototype.update = function(
|
|
|
|
deltaTime,
|
|
|
|
late
|
|
|
|
) {
|
|
|
|
|
|
|
|
this.components.forEach(
|
|
|
|
function (component) {
|
|
|
|
if (!late && component.onUpdate) {
|
|
|
|
component.onUpdate(deltaTime, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (late && component.onLateUpdate) {
|
|
|
|
component.onLateUpdate(deltaTime, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a GameLib.D3.Entity to GameLib.D3.API.Entity
|
|
|
|
* @returns {GameLib.D3.API.Entity}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.prototype.toApiEntity = function() {
|
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
var apiEntity = new GameLib.D3.API.Entity(
|
2016-12-09 20:32:09 +01:00
|
|
|
this.id,
|
|
|
|
this.name,
|
2016-12-12 17:24:05 +01:00
|
|
|
GameLib.D3.Utils.IdArrayOrEmptyArray(this.components),
|
2016-12-09 20:32:09 +01:00
|
|
|
this.position.toApiVector(),
|
|
|
|
this.quaternion.toApiQuaternion(),
|
|
|
|
this.scale.toApiVector(),
|
|
|
|
GameLib.D3.Utils.IdOrNull(this.parentScene),
|
|
|
|
GameLib.D3.Utils.IdOrNull(this.mesh)
|
|
|
|
);
|
2016-12-12 17:24:05 +01:00
|
|
|
|
|
|
|
return apiEntity;
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param graphics GameLib.D3.Graphics
|
|
|
|
* @param objectEntity Object
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.FromObjectEntity = function(graphics, objectEntity) {
|
|
|
|
|
|
|
|
var apiEntity = new GameLib.D3.API.Entity(
|
|
|
|
objectEntity.id,
|
|
|
|
objectEntity.name,
|
2016-12-12 17:24:05 +01:00
|
|
|
objectEntity.components,
|
2016-12-09 20:32:09 +01:00
|
|
|
new GameLib.D3.API.Vector3(
|
|
|
|
objectEntity.position.x,
|
|
|
|
objectEntity.position.y,
|
|
|
|
objectEntity.position.z
|
|
|
|
),
|
|
|
|
new GameLib.D3.API.Quaternion(
|
|
|
|
objectEntity.quaternion.x,
|
|
|
|
objectEntity.quaternion.y,
|
|
|
|
objectEntity.quaternion.z,
|
|
|
|
objectEntity.quaternion.w,
|
|
|
|
new GameLib.D3.API.Vector3(
|
|
|
|
objectEntity.quaternion.axis.x,
|
|
|
|
objectEntity.quaternion.axis.y,
|
|
|
|
objectEntity.quaternion.axis.z
|
|
|
|
)
|
|
|
|
),
|
|
|
|
new GameLib.D3.API.Vector3(
|
|
|
|
objectEntity.scale.x,
|
|
|
|
objectEntity.scale.y,
|
|
|
|
objectEntity.scale.z
|
|
|
|
),
|
|
|
|
objectEntity.parentScene,
|
|
|
|
objectEntity.mesh
|
|
|
|
);
|
|
|
|
|
|
|
|
return new GameLib.D3.Entity(
|
|
|
|
graphics,
|
|
|
|
apiEntity
|
|
|
|
);
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
/**
|
|
|
|
* Gets called when the entity was registered with it's parent scene
|
|
|
|
* @param parentScene GameLib.D3.Scene
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.prototype.register = function(
|
2016-11-01 09:43:36 +01:00
|
|
|
parentScene
|
|
|
|
) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
/**
|
|
|
|
* Adds a components to the entity and registers it with the entity's parent scene
|
2016-12-12 17:24:05 +01:00
|
|
|
* @param component GameLib.D3.Component
|
2016-11-01 10:00:15 +01:00
|
|
|
*/
|
2016-11-01 09:43:36 +01:00
|
|
|
GameLib.D3.Entity.prototype.addComponent = function(
|
|
|
|
component
|
|
|
|
) {
|
2016-12-12 17:24:05 +01:00
|
|
|
this.components.push(component);
|
2016-11-01 09:43:36 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
if (_.isFunction(component.onAdd)) {
|
|
|
|
component.onAdd(this);
|
2016-11-01 12:42:06 +01:00
|
|
|
}
|
2016-11-01 10:00:15 +01:00
|
|
|
};
|
|
|
|
|
2016-11-30 10:46:25 +01:00
|
|
|
GameLib.D3.Entity.prototype.removeComponent = function(component) {
|
2016-12-12 17:24:05 +01:00
|
|
|
|
|
|
|
var index = this.components.indexOf(component);
|
|
|
|
this.components.splice(index, 1);
|
|
|
|
|
|
|
|
if (_.isFunction(component.onRemove)) {
|
|
|
|
component.onRemove(this);
|
2016-11-30 10:46:25 +01:00
|
|
|
}
|
|
|
|
};
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
/**
|
|
|
|
* Links object Ids to actual objects
|
|
|
|
* @param idToObject
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.prototype.linkObjects = function(idToObject) {
|
2016-11-04 10:58:48 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
this.components.forEach(
|
|
|
|
function(currentValue, index, array) {
|
2016-11-04 10:58:48 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
if (!idToObject[currentValue]) {
|
|
|
|
throw new Error('Unable to locate component with ID: ' + currentValue);
|
|
|
|
}
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
array[index] = idToObject[currentValue];
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
if (this.parentScene) {
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
if (!idToObject[this.parentScene]) {
|
|
|
|
throw new Error('Unable to locate scene with ID: ' + this.parentScene);
|
|
|
|
}
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
this.parentScene = idToObject[this.parentScene];
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
}
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
if (this.mesh) {
|
2016-12-09 20:32:09 +01:00
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
if (!idToObject[this.mesh]) {
|
|
|
|
throw new Error('Unable to locate mesh with ID: ' + this.mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.mesh = idToObject[this.mesh];
|
|
|
|
}
|
2016-12-09 20:32:09 +01:00
|
|
|
|
2016-10-28 15:30:15 +02:00
|
|
|
};
|