2017-09-01 18:19:17 +02:00
|
|
|
/**
|
|
|
|
* RaycastVehicle Runtime
|
|
|
|
* @param physics GameLib.D3.Graphics
|
|
|
|
* @param apiRaycastVehicle GameLib.D3.API.RaycastVehicle
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.RaycastVehicle = function (
|
|
|
|
physics,
|
|
|
|
apiRaycastVehicle
|
|
|
|
) {
|
|
|
|
|
|
|
|
this.physics = physics;
|
|
|
|
this.physics.isNotCannonThrow();
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiRaycastVehicle)) {
|
|
|
|
apiRaycastVehicle = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiRaycastVehicle instanceof GameLib.D3.RaycastVehicle) {
|
|
|
|
return apiRaycastVehicle;
|
|
|
|
}
|
|
|
|
|
|
|
|
GameLib.D3.API.RaycastVehicle.call(
|
|
|
|
this,
|
|
|
|
apiRaycastVehicle.id,
|
|
|
|
apiRaycastVehicle.name,
|
|
|
|
apiRaycastVehicle.chassis,
|
|
|
|
apiRaycastVehicle.wheels,
|
2017-09-05 05:22:52 +02:00
|
|
|
apiRaycastVehicle.raycastWheels,
|
|
|
|
apiRaycastVehicle.parentWorld,
|
2017-09-01 18:19:17 +02:00
|
|
|
apiRaycastVehicle.parentEntity
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.chassis instanceof GameLib.D3.API.RaycastVehicle) {
|
|
|
|
this.chassis = new GameLib.D3.RaycastVehicle(
|
|
|
|
this.physics,
|
|
|
|
this.chassis
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.wheels = this.wheels.map(function(wheel){
|
2017-09-05 05:22:52 +02:00
|
|
|
if (wheel instanceof GameLib.D3.API.RigidBody) {
|
|
|
|
return new GameLib.D3.RigidBody(
|
2017-09-01 18:19:17 +02:00
|
|
|
this.physics,
|
|
|
|
wheel
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return wheel;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
2017-09-05 05:22:52 +02:00
|
|
|
this.raycastWheels = this.raycastWheels.map(function(raycastWheel){
|
|
|
|
if (raycastWheel instanceof GameLib.D3.API.RaycastWheel) {
|
|
|
|
return new GameLib.D3.RaycastWheel(
|
|
|
|
this.physics,
|
|
|
|
raycastWheel
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return raycastWheel;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
2017-09-01 18:19:17 +02:00
|
|
|
GameLib.Component.call(
|
|
|
|
this,
|
|
|
|
GameLib.Component.COMPONENT_RAYCAST_VEHICLE,
|
|
|
|
{
|
2017-09-05 05:22:52 +02:00
|
|
|
'chassis' : GameLib.D3.RigidBody,
|
|
|
|
'wheels' : [GameLib.D3.RigidBody],
|
|
|
|
'raycastWheels' : [GameLib.D3.RaycastWheel],
|
|
|
|
'parentWorld' : GameLib.D3.PhysicsWorld
|
2017-09-01 18:19:17 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.RaycastVehicle.prototype = Object.create(GameLib.D3.API.RaycastVehicle.prototype);
|
|
|
|
GameLib.D3.RaycastVehicle.prototype.constructor = GameLib.D3.RaycastVehicle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
GameLib.D3.RaycastVehicle.prototype.createInstance = function() {
|
|
|
|
|
2017-09-02 12:55:43 +02:00
|
|
|
/**
|
|
|
|
* At this point - even though this component exists - the chassis could maybe not been have assigned, failed to
|
|
|
|
* register as a dependency, and therefore is not present at the time of createInstance() - we will need to call
|
|
|
|
* delayedInstance somehow...
|
|
|
|
* @type {GameLib.D3.RaycastVehicle|GameLib.D3.API.RaycastVehicle|*}
|
|
|
|
*/
|
|
|
|
var instance = false;
|
2017-09-01 18:19:17 +02:00
|
|
|
|
2017-09-02 12:55:43 +02:00
|
|
|
if (this.chassis && this.chassis.instance) {
|
2017-09-05 05:22:52 +02:00
|
|
|
|
|
|
|
instance = new CANNON.RaycastVehicle({
|
2017-09-02 12:55:43 +02:00
|
|
|
chassisBody: this.chassis.instance
|
|
|
|
});
|
2017-09-05 05:22:52 +02:00
|
|
|
|
|
|
|
this.raycastWheels.map(function(wheel){
|
|
|
|
instance.addWheel(wheel.instance);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.parentWorld && this.parentWorld.instance) {
|
|
|
|
instance.addToWorld(this.parentWorld.instance);
|
|
|
|
}
|
2017-09-02 12:55:43 +02:00
|
|
|
}
|
2017-09-01 18:19:17 +02:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.RaycastVehicle.prototype.updateInstance = function() {
|
2017-09-05 05:22:52 +02:00
|
|
|
// this.instance.chassisBody = this.chassis.instance;
|
2017-09-01 18:19:17 +02:00
|
|
|
//TODO: add / remove wheels?
|
2017-09-05 05:22:52 +02:00
|
|
|
console.log('TODO: update raycast vehicle instance');
|
2017-09-01 18:19:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GameLib.D3.RaycastVehicle to GameLib.D3.API.RaycastVehicle
|
|
|
|
* @returns {GameLib.D3.API.RaycastVehicle}
|
|
|
|
*/
|
|
|
|
GameLib.D3.RaycastVehicle.prototype.toApiObject = function() {
|
|
|
|
|
|
|
|
var apiRaycastVehicle = new GameLib.D3.API.RaycastVehicle(
|
|
|
|
this.id,
|
|
|
|
this.name,
|
|
|
|
GameLib.Utils.IdOrNull(this.chassis),
|
|
|
|
this.wheels.map(function(wheel){
|
|
|
|
return GameLib.Utils.IdOrNull(wheel);
|
|
|
|
}),
|
2017-09-05 05:22:52 +02:00
|
|
|
this.raycastWheels.map(function(raycastWheel){
|
|
|
|
return GameLib.Utils.IdOrNull(raycastWheel);
|
|
|
|
}),
|
|
|
|
GameLib.Utils.IdOrNull(this.parentWorld),
|
2017-09-01 18:19:17 +02:00
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
|
|
|
);
|
|
|
|
|
|
|
|
return apiRaycastVehicle;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GameLib.D3.RaycastVehicle from Object RaycastVehicle
|
|
|
|
* @param physics
|
|
|
|
* @param objectComponent
|
|
|
|
* @returns {GameLib.D3.RaycastVehicle}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.RaycastVehicle.FromObject = function(physics, objectComponent) {
|
|
|
|
|
|
|
|
var apiRaycastVehicle = GameLib.D3.API.RaycastVehicle.FromObject(objectComponent);
|
|
|
|
|
|
|
|
return new GameLib.D3.RaycastVehicle(
|
|
|
|
physics,
|
|
|
|
apiRaycastVehicle
|
|
|
|
);
|
|
|
|
};
|