From a830c0502faf2eeb52d8113b21eafe83e77a700b Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Fri, 4 Nov 2016 10:58:48 +0100 Subject: [PATCH] vehicle controls. todo: + get REAL scaled radius of wheelMesh. + create input class. --- ...-lib-component-raycast-vehicle-controls.js | 86 ++++++++++++++++--- src/game-lib-entity.js | 14 +++ src/game-lib-raycast-vehicle.js | 21 ++++- 3 files changed, 107 insertions(+), 14 deletions(-) diff --git a/src/game-lib-component-raycast-vehicle-controls.js b/src/game-lib-component-raycast-vehicle-controls.js index 411adb6..84d0317 100644 --- a/src/game-lib-component-raycast-vehicle-controls.js +++ b/src/game-lib-component-raycast-vehicle-controls.js @@ -1,24 +1,90 @@ GameLib.D3.ComponentRaycastVehicleControls = function( - componentId + componentId, + frontLWheelIndex, + frontRWheelIndex, + backLWheelIndex, + backRWheelIndex, + maxForce, + steering ) { this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.parentEntity = null; + // maybe we pass this in the constructor + this.raycastVehicleComponent = null; + this.frontLWheelIndex = frontLWheelIndex || 0; + this.frontRWheelIndex = frontRWheelIndex || 1; + this.backLWheelIndex = backLWheelIndex || 2; + this.backRWheelIndex = backRWheelIndex || 3; + + this.maxForce = maxForce || 400; + this.steering = steering || 0.5; + // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentRaycastVehicleControls, GameLib.D3.ComponentInterface); }; ///////////////////////// Methods to override ////////////////////////// -GameLib.D3.ComponentRaycastVehicleControls.prototype.onUpdate = function( - deltaTime, +GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity = function( + parentScene, parentEntity ) { - this.parentEntity.mesh.material.color = new THREE.Color(Math.random(), Math.random(), Math.random()); -}; -GameLib.D3.ComponentRaycastVehicleControls.prototype.onRegistered = function( - parentScene -) { - // Hook in document.eventlisteners - // save values & in the update method we just apply them on the vehicle itself + console.log("Set parent!"); + + this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle); + if(!this.raycastVehicleComponent) { + console.warn("NO RAYCAST VEHICLE FOUND!"); + } else { + + var component = this; + document.addEventListener('keydown', function(event) { + + if (event.keyCode == 73) { // Forward [i] + + component.raycastVehicleComponent.instance.applyEngineForce(-component.maxForce, component.backLWheelIndex); + component.raycastVehicleComponent.instance.applyEngineForce(-component.maxForce, component.backRWheelIndex); + + } else if (event.keyCode == 74) { // Left [j] + + component.raycastVehicleComponent.instance.setSteeringValue(component.steering, component.frontLWheelIndex); + component.raycastVehicleComponent.instance.setSteeringValue(component.steering, component.frontRWheelIndex); + + } else if (event.keyCode == 75) { // Back [k] + + component.raycastVehicleComponent.instance.applyEngineForce(component.maxForce, component.backLWheelIndex); + component.raycastVehicleComponent.instance.applyEngineForce(component.maxForce, component.backRWheelIndex); + + } else if (event.keyCode == 76) { // Right [l] + component.raycastVehicleComponent.instance.setSteeringValue(-component.steering, component.frontLWheelIndex); + component.raycastVehicleComponent.instance.setSteeringValue(-component.steering, component.frontRWheelIndex); + } + + }, false); + + document.addEventListener('keyup', function(event) { + + if (event.keyCode == 73) { // Forward [i] + + component.raycastVehicleComponent.instance.applyEngineForce(0, component.backLWheelIndex); + component.raycastVehicleComponent.instance.applyEngineForce(0, component.backRWheelIndex); + + } else if (event.keyCode == 74) { // Left [j] + + component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontLWheelIndex); + component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontRWheelIndex); + + } else if (event.keyCode == 75) { // Back [k] + + component.raycastVehicleComponent.instance.applyEngineForce(0, component.backLWheelIndex); + component.raycastVehicleComponent.instance.applyEngineForce(0, component.backRWheelIndex); + + } else if (event.keyCode == 76) { // Right [l] + component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontLWheelIndex); + component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontRWheelIndex); + } + + }, false); + + } }; \ No newline at end of file diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 75fc2a4..60f745a 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -105,6 +105,20 @@ GameLib.D3.Entity.prototype.addComponent = function( }; +GameLib.D3.Entity.prototype.getComponent = function( + componentType +) { + for(var c in this.componentIds) { + var componentId = this.componentIds[c]; + var component = this.parentScene.componentIdToComponent[componentId]; + if (component instanceof componentType) { + return component; + } + } + + return null; +}; + ///////////////////////// Methods to override ////////////////////////// GameLib.D3.Entity.prototype.onUpdate = function( diff --git a/src/game-lib-raycast-vehicle.js b/src/game-lib-raycast-vehicle.js index bfbcf2c..f84ce77 100644 --- a/src/game-lib-raycast-vehicle.js +++ b/src/game-lib-raycast-vehicle.js @@ -8,7 +8,8 @@ GameLib.D3.RaycastVehicle = function( engine, chassisBody, - wheels + wheels, + wheelBodies ) { this.engine = engine; this.engine.isNotCannonThrow(); @@ -22,6 +23,11 @@ GameLib.D3.RaycastVehicle = function( } this.wheels = wheels; + if(GameLib.D3.Utils.UndefinedOrNull(wheelBodies)) { + wheelBodies = []; + } + this.wheelBodies = wheelBodies; + this.instance = this.createInstance(); GameLib.D3.Utils.Extend(GameLib.D3.RaycastVehicle, GameLib.D3.ComponentInterface); @@ -40,11 +46,14 @@ GameLib.D3.RaycastVehicle.prototype.createInstance = function() { /** * Adds a raycast wheel to this vehicle * @param wheel GameLib.D3.RaycastWheel + * @param wheelRigidBody GameLib.D3.RigidBody */ GameLib.D3.RaycastVehicle.prototype.addWheel = function ( - wheel + wheel, + wheelRigidBody ) { this.wheels.push(wheel); + this.wheelBodies.push(wheelRigidBody); wheel.wheelIndex = this.instance.addWheel(wheel.instance); }; @@ -64,9 +73,13 @@ GameLib.D3.RaycastVehicle.prototype.onUpdate = function( deltaTime, parentEntity ) { - /*for (var i = 0; i < this.getWheelInfo().length; i++) { + for (var i = 0; i < this.getWheelInfo().length; i++) { this.instance.updateWheelTransform(i); - }*/ + var t = this.getWheelInfo()[i].worldTransform; + var wheelBody = this.wheelBodies[i].instance; + wheelBody.position.copy(t.position); + wheelBody.quaternion.copy(t.quaternion); + } }; GameLib.D3.RaycastVehicle.prototype.onRegistered = function(