vehicle controls.
todo: + get REAL scaled radius of wheelMesh. + create input class.beta.r3js.org
parent
c38d302407
commit
a830c0502f
|
@ -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);
|
||||
|
||||
}
|
||||
};
|
|
@ -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(
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue