r3-legacy/src/game-lib-d3-look-at.js

131 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-12-15 14:53:39 +01:00
/**
* Entities with LookAt component looks to targetPosition (default up is 0,1,0)
* @param graphics GameLib.D3.Graphics
* @param apiLookAt GameLib.D3.API.LookAt
* @constructor
*/
2017-01-06 16:53:53 +01:00
GameLib.D3.LookAt = function (
2016-12-15 14:53:39 +01:00
graphics,
apiLookAt
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiLookAt)) {
apiLookAt = {};
}
2016-12-15 14:53:39 +01:00
GameLib.D3.API.LookAt.call(
this,
2016-12-19 17:44:15 +01:00
apiLookAt.id,
apiLookAt.name,
apiLookAt.currentComponent,
apiLookAt.targetComponent,
apiLookAt.targetPositionOffset,
2017-01-02 17:05:40 +01:00
apiLookAt.rotationSpeed,
apiLookAt.parentEntity
2016-12-15 14:53:39 +01:00
);
2016-12-19 17:44:15 +01:00
this.targetPositionOffset = new GameLib.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.targetPositionOffset,
this
2016-12-15 14:53:39 +01:00
);
this.lookAtMatrix = new GameLib.Matrix4(
this.graphics,
this.lookAtMatrix,
this
2016-12-15 14:53:39 +01:00
);
this.up = new GameLib.Vector3(
this.graphics,
this.up,
this
2016-12-15 14:53:39 +01:00
);
this.currentRotation = new GameLib.Quaternion(
this.graphics,
this.currentRotation,
this
2016-12-15 14:53:39 +01:00
);
this.targetPosition = new GameLib.Vector3(
this.graphics,
this.targetPosition,
this
);
2016-12-15 14:53:39 +01:00
};
GameLib.D3.LookAt.prototype = Object.create(GameLib.D3.API.LookAt.prototype);
GameLib.D3.LookAt.prototype.constructor = GameLib.D3.LookAt;
2016-12-19 17:44:15 +01:00
GameLib.D3.LookAt.prototype.toApiComponent = function() {
var apiLookAt = new GameLib.D3.API.LookAt(
this.id,
this.name,
GameLib.Utils.IdOrNull(this.currentComponent),
GameLib.Utils.IdOrNull(this.targetComponent),
this.targetPositionOffset.toApiVector(),
2017-01-02 17:05:40 +01:00
this.rotationSpeed,
GameLib.Utils.IdOrNull(this.parentEntity)
2016-12-19 17:44:15 +01:00
);
return apiLookAt;
};
GameLib.D3.LookAt.FromObjectComponent = function(graphics, objectComponent) {
2017-01-06 16:53:53 +01:00
var apiLookAt = GameLib.D3.API.LookAt.FromObjectComponent(objectComponent);
2016-12-19 17:44:15 +01:00
return new GameLib.D3.LookAt(
graphics,
apiLookAt
);
};
/**
2017-01-06 16:53:53 +01:00
* Looks at using time
2016-12-19 17:44:15 +01:00
* @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();
2017-01-09 15:20:48 +01:00
// 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);
2017-01-09 15:20:48 +01:00
// 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();
}
2016-12-19 17:44:15 +01:00
};