/** * RaycastVehicle Runtime * @param physics GameLib.GraphicsRuntime * @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 = {}; } GameLib.D3.API.RaycastVehicle.call( this, apiRaycastVehicle.id, apiRaycastVehicle.name, apiRaycastVehicle.chassis, apiRaycastVehicle.wheels, apiRaycastVehicle.raycastWheels, apiRaycastVehicle.parentPhysicsWorld, 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.RigidBody) { return new GameLib.D3.RigidBody( this.physics, wheel ) } else { return wheel; } }.bind(this)); 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, { 'chassis' : GameLib.D3.RigidBody, 'wheels' : [GameLib.D3.RigidBody], 'raycastWheels' : [GameLib.D3.RaycastWheel], 'parentPhysicsWorld' : GameLib.D3.PhysicsWorld } ); }; GameLib.D3.RaycastVehicle.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.RaycastVehicle.prototype.constructor = GameLib.D3.RaycastVehicle; /** * * @returns {*} */ GameLib.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 {GameLib.D3.RaycastVehicle|GameLib.D3.API.RaycastVehicle|*} */ if (GameLib.Utils.UndefinedOrNull(this.chassis)) { throw new Error('no chassis'); } if (GameLib.Utils.UndefinedOrNull(this.chassis.instance)) { throw new Error('no chassis instance'); } if (GameLib.Utils.UndefinedOrNull(this.parentPhysicsWorld)) { throw new Error('no parent world'); } if (GameLib.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 (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.parentPhysicsWorld.instance); GameLib.Component.prototype.createInstance.call(this); }; GameLib.D3.RaycastVehicle.prototype.updateInstance = function() { // this.instance.chassisBody = this.chassis.instance; //TODO: add / remove wheels? 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); }), this.raycastWheels.map(function(raycastWheel){ return GameLib.Utils.IdOrNull(raycastWheel); }), GameLib.Utils.IdOrNull(this.parentPhysicsWorld), 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 ); };