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

155 lines
4.1 KiB
JavaScript

/**
* Runtime Follow Component
* @param graphics R3.D3.Graphics
* @param apiFollow
* @constructor
*/
R3.D3.Follow = function (
graphics,
apiFollow
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (R3.Utils.UndefinedOrNull(apiFollow)) {
apiFollow = {};
}
if (apiFollow instanceof R3.D3.Follow) {
return apiFollow;
}
R3.D3.API.Follow.call(
this,
apiFollow.id,
apiFollow.name,
apiFollow.currentComponent,
apiFollow.targetComponent,
apiFollow.targetPositionOffset,
apiFollow.minDistance,
apiFollow.moveSpeed,
apiFollow.parentEntity
);
this.targetPositionOffset = new R3.Vector3(
this.graphics,
this.targetPositionOffset,
this
);
this.target = new R3.Vector3(
this.graphics,
this.target,
this
);
this.targetToParent = new R3.Vector3(
this.graphics,
this.targetToParent,
this
);
this.rotatedTargetOffset = new R3.Vector3(
this.graphics,
this.rotatedTargetOffset,
this
);
this.rotated = new R3.Quaternion(
this.graphics,
this.rotated,
this
);
this.buildIdToObject();
};
R3.D3.Follow.prototype = Object.create(R3.D3.API.Follow.prototype);
R3.D3.Follow.prototype.constructor = R3.D3.Follow;
R3.D3.Follow.prototype.toApiObject = function() {
var apiFollow = new R3.D3.API.Follow(
this.id,
this.name,
R3.Utils.IdOrNull(this.currentComponent),
R3.Utils.IdOrNull(this.targetComponent),
this.targetPositionOffset.toApiObject(),
this.minDistance,
this.moveSpeed,
R3.Utils.IdOrNull(this.parentEntity)
);
return apiFollow;
};
R3.D3.Follow.FromObject = function(graphics, objectComponent) {
var apiFollow = R3.D3.API.Follow.FromObject(objectComponent);
return new R3.D3.Follow(
graphics,
apiFollow
);
};
/**
* Updates the component
* @param deltaTime
*/
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;
// 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);
// 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;
// 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;
// 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;
// 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;
}
};