GameLib.D3.ComponentRaycastVehicleControls = function( 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; this.brakeForce = 1000000; this.currentSteering = 0; this.steeringSpeed = 8.0; this.keyLeftPressed = false; this.keyRightPressed = false; this.keyForwardPressed = false; this.keyBackPressed = false; this.keyBreakPressed = false; // 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, parentEntity ) { if (this.keyForwardPressed) { // Forward [i] this.raycastVehicleComponent.instance.applyEngineForce(-this.maxForce, this.backLWheelIndex); this.raycastVehicleComponent.instance.applyEngineForce(-this.maxForce, this.backRWheelIndex); } else if (this.keyBackPressed) { // Back [k] this.raycastVehicleComponent.instance.applyEngineForce(this.maxForce, this.backLWheelIndex); this.raycastVehicleComponent.instance.applyEngineForce(this.maxForce, this.backRWheelIndex); } else if (!this.keyForwardPressed && !this.keyBackPressed) { this.raycastVehicleComponent.instance.applyEngineForce(0, this.backLWheelIndex); this.raycastVehicleComponent.instance.applyEngineForce(0, this.backRWheelIndex); } if (this.keyLeftPressed) { // Left [j] var t = deltaTime * this.steeringSpeed; t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); this.currentSteering = this.currentSteering + (this.steering - this.currentSteering) * t; this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex); this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex); } else if (this.keyRightPressed) { // Right [l] var t = deltaTime * this.steeringSpeed; t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); this.currentSteering = this.currentSteering + ((-this.steering) - this.currentSteering) * t; this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex); this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex); } else if (!this.keyLeftPressed && !this.keyRightPressed) { var t = deltaTime * this.steeringSpeed * 2; t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); this.currentSteering = this.currentSteering + (0 - this.currentSteering) * t; this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontLWheelIndex); this.raycastVehicleComponent.instance.setSteeringValue(this.currentSteering, this.frontRWheelIndex); } if(this.keyBreakPressed) { this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.frontLWheelIndex); this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.frontRWheelIndex); this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.backLWheelIndex); this.raycastVehicleComponent.instance.setBrake(this.brakeForce, this.backRWheelIndex); } else { this.raycastVehicleComponent.instance.setBrake(0, this.frontLWheelIndex); this.raycastVehicleComponent.instance.setBrake(0, this.frontRWheelIndex); this.raycastVehicleComponent.instance.setBrake(0, this.backLWheelIndex); this.raycastVehicleComponent.instance.setBrake(0, this.backRWheelIndex); } }; GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity = function( parentScene, parentEntity ) { 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.keyForwardPressed = true; } else if (event.keyCode == 75) { // Back [k] component.keyBackPressed = true; } if (event.keyCode == 74) { // Left [j] component.keyLeftPressed = true; } else if (event.keyCode == 76) { // Right [l] component.keyRightPressed = true; } if (event.keyCode == 66) { component.keyBreakPressed = true; } }, false); document.addEventListener('keyup', function(event) { if (event.keyCode == 73) { // Forward [i] component.keyForwardPressed = false; } else if (event.keyCode == 75) { // Back [k] component.keyBackPressed = false; } if (event.keyCode == 74) { // Left [j] component.keyLeftPressed = false; } else if (event.keyCode == 76) { // Right [l] component.keyRightPressed = false; } if (event.keyCode == 66) { component.keyBreakPressed = false; } }, false); } };