r3-legacy/src/game-lib-d3-raycast-vehicle.js

178 lines
4.9 KiB
JavaScript
Raw Normal View History

/**
* 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,
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(
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));
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
}
);
};
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|*}
*/
2017-10-23 14:52:35 +02:00
if (GameLib.Utils.UndefinedOrNull(this.chassis)) {
throw new Error('no chassis');
}
2017-09-05 05:22:52 +02:00
2017-10-23 14:52:35 +02:00
if (GameLib.Utils.UndefinedOrNull(this.chassis.instance)) {
throw new Error('no chassis instance');
}
2017-09-05 05:22:52 +02:00
2017-10-23 14:52:35 +02:00
if (GameLib.Utils.UndefinedOrNull(this.parentWorld)) {
throw new Error('no parent world');
}
2017-09-05 05:22:52 +02:00
2017-10-23 14:52:35 +02:00
if (GameLib.Utils.UndefinedOrNull(this.parentWorld.instance)) {
throw new Error('no parent world instance');
2017-09-02 12:55:43 +02:00
}
2017-10-23 14:52:35 +02:00
this.instance = new CANNON.RaycastVehicle({
chassisBody: this.chassis.instance
});
this.raycastWheels.map(
function(wheel){
if (GameLib.Utils.UndefinedOrNull(wheel)) {
throw new Error('no wheel');
}
if (GameLib.Utils.UndefinedOrNull(wheel.instance)) {
throw new Error('no wheel instance');
}
this.instance.addWheel(wheel.instance);
}.bind(this)
);
this.instance.addToWorld(this.parentWorld.instance);
GameLib.Component.prototype.createInstance.call(this);
};
GameLib.D3.RaycastVehicle.prototype.updateInstance = function() {
2017-09-05 05:22:52 +02:00
// this.instance.chassisBody = this.chassis.instance;
//TODO: add / remove wheels?
2017-09-05 05:22:52 +02:00
console.log('TODO: update raycast vehicle instance');
};
/**
* 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),
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
);
};