156 lines
4.2 KiB
JavaScript
156 lines
4.2 KiB
JavaScript
|
/**
|
||
|
* Entities with LookAt component looks to targetPosition (default up is 0,1,0)
|
||
|
* @param graphics GameLib.GraphicsRuntime
|
||
|
* @param apiLookAt GameLib.D3.API.LookAt
|
||
|
* @constructor
|
||
|
*/
|
||
|
GameLib.D3.LookAt = function (
|
||
|
graphics,
|
||
|
apiLookAt
|
||
|
) {
|
||
|
this.graphics = graphics;
|
||
|
this.graphics.isNotThreeThrow();
|
||
|
|
||
|
if (GameLib.Utils.UndefinedOrNull(apiLookAt)) {
|
||
|
apiLookAt = {};
|
||
|
}
|
||
|
|
||
|
if (apiLookAt instanceof GameLib.D3.LookAt) {
|
||
|
return apiLookAt;
|
||
|
}
|
||
|
|
||
|
GameLib.D3.API.LookAt.call(
|
||
|
this,
|
||
|
apiLookAt.id,
|
||
|
apiLookAt.name,
|
||
|
apiLookAt.currentComponent,
|
||
|
apiLookAt.targetComponent,
|
||
|
apiLookAt.targetPositionOffset,
|
||
|
apiLookAt.rotationSpeed,
|
||
|
apiLookAt.parentEntity
|
||
|
);
|
||
|
|
||
|
this.targetPositionOffset = new GameLib.Vector3(
|
||
|
this.graphics,
|
||
|
this.targetPositionOffset,
|
||
|
this
|
||
|
);
|
||
|
|
||
|
this.lookAtMatrix = new GameLib.Matrix4(
|
||
|
this.graphics,
|
||
|
this.lookAtMatrix,
|
||
|
this
|
||
|
);
|
||
|
|
||
|
this.up = new GameLib.Vector3(
|
||
|
this.graphics,
|
||
|
this.up,
|
||
|
this
|
||
|
);
|
||
|
|
||
|
this.currentRotation = new GameLib.Quaternion(
|
||
|
this.graphics,
|
||
|
this.currentRotation,
|
||
|
this
|
||
|
);
|
||
|
|
||
|
this.targetPosition = new GameLib.Vector3(
|
||
|
this.graphics,
|
||
|
this.targetPosition,
|
||
|
this
|
||
|
);
|
||
|
|
||
|
GameLib.Component.call(
|
||
|
this,
|
||
|
GameLib.Component.COMPONENT_LOOK_AT,
|
||
|
{
|
||
|
'currentComponent' : GameLib.Component,
|
||
|
'targetComponent' : GameLib.Component
|
||
|
}
|
||
|
);
|
||
|
};
|
||
|
|
||
|
GameLib.D3.LookAt.prototype = Object.create(GameLib.D3.API.LookAt.prototype);
|
||
|
GameLib.D3.LookAt.prototype.constructor = GameLib.D3.LookAt;
|
||
|
|
||
|
GameLib.D3.LookAt.prototype.createInstance = function() {
|
||
|
this.instance = true;
|
||
|
GameLib.Component.prototype.createInstance.call(this);
|
||
|
};
|
||
|
|
||
|
GameLib.D3.LookAt.prototype.updateInstance = function() {
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* to API object
|
||
|
* @returns {GameLib.D3.API.LookAt}
|
||
|
*/
|
||
|
GameLib.D3.LookAt.prototype.toApiObject = function() {
|
||
|
|
||
|
var apiLookAt = new GameLib.D3.API.LookAt(
|
||
|
this.id,
|
||
|
this.name,
|
||
|
GameLib.Utils.IdOrNull(this.currentComponent),
|
||
|
GameLib.Utils.IdOrNull(this.targetComponent),
|
||
|
this.targetPositionOffset.toApiObject(),
|
||
|
this.rotationSpeed,
|
||
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
||
|
);
|
||
|
|
||
|
return apiLookAt;
|
||
|
};
|
||
|
|
||
|
GameLib.D3.LookAt.FromObject = function(graphics, objectComponent) {
|
||
|
|
||
|
var apiLookAt = GameLib.D3.API.LookAt.FromObject(objectComponent);
|
||
|
|
||
|
return new GameLib.D3.LookAt(
|
||
|
graphics,
|
||
|
apiLookAt
|
||
|
);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Looks at using time
|
||
|
* @param deltaTime
|
||
|
*/
|
||
|
GameLib.D3.LookAt.prototype.update = function(deltaTime) {
|
||
|
|
||
|
if (this.currentComponent && this.targetComponent) {
|
||
|
|
||
|
this.targetPosition.x = this.targetComponent.position.x + this.targetPositionOffset.x;
|
||
|
this.targetPosition.y = this.targetComponent.position.y + this.targetPositionOffset.y;
|
||
|
this.targetPosition.z = this.targetComponent.position.z + this.targetPositionOffset.z;
|
||
|
|
||
|
this.targetPosition.updateInstance();
|
||
|
|
||
|
// this.lookAtMatrix.lookAt(
|
||
|
// this.currentComponent.position,
|
||
|
// this.targetPosition,
|
||
|
// this.up
|
||
|
// );
|
||
|
//
|
||
|
// this.currentRotation = new GameLib.Quaternion(this.graphics, this, new GameLib.API.Quaternion());
|
||
|
//
|
||
|
// this.currentRotation.setFromRotationMatrix(this.lookAtMatrix);
|
||
|
|
||
|
// var t = deltaTime * this.rotationSpeed;
|
||
|
// t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
|
||
|
|
||
|
// this.currentRotation.slerp(this.currentRotation, t);
|
||
|
|
||
|
// this.currentRotation.normalize();
|
||
|
//
|
||
|
// this.currentComponent.quaternion.x = this.currentRotation.x;
|
||
|
// this.currentComponent.quaternion.y = this.currentRotation.y;
|
||
|
// this.currentComponent.quaternion.z = this.currentRotation.z;
|
||
|
// this.currentComponent.quaternion.w = this.currentRotation.w;
|
||
|
//
|
||
|
this.currentComponent.lookAt.x = this.targetPosition.x;
|
||
|
this.currentComponent.lookAt.y = this.targetPosition.y;
|
||
|
this.currentComponent.lookAt.z = this.targetPosition.z;
|
||
|
|
||
|
this.currentComponent.lookAt.updateInstance();
|
||
|
}
|
||
|
|
||
|
};
|