r3-legacy/bak/game-lib-d3-path-following.js

257 lines
6.7 KiB
JavaScript
Raw Normal View History

2016-12-15 14:53:39 +01:00
/**
* This component makes the parentEntity (ex. car) follow the path provided by the spline
* @param graphics GameLib.GraphicsRuntime
2016-12-15 14:53:39 +01:00
* @param apiPathFollowing GameLib.D3.API.PathFollowing
* @constructor
*/
2017-01-06 16:53:53 +01:00
GameLib.D3.PathFollowing = function (
2016-12-15 14:53:39 +01:00
graphics,
apiPathFollowing
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiPathFollowing)) {
apiPathFollowing = {};
}
if (apiPathFollowing instanceof GameLib.D3.PathFollowing) {
return apiPathFollowing;
}
2016-12-15 14:53:39 +01:00
GameLib.D3.API.PathFollowing.call(
this,
apiPathFollowing.id,
apiPathFollowing.name,
apiPathFollowing.spline,
2016-12-19 17:44:15 +01:00
apiPathFollowing.mesh,
apiPathFollowing.raytraceMesh,
2016-12-15 14:53:39 +01:00
apiPathFollowing.accelleration,
apiPathFollowing.maxSpeed,
apiPathFollowing.baseOffset,
apiPathFollowing.maxOffset,
apiPathFollowing.steeringSpeed,
apiPathFollowing.targetOffset,
apiPathFollowing.currentOffset,
apiPathFollowing.currentPathValue,
apiPathFollowing.currentSpeed,
apiPathFollowing.direction,
apiPathFollowing.raycaster,
apiPathFollowing.currentPosition,
apiPathFollowing.futurePosition,
apiPathFollowing.up,
apiPathFollowing.rotationMatrix,
2017-01-02 17:05:40 +01:00
apiPathFollowing.rotationVector,
apiPathFollowing.parentEntity
2016-12-15 14:53:39 +01:00
);
this.baseOffset = new GameLib.Vector3(
this.graphics,
this.baseOffset,
this
2016-12-15 14:53:39 +01:00
);
this.maxOffset = new GameLib.Vector3(
this.graphics,
this.maxOffset,
this
2016-12-15 14:53:39 +01:00
);
this.targetOffset = new GameLib.Vector3(
this.graphics,
this.targetOffset,
this
2016-12-15 14:53:39 +01:00
);
this.currentOffset = new GameLib.Vector3(
this.graphics,
this.currentOffset,
this
2016-12-15 14:53:39 +01:00
);
this.raycaster = new GameLib.D3.Raycaster(
this.graphics,
this.raycaster
);
this.currentPosition = new GameLib.Vector3(
this.graphics,
this.currentPosition,
this
2016-12-15 14:53:39 +01:00
);
this.futurePosition = new GameLib.Vector3(
this.graphics,
this.futurePosition,
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.rotationMatrix = new GameLib.Matrix4(
this.graphics,
this.rotationMatrix,
this
2016-12-15 14:53:39 +01:00
);
this.rotationVector = new GameLib.Quaternion(
this.graphics,
this.rotationVector,
this
2016-12-15 14:53:39 +01:00
);
2017-01-03 18:15:03 +01:00
this.mx = new GameLib.Utils.MovingAverage(10);
this.my = new GameLib.Utils.MovingAverage(10);
this.mz = new GameLib.Utils.MovingAverage(10);
2017-06-16 15:49:53 +02:00
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_PATH_FOLLOWING,
{
'spline': GameLib.D3.Spline,
'mesh' : GameLib.D3.Mesh,
'raytraceMesh' : GameLib.D3.Mesh
}
);
2016-12-15 14:53:39 +01:00
};
GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype);
GameLib.D3.PathFollowing.prototype.constructor = GameLib.D3.PathFollowing;
2017-06-16 15:49:53 +02:00
GameLib.D3.PathFollowing.prototype.createInstance = function() {
2017-06-16 18:45:25 +02:00
console.log('GameLib.D3.PathFollowing.prototype.createInstance()');
2017-10-23 14:52:35 +02:00
GameLib.Component.prototype.createInstance.call(this);
2017-06-16 18:45:25 +02:00
2017-06-16 15:49:53 +02:00
};
2017-05-16 14:51:57 +02:00
GameLib.D3.PathFollowing.prototype.toApiObject = function() {
var apiPathFollowing = new GameLib.D3.API.PathFollowing(
this.id,
this.name,
2016-12-19 17:44:15 +01:00
GameLib.Utils.IdOrNull(this.spline),
GameLib.Utils.IdOrNull(this.mesh),
GameLib.Utils.IdOrNull(this.raytraceMesh),
this.accelleration,
this.maxSpeed,
2017-05-16 14:51:57 +02:00
this.baseOffset.toApiObject(),
this.maxOffset.toApiObject(),
this.steeringSpeed,
2017-05-16 14:51:57 +02:00
this.targetOffset.toApiObject(),
this.currentOffset.toApiObject(),
this.currentPathValue,
this.currentSpeed,
this.direction,
2017-05-16 14:51:57 +02:00
this.raycaster.toApiObject(),
this.currentPosition.toApiObject(),
this.futurePosition.toApiObject(),
this.up.toApiObject(),
this.rotationMatrix.toApiObject(),
this.rotationVector.toApiObject(),
2017-01-02 17:05:40 +01:00
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiPathFollowing;
};
2017-01-06 16:53:53 +01:00
/**
* Object path following to GameLib.D3.PathFollowing
* @param graphics
* @param objectComponent
* @returns {GameLib.D3.PathFollowing}
* @constructor
*/
2017-06-14 14:21:57 +02:00
GameLib.D3.PathFollowing.FromObject = function(graphics, objectComponent) {
2017-06-14 14:21:57 +02:00
var apiPathFollowing = GameLib.D3.API.PathFollowing.FromObject(objectComponent);
return new GameLib.D3.PathFollowing(
graphics,
apiPathFollowing
);
2016-12-19 17:44:15 +01:00
};
/**
* Updates the component
* @param deltaTime
*/
GameLib.D3.PathFollowing.prototype.update = function(deltaTime) {
if (this.spline && this.mesh && this.raytraceMesh) {
this.currentSpeed += this.accelleration * deltaTime * this.direction;
if(this.currentSpeed > this.maxSpeed) {
this.currentSpeed = this.maxSpeed;
}
this.grain = (this.currentSpeed / 100.0);
2017-01-03 18:15:03 +01:00
var currentPosition = this.spline.getPointAt(this.currentPathValue);
this.currentPosition.x = currentPosition.x;
this.currentPosition.y = currentPosition.y;
this.currentPosition.z = currentPosition.z;
2016-12-19 17:44:15 +01:00
this.currentPathValue += this.grain;
if (this.currentPathValue >= 1) {
this.currentPathValue = this.currentPathValue - 1;
}
if (this.currentPathValue < 0) {
this.currentPathValue = 0.0;
}
2017-01-03 18:15:03 +01:00
var futurePosition = this.spline.getPointAt(this.currentPathValue);
this.futurePosition.x = futurePosition.x;
this.futurePosition.y = futurePosition.y;
this.futurePosition.z = futurePosition.z;
2016-12-19 17:44:15 +01:00
this.raycaster.setPosition(
2017-01-04 16:12:30 +01:00
this.currentPosition
2017-01-03 18:15:03 +01:00
);
this.raycaster.setDirection(
2017-01-04 16:12:30 +01:00
{
x : -this.up.x,
y : -this.up.y,
z : -this.up.z
}
2017-01-03 18:15:03 +01:00
);
2016-12-19 17:44:15 +01:00
var normal = this.raycaster.getFaceNormal(this.raytraceMesh);
if (normal) {
this.up.x = this.mx(normal.x);
this.up.y = this.my(normal.y);
this.up.z = this.mz(normal.z);
}
this.rotationMatrix.lookAt(
this.currentPosition,
this.futurePosition,
this.up
);
this.rotationVector.setFromRotationMatrix(this.rotationMatrix);
2017-01-09 15:20:48 +01:00
this.mesh.position.x = this.futurePosition.x;
this.mesh.position.y = this.futurePosition.y;
this.mesh.position.z = this.futurePosition.z;
2016-12-19 17:44:15 +01:00
/**
* Update Rotation
*/
this.mesh.quaternion.x = this.rotationVector.x;
this.mesh.quaternion.y = this.rotationVector.y;
this.mesh.quaternion.z = this.rotationVector.z;
this.mesh.quaternion.w = this.rotationVector.w;
}
};