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

183 lines
5.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;
2016-11-11 10:10:43 +01:00
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 //////////////////////////
2016-11-11 10:10:43 +01:00
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;
2016-11-11 10:10:43 +01:00
document.addEventListener('keydown', function(event) {
if (event.keyCode == 73) { // Forward [i]
2016-11-11 10:10:43 +01:00
component.keyForwardPressed = true;
2016-11-11 10:10:43 +01:00
} else if (event.keyCode == 75) { // Back [k]
2016-11-11 10:10:43 +01:00
component.keyBackPressed = true;
2016-11-11 10:10:43 +01:00
}
if (event.keyCode == 74) { // Left [j]
2016-11-11 10:10:43 +01:00
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]
2016-11-11 10:10:43 +01:00
component.keyForwardPressed = false;
2016-11-11 10:10:43 +01:00
} else if (event.keyCode == 75) { // Back [k]
2016-11-11 10:10:43 +01:00
component.keyBackPressed = false;
2016-11-11 10:10:43 +01:00
}
if (event.keyCode == 74) { // Left [j]
component.keyLeftPressed = false;
} else if (event.keyCode == 76) { // Right [l]
component.keyRightPressed = false;
}
if (event.keyCode == 66) {
2016-11-11 10:10:43 +01:00
component.keyBreakPressed = false;
}
}, false);
}
};