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

124 lines
3.1 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,
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){
if (wheel instanceof GameLib.D3.API.RaycastWheel) {
return new GameLib.D3.RaycastWheel(
this.physics,
wheel
)
} else {
return wheel;
}
}.bind(this));
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_RAYCAST_VEHICLE,
{
'chassis' : GameLib.D3.RigidBody,
'wheels' : [GameLib.D3.RaycastWheel]
}
);
};
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() {
var instance = new CANNON.RaycastVehicle({
chassisBody: this.chassis.instance
});
//TODO: maybe move this into linking system - ALSO - store a reference to the wheel somehow to update the wheel instance?
this.wheels.map(
function(wheel){
instance.addWheel(wheel.instance);
}
);
return instance;
};
GameLib.D3.RaycastVehicle.prototype.updateInstance = function() {
this.instance.chassisBody = this.chassis.instance;
//TODO: add / remove wheels?
console.log('TODO: add/remove wheels?');
};
/**
* 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);
}),
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
);
};