2016-11-03 11:57:05 +01:00
|
|
|
GameLib.D3.ComponentRaycastVehicleControls = function(
|
2016-11-04 10:58:48 +01:00
|
|
|
componentId,
|
|
|
|
frontLWheelIndex,
|
|
|
|
frontRWheelIndex,
|
|
|
|
backLWheelIndex,
|
|
|
|
backRWheelIndex,
|
|
|
|
maxForce,
|
|
|
|
steering
|
2016-11-03 11:57:05 +01:00
|
|
|
) {
|
|
|
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
|
|
|
this.parentEntity = null;
|
|
|
|
|
2016-11-04 10:58:48 +01:00
|
|
|
// 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;
|
|
|
|
|
2016-11-03 11:57:05 +01:00
|
|
|
// 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 //////////////////////////
|
2016-11-04 10:58:48 +01:00
|
|
|
GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity = function(
|
|
|
|
parentScene,
|
2016-11-03 11:57:05 +01:00
|
|
|
parentEntity
|
|
|
|
) {
|
|
|
|
|
2016-11-04 10:58:48 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
2016-11-03 11:57:05 +01:00
|
|
|
};
|