r3-legacy/bak/r3-d3-input-drive.js

174 lines
5.2 KiB
JavaScript

/**
* Input parent class
* @param graphics R3.D3.Graphics
* @param apiInputDrive R3.D3.API.Input.Drive
* @param dom R3.Dom
* @constructor
*/
R3.D3.Input.Drive = function (
graphics,
apiInputDrive,
dom
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (R3.Utils.UndefinedOrNull(apiInputDrive)) {
apiInputDrive = {};
}
if (apiInputDrive instanceof R3.D3.Drive) {
return apiInputDrive;
}
R3.D3.API.Input.Drive.call(
this,
apiInputDrive.id,
apiInputDrive.name,
apiInputDrive.domElementId,
apiInputDrive.pathFollowingComponent,
apiInputDrive.parentEntity,
apiInputDrive.wheelFL,
apiInputDrive.wheelFR,
apiInputDrive.wheelRL,
apiInputDrive.wheelRR,
apiInputDrive.heightOffset,
apiInputDrive.distance,
apiInputDrive.distanceGrain,
apiInputDrive.rotationFactor
);
if (R3.Utils.UndefinedOrNull(dom)) {
console.warn('Cannot create Input without an handle to the DOM');
throw new Error('Cannot create Input without an handle to the DOM');
}
this.dom = dom;
this.keyLeft = false;
this.keyRight = false;
R3.Component.call(
this,
R3.Component.COMPONENT_INPUT_DRIVE,
{
'pathFollowingComponent' : R3.D3.PathFollowing,
'wheelFL' : R3.D3.Mesh,
'wheelFR' : R3.D3.Mesh,
'wheelRL' : R3.D3.Mesh,
'wheelRR' : R3.D3.Mesh
}
);
};
R3.D3.Input.Drive.prototype = Object.create(R3.D3.API.Input.Drive.prototype);
R3.D3.Input.Drive.prototype.constructor = R3.D3.Input.Drive;
R3.D3.Input.Drive.prototype.createInstance = function(update) {
if (update) {
return this.instance;
}
var instance = this.dom.document.getElementById(this.domElementId);
instance.addEventListener(
'keydown',
function(keyboardEvent) {
if (keyboardEvent.key == 'j') {
this.keyLeft = true;
}
if (keyboardEvent.key == 'l') {
this.keyRight = true;
}
}.bind(this),
false
);
instance.addEventListener(
'keyup',
function(keyboardEvent) {
if (keyboardEvent.key == 'j') {
this.keyLeft = false;
}
if (keyboardEvent.key == 'l') {
this.keyRight = false;
}
}.bind(this),
false
);
return instance;
};
R3.D3.Input.Drive.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* R3.D3.Input.Drive to R3.D3.API.Input.Drive
* @returns {R3.D3.API.Input.Drive}
*/
R3.D3.Input.Drive.prototype.toApiObject = function() {
var apiInputDrive = new R3.D3.API.Input.Drive(
this.id,
this.name,
this.domElementId,
R3.Utils.IdOrNull(this.pathFollowingComponent),
R3.Utils.IdOrNull(this.parentEntity),
R3.Utils.IdOrNull(this.wheelFL),
R3.Utils.IdOrNull(this.wheelFR),
R3.Utils.IdOrNull(this.wheelRL),
R3.Utils.IdOrNull(this.wheelRR),
this.heightOffset,
this.distance,
this.distanceGrain,
this.rotationFactor
);
return apiInputDrive;
};
R3.D3.Input.Drive.FromObject = function(graphics, objectComponent) {
var apiInputDrive = R3.D3.API.Input.Drive.FromObject(objectComponent);
return new R3.D3.Input.Drive(
graphics,
apiInputDrive
);
};
R3.D3.Input.Drive.prototype.update = function(deltaTime) {
if (this.pathFollowingComponent) {
this.pathFollowingComponent.mesh.localPosition.x = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.x);
this.pathFollowingComponent.mesh.localPosition.y = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.y);
this.pathFollowingComponent.mesh.localPosition.z = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.z);
if (this.keyLeft) {
this.distance -= this.distanceGrain;
}
if (this.keyRight) {
this.distance += this.distanceGrain;
}
this.pathFollowingComponent.mesh.localPosition.x += (this.distance * this.pathFollowingComponent.rotationMatrix.left.x);
this.pathFollowingComponent.mesh.localPosition.y += (this.distance * this.pathFollowingComponent.rotationMatrix.left.y);
this.pathFollowingComponent.mesh.localPosition.z += (this.distance * this.pathFollowingComponent.rotationMatrix.left.z);
this.wheelFL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed;
this.wheelFR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed;
this.wheelFL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed;
this.wheelFR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed;
this.wheelRL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed;
this.wheelRR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed;
}
};