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