2016-11-04 15:50:33 +01:00
|
|
|
GameLib.D3.ComponentLookAt = function(
|
|
|
|
componentId,
|
2016-11-04 16:31:56 +01:00
|
|
|
targetEntity,
|
2016-11-08 12:04:42 +01:00
|
|
|
targetOffset,
|
|
|
|
rotationSpeed
|
2016-11-04 15:50:33 +01:00
|
|
|
) {
|
|
|
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
|
|
|
this.parentEntity = null;
|
|
|
|
|
|
|
|
// Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.
|
|
|
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentLookAt, GameLib.D3.ComponentInterface);
|
|
|
|
|
2016-11-04 16:31:56 +01:00
|
|
|
// todo: USE TARGET OFFSET.
|
2016-11-04 15:50:33 +01:00
|
|
|
this.targetEntity = targetEntity;
|
2016-11-08 11:13:55 +01:00
|
|
|
|
|
|
|
if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) {
|
|
|
|
targetOffset = new GameLib.D3.Vector3(0, 0, 0);
|
|
|
|
}
|
|
|
|
this.targetOffset = targetOffset;
|
|
|
|
|
|
|
|
this.lastTargetQuaternion = new GameLib.D3.Vector4(0, 0, 0, 1);
|
2016-11-08 12:04:42 +01:00
|
|
|
this.rotationSpeed = rotationSpeed || 22.0;
|
2016-11-04 15:50:33 +01:00
|
|
|
};
|
|
|
|
|
2016-11-08 11:13:55 +01:00
|
|
|
|
2016-11-08 12:12:56 +01:00
|
|
|
ComponentLookAt_rotatedTargetOffset = new THREE.Vector3();
|
2016-11-08 11:13:55 +01:00
|
|
|
ComponentLookAt_currentPos = new THREE.Vector3();
|
|
|
|
ComponentLookAt_targetPos = new THREE.Vector3();
|
|
|
|
ComponentLookAt_upVector = new THREE.Vector3(0, 1, 0);
|
2016-11-08 12:12:56 +01:00
|
|
|
ComponentLookAt_targetQuaternion = new THREE.Quaternion(0, 0, 0, 1);
|
2016-11-08 11:13:55 +01:00
|
|
|
ComponentLookAt_tmpQuaternion = new THREE.Quaternion(0, 0, 0, 1);
|
|
|
|
ComponentLookAt_newRotationQuaternion = new THREE.Quaternion(0, 0, 0, 1);
|
|
|
|
ComponentLookAt_lastRotationQuaternion = new THREE.Quaternion(0, 0, 0, 1);
|
|
|
|
ComponentLookAt_lookAtMatrix = new THREE.Matrix4();
|
|
|
|
|
2016-11-04 15:50:33 +01:00
|
|
|
///////////////////////// Methods to override //////////////////////////
|
|
|
|
GameLib.D3.ComponentLookAt.prototype.onUpdate = function(
|
|
|
|
deltaTime,
|
|
|
|
parentEntity
|
|
|
|
) {
|
|
|
|
if(this.targetEntity) {
|
|
|
|
var target = this.targetEntity.position;
|
2016-11-08 11:13:55 +01:00
|
|
|
|
|
|
|
ComponentLookAt_currentPos.set(
|
|
|
|
parentEntity.position.x,
|
|
|
|
parentEntity.position.y,
|
|
|
|
parentEntity.position.z
|
|
|
|
);
|
|
|
|
|
|
|
|
ComponentLookAt_lastRotationQuaternion.set(
|
|
|
|
this.lastTargetQuaternion.x,
|
|
|
|
this.lastTargetQuaternion.y,
|
|
|
|
this.lastTargetQuaternion.z,
|
|
|
|
this.lastTargetQuaternion.w
|
2016-11-04 15:50:33 +01:00
|
|
|
);
|
|
|
|
|
2016-11-08 12:12:56 +01:00
|
|
|
ComponentLookAt_rotatedTargetOffset.set(
|
|
|
|
this.targetOffset.x,
|
|
|
|
this.targetOffset.y,
|
|
|
|
this.targetOffset.z
|
|
|
|
);
|
|
|
|
|
|
|
|
ComponentLookAt_targetQuaternion.set(
|
|
|
|
this.targetEntity.quaternion.x,
|
|
|
|
this.targetEntity.quaternion.y,
|
|
|
|
this.targetEntity.quaternion.z,
|
|
|
|
this.targetEntity.quaternion.w
|
|
|
|
);
|
|
|
|
|
|
|
|
ComponentLookAt_rotatedTargetOffset =
|
|
|
|
ComponentLookAt_rotatedTargetOffset.applyQuaternion(
|
|
|
|
ComponentLookAt_targetQuaternion
|
|
|
|
);
|
|
|
|
|
2016-11-08 11:13:55 +01:00
|
|
|
ComponentLookAt_targetPos.set(
|
2016-11-08 12:12:56 +01:00
|
|
|
target.x + ComponentLookAt_rotatedTargetOffset.x,
|
|
|
|
target.y + ComponentLookAt_rotatedTargetOffset.y,
|
|
|
|
target.z + ComponentLookAt_rotatedTargetOffset.z
|
2016-11-08 11:13:55 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
ComponentLookAt_lookAtMatrix.lookAt(
|
|
|
|
ComponentLookAt_currentPos,
|
|
|
|
ComponentLookAt_targetPos,
|
|
|
|
ComponentLookAt_upVector
|
|
|
|
);
|
|
|
|
|
|
|
|
ComponentLookAt_tmpQuaternion.setFromRotationMatrix(ComponentLookAt_lookAtMatrix);
|
|
|
|
|
|
|
|
var t = deltaTime * this.rotationSpeed;
|
|
|
|
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
|
|
|
|
|
|
|
|
THREE.Quaternion.slerp(ComponentLookAt_lastRotationQuaternion, ComponentLookAt_tmpQuaternion, ComponentLookAt_newRotationQuaternion, t);
|
|
|
|
|
|
|
|
this.parentEntity.quaternion.x = this.lastTargetQuaternion.x = ComponentLookAt_newRotationQuaternion.x;
|
|
|
|
this.parentEntity.quaternion.y = this.lastTargetQuaternion.y = ComponentLookAt_newRotationQuaternion.y;
|
|
|
|
this.parentEntity.quaternion.z = this.lastTargetQuaternion.z = ComponentLookAt_newRotationQuaternion.z;
|
|
|
|
this.parentEntity.quaternion.w = this.lastTargetQuaternion.w = ComponentLookAt_newRotationQuaternion.w;
|
2016-11-04 15:50:33 +01:00
|
|
|
}
|
|
|
|
};
|