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

174 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-01-02 17:05:40 +01:00
/**
* Input parent class
2018-04-09 10:05:13 +02:00
* @param graphics R3.D3.Graphics
* @param apiInputDrive R3.D3.API.Input.Drive
* @param dom R3.Dom
2017-01-02 17:05:40 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Input.Drive = function (
2017-01-02 17:05:40 +01:00
graphics,
2017-01-12 17:40:17 +01:00
apiInputDrive,
dom
2017-01-02 17:05:40 +01:00
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiInputDrive)) {
2017-01-12 17:40:17 +01:00
apiInputDrive = {};
}
2018-04-09 10:05:13 +02:00
if (apiInputDrive instanceof R3.D3.Drive) {
return apiInputDrive;
}
2018-04-09 10:05:13 +02:00
R3.D3.API.Input.Drive.call(
2017-01-02 17:05:40 +01:00
this,
apiInputDrive.id,
apiInputDrive.name,
apiInputDrive.domElementId,
apiInputDrive.pathFollowingComponent,
2017-01-04 16:12:30 +01:00
apiInputDrive.parentEntity,
apiInputDrive.wheelFL,
apiInputDrive.wheelFR,
apiInputDrive.wheelRL,
apiInputDrive.wheelRR,
apiInputDrive.heightOffset,
apiInputDrive.distance,
apiInputDrive.distanceGrain,
apiInputDrive.rotationFactor
2017-01-02 17:05:40 +01:00
);
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(dom)) {
2017-01-12 17:40:17 +01:00
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;
2017-01-02 17:05:40 +01:00
this.keyLeft = false;
this.keyRight = false;
2018-04-09 10:05:13 +02:00
R3.Component.call(
2017-06-16 15:49:53 +02:00
this,
2018-04-09 10:05:13 +02:00
R3.Component.COMPONENT_INPUT_DRIVE,
2017-06-16 15:49:53 +02:00
{
2018-04-09 10:05:13 +02:00
'pathFollowingComponent' : R3.D3.PathFollowing,
'wheelFL' : R3.D3.Mesh,
'wheelFR' : R3.D3.Mesh,
'wheelRL' : R3.D3.Mesh,
'wheelRR' : R3.D3.Mesh
2017-06-16 15:49:53 +02:00
}
);
2017-01-02 17:05:40 +01:00
};
2018-04-09 10:05:13 +02:00
R3.D3.Input.Drive.prototype = Object.create(R3.D3.API.Input.Drive.prototype);
R3.D3.Input.Drive.prototype.constructor = R3.D3.Input.Drive;
2017-01-02 17:05:40 +01:00
2018-04-09 10:05:13 +02:00
R3.D3.Input.Drive.prototype.createInstance = function(update) {
2017-01-02 17:05:40 +01:00
if (update) {
return this.instance;
}
2017-01-12 17:40:17 +01:00
var instance = this.dom.document.getElementById(this.domElementId);
2017-01-02 17:05:40 +01:00
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;
};
2018-04-09 10:05:13 +02:00
R3.D3.Input.Drive.prototype.updateInstance = function() {
2017-01-02 17:05:40 +01:00
this.instance = this.createInstance(true);
};
2017-01-06 16:53:53 +01:00
/**
2018-04-09 10:05:13 +02:00
* R3.D3.Input.Drive to R3.D3.API.Input.Drive
* @returns {R3.D3.API.Input.Drive}
2017-01-06 16:53:53 +01:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.Input.Drive.prototype.toApiObject = function() {
2017-01-02 17:05:40 +01:00
2018-04-09 10:05:13 +02:00
var apiInputDrive = new R3.D3.API.Input.Drive(
2017-01-02 17:05:40 +01:00
this.id,
this.name,
this.domElementId,
2018-04-09 10:05:13 +02:00
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),
2017-01-04 16:12:30 +01:00
this.heightOffset,
this.distance,
this.distanceGrain,
this.rotationFactor
2017-01-02 17:05:40 +01:00
);
return apiInputDrive;
};
2018-04-09 10:05:13 +02:00
R3.D3.Input.Drive.FromObject = function(graphics, objectComponent) {
2017-01-02 17:05:40 +01:00
2018-04-09 10:05:13 +02:00
var apiInputDrive = R3.D3.API.Input.Drive.FromObject(objectComponent);
2017-01-02 17:05:40 +01:00
2018-04-09 10:05:13 +02:00
return new R3.D3.Input.Drive(
2017-01-02 17:05:40 +01:00
graphics,
apiInputDrive
);
};
2018-04-09 10:05:13 +02:00
R3.D3.Input.Drive.prototype.update = function(deltaTime) {
2017-01-02 17:05:40 +01:00
if (this.pathFollowingComponent) {
2017-01-04 16:12:30 +01:00
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);
2017-01-02 17:05:40 +01:00
2017-01-03 18:15:03 +01:00
if (this.keyLeft) {
2017-01-04 16:12:30 +01:00
this.distance -= this.distanceGrain;
2017-01-02 17:05:40 +01:00
}
if (this.keyRight) {
2017-01-04 16:12:30 +01:00
this.distance += this.distanceGrain;
2017-01-02 17:05:40 +01:00
}
2017-01-03 18:15:03 +01:00
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);
2017-01-04 16:12:30 +01:00
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;
2017-01-02 17:05:40 +01:00
}
};