r3-legacy/bak/r3-d3-follow.js

155 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2016-12-15 14:53:39 +01:00
/**
* Runtime Follow Component
2018-04-09 10:05:13 +02:00
* @param graphics R3.D3.Graphics
2016-12-15 14:53:39 +01:00
* @param apiFollow
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Follow = function (
2016-12-15 14:53:39 +01:00
graphics,
apiFollow
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiFollow)) {
2017-01-12 17:40:17 +01:00
apiFollow = {};
}
2018-04-09 10:05:13 +02:00
if (apiFollow instanceof R3.D3.Follow) {
return apiFollow;
}
2018-04-09 10:05:13 +02:00
R3.D3.API.Follow.call(
2016-12-15 14:53:39 +01:00
this,
apiFollow.id,
apiFollow.name,
apiFollow.currentComponent,
apiFollow.targetComponent,
apiFollow.targetPositionOffset,
apiFollow.minDistance,
2017-01-02 17:05:40 +01:00
apiFollow.moveSpeed,
apiFollow.parentEntity
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.targetPositionOffset = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.targetPositionOffset,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.target = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.target,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.targetToParent = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.targetToParent,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.rotatedTargetOffset = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.rotatedTargetOffset,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.rotated = new R3.Quaternion(
2016-12-15 14:53:39 +01:00
this.graphics,
this.rotated,
this
2016-12-15 14:53:39 +01:00
);
2017-01-19 17:50:11 +01:00
this.buildIdToObject();
2016-12-15 14:53:39 +01:00
};
2018-04-09 10:05:13 +02:00
R3.D3.Follow.prototype = Object.create(R3.D3.API.Follow.prototype);
R3.D3.Follow.prototype.constructor = R3.D3.Follow;
2018-04-09 10:05:13 +02:00
R3.D3.Follow.prototype.toApiObject = function() {
2018-04-09 10:05:13 +02:00
var apiFollow = new R3.D3.API.Follow(
this.id,
this.name,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.currentComponent),
R3.Utils.IdOrNull(this.targetComponent),
2017-05-16 14:51:57 +02:00
this.targetPositionOffset.toApiObject(),
this.minDistance,
2017-01-02 17:05:40 +01:00
this.moveSpeed,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.parentEntity)
);
return apiFollow;
};
2018-04-09 10:05:13 +02:00
R3.D3.Follow.FromObject = function(graphics, objectComponent) {
2018-04-09 10:05:13 +02:00
var apiFollow = R3.D3.API.Follow.FromObject(objectComponent);
2018-04-09 10:05:13 +02:00
return new R3.D3.Follow(
graphics,
apiFollow
);
};
/**
* Updates the component
* @param deltaTime
*/
2018-04-09 10:05:13 +02:00
R3.D3.Follow.prototype.update = function(deltaTime) {
if (this.currentComponent && this.targetComponent) {
this.rotated.x = this.targetComponent.quaternion.x;
this.rotated.y = this.targetComponent.quaternion.y;
this.rotated.z = this.targetComponent.quaternion.z;
this.rotated.w = this.targetComponent.quaternion.w;
2017-01-02 17:05:40 +01:00
// this.rotated.updateInstance();
this.rotatedTargetOffset.x = this.targetPositionOffset.x;
this.rotatedTargetOffset.y = this.targetPositionOffset.y;
this.rotatedTargetOffset.z = this.targetPositionOffset.z;
this.rotatedTargetOffset.applyQuaternion(this.rotated);
2017-01-02 17:05:40 +01:00
// this.rotatedTargetOffset.updateInstance();
this.target.x = this.targetComponent.position.x + this.rotatedTargetOffset.x;
this.target.y = this.targetComponent.position.y + this.rotatedTargetOffset.y;
this.target.z = this.targetComponent.position.z + this.rotatedTargetOffset.z;
2017-01-02 17:05:40 +01:00
// this.target.updateInstance();
this.targetToParent.x = this.currentComponent.position.x - this.targetComponent.position.x;
this.targetToParent.y = this.currentComponent.position.y - this.targetComponent.position.y;
this.targetToParent.z = this.currentComponent.position.z - this.targetComponent.position.z;
this.targetToParent.normalize();
this.targetToParent.x *= this.minDistance;
this.targetToParent.y *= this.minDistance;
this.targetToParent.z *= this.minDistance;
2017-01-02 17:05:40 +01:00
// this.targetToParent.updateInstance();
this.target.x = this.target.x + this.targetToParent.x;
this.target.y = this.target.y + this.targetToParent.y;
this.target.z = this.target.z + this.targetToParent.z;
2017-01-02 17:05:40 +01:00
// this.target.updateInstance();
var t = deltaTime * this.moveSpeed;
//t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
var lerp = this.currentComponent.position.lerp(this.target, t);
this.currentComponent.position.x = lerp.x;
this.currentComponent.position.y = lerp.y;
this.currentComponent.position.z = lerp.z;
}
};