/** * RaycastVehicle Runtime * @param physics R3.GraphicsRuntime * @param apiRaycastVehicle R3.D3.API.RaycastVehicle * @constructor */ R3.D3.RaycastVehicle = function ( physics, apiRaycastVehicle ) { this.physics = physics; this.physics.isNotCannonThrow(); if (R3.Utils.UndefinedOrNull(apiRaycastVehicle)) { apiRaycastVehicle = {}; } R3.D3.API.RaycastVehicle.call( this, apiRaycastVehicle.id, apiRaycastVehicle.name, apiRaycastVehicle.chassis, apiRaycastVehicle.wheels, apiRaycastVehicle.raycastWheels, apiRaycastVehicle.parentPhysicsWorld, apiRaycastVehicle.parentEntity ); if (this.chassis instanceof R3.D3.API.RaycastVehicle) { this.chassis = new R3.D3.RaycastVehicle( this.physics, this.chassis ) } this.wheels = this.wheels.map(function(wheel){ if (wheel instanceof R3.D3.API.RigidBody) { return new R3.D3.RigidBody( this.physics, wheel ) } else { return wheel; } }.bind(this)); this.raycastWheels = this.raycastWheels.map(function(raycastWheel){ if (raycastWheel instanceof R3.D3.API.RaycastWheel) { return new R3.D3.RaycastWheel( this.physics, raycastWheel ) } else { return raycastWheel; } }.bind(this)); R3.Component.call( this, { 'chassis' : R3.D3.RigidBody, 'wheels' : [R3.D3.RigidBody], 'raycastWheels' : [R3.D3.RaycastWheel], 'parentPhysicsWorld' : R3.D3.PhysicsWorld } ); }; R3.D3.RaycastVehicle.prototype = Object.create(R3.Component.prototype); R3.D3.RaycastVehicle.prototype.constructor = R3.D3.RaycastVehicle; /** * * @returns {*} */ R3.D3.RaycastVehicle.prototype.createInstance = function() { /** * 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 {R3.D3.RaycastVehicle|R3.D3.API.RaycastVehicle|*} */ if (R3.Utils.UndefinedOrNull(this.chassis)) { throw new Error('no chassis'); } if (R3.Utils.UndefinedOrNull(this.chassis.instance)) { throw new Error('no chassis instance'); } if (R3.Utils.UndefinedOrNull(this.parentPhysicsWorld)) { throw new Error('no parent world'); } if (R3.Utils.UndefinedOrNull(this.parentPhysicsWorld.instance)) { throw new Error('no parent world instance'); } this.instance = new CANNON.RaycastVehicle({ chassisBody: this.chassis.instance }); this.raycastWheels.map( function(wheel){ if (R3.Utils.UndefinedOrNull(wheel)) { throw new Error('no wheel'); } if (R3.Utils.UndefinedOrNull(wheel.instance)) { throw new Error('no wheel instance'); } this.instance.addWheel(wheel.instance); }.bind(this) ); this.instance.addToWorld(this.parentPhysicsWorld.instance); R3.Component.prototype.createInstance.call(this); }; R3.D3.RaycastVehicle.prototype.updateInstance = function(property) { // this.instance.chassisBody = this.chassis.instance; //TODO: add / remove wheels? console.log('TODO: update raycast vehicle instance'); R3.Component.prototype.updateInstance.call(this, property); }; /** * R3.D3.RaycastVehicle to R3.D3.API.RaycastVehicle * @returns {R3.D3.API.RaycastVehicle} */ R3.D3.RaycastVehicle.prototype.toApiObject = function() { var apiRaycastVehicle = new R3.D3.API.RaycastVehicle( this.id, this.name, R3.Utils.IdOrNull(this.chassis), this.wheels.map(function(wheel){ return R3.Utils.IdOrNull(wheel); }), this.raycastWheels.map(function(raycastWheel){ return R3.Utils.IdOrNull(raycastWheel); }), R3.Utils.IdOrNull(this.parentPhysicsWorld), R3.Utils.IdOrNull(this.parentEntity) ); return apiRaycastVehicle; }; /** * R3.D3.RaycastVehicle from Object RaycastVehicle * @param physics * @param objectComponent * @returns {R3.D3.RaycastVehicle} * @constructor */ R3.D3.RaycastVehicle.FromObject = function(physics, objectComponent) { var apiRaycastVehicle = R3.D3.API.RaycastVehicle.FromObject(objectComponent); return new R3.D3.RaycastVehicle( physics, apiRaycastVehicle ); };