r3-legacy/src/game-lib-component-raycast-...

90 lines
3.8 KiB
JavaScript
Raw Normal View History

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;
// 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.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.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);
}
};