r3-legacy/game-lib-controls.js

225 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-10-07 10:51:37 +02:00
function Controls() {}
Controls.FlyControls = function(
camera,
THREE,
canvas
) {
this.flySpeed = 100;
2016-10-07 10:51:37 +02:00
this.canvas = canvas;
this.THREE = THREE;
this.yaw = 0;
this.pitch = 0;
this.canRotate = false;
this.moveForward = false;
this.moveBackward = false;
this.moveLeft = false;
this.moveRight = false;
this.moveUp = false;
this.moveDown = false;
this.mouseUpCallback = this.onMouseUp.bind(this);
this.mouseDownCallback = this.onMouseDown.bind(this);
this.mouseMoveCallback = this.onMouseMove.bind(this);
this.mouseWheelCallback = this.onMouseWheel.bind(this);
2016-10-07 10:51:37 +02:00
this.keyDownCallback = this.onKeyDown.bind(this);
this.keyUpCallback = this.onKeyUp.bind(this);
this.camera = camera;
this.canvas.addEventListener('keydown', this.keyDownCallback, false);
this.canvas.addEventListener('keyup', this.keyUpCallback, false);
this.canvas.addEventListener('mousedown', this.mouseDownCallback, false);
this.canvas.addEventListener('mouseup', this.mouseUpCallback, false);
this.canvas.addEventListener('mousewheel', this.mouseWheelCallback, false);
this.havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
this.element = document.body;
2016-10-07 10:51:37 +02:00
if (this.havePointerLock) {
this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
2016-10-07 10:51:37 +02:00
}
};
Controls.FlyControls.prototype.onMouseWheel = function(event) {
this.moveForward = true;
this.applyTranslation(event.wheelDelta * 0.001);
event.preventDefault();
this.moveForward = false;
2016-10-07 10:51:37 +02:00
};
Controls.FlyControls.prototype.onMouseDown = function(event) {
// if (event.button == 0) {
// this.canRotate = true;
// this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
// if (this.havePointerLock) {
// this.element.requestPointerLock();
// }
// }
if (event.button == 1) {
2016-10-07 10:51:37 +02:00
this.canRotate = true;
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
}
};
Controls.FlyControls.prototype.onMouseUp = function(event) {
// if (event.button == 0) {
// this.canRotate = false;
// this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
// if (this.havePointerLock) {
// document.exitPointerLock();
// }
// }
if (event.button == 1) {
2016-10-07 10:51:37 +02:00
this.canRotate = false;
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
}
};
2016-10-07 16:06:50 +02:00
Controls.FlyControls.prototype.applyRotation = function() {
2016-10-07 10:51:37 +02:00
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ");
};
Controls.FlyControls.prototype.applyTranslation = function(deltaTime) {
var direction = new this.THREE.Vector3(0, 0, -1);
var rotation = new this.THREE.Euler(0, 0, 0, "YXZ");
rotation.set(this.pitch, this.yaw, 0, "YXZ");
direction = direction.applyEuler(rotation);
if(this.moveForward) {
var newPos = direction.normalize();
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveBackward) {
var newPos = direction.normalize();
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
}
if(this.moveLeft) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveRight) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
}
// Absolute Y-Axis
if(this.moveUp) {
this.camera.position.y += (deltaTime * this.flySpeed);
} else if(this.moveDown) {
this.camera.position.y -= (deltaTime * this.flySpeed);
}
};
Controls.FlyControls.prototype.update = function(deltaTime) {
2016-10-07 16:06:50 +02:00
this.applyRotation();
this.applyTranslation(deltaTime);
2016-10-07 10:51:37 +02:00
};
Controls.FlyControls.prototype.onMouseMove = function ( event ) {
if (this.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
this.yaw -= movementX * 0.002;
this.pitch -= movementY * 0.002;
}
};
Controls.FlyControls.prototype.onKeyDown = function ( event ) {
switch ( event.keyCode ) {
case 87: // w
this.moveForward = true;
break;
case 65: // a
this.moveLeft = true;
break;
case 83: // s
this.moveBackward = true;
break;
case 68: // d
this.moveRight = true;
break;
case 104: // keypad up arrow
2016-10-07 10:51:37 +02:00
this.moveUp = true;
break;
case 98: // keypad down arrow
2016-10-07 10:51:37 +02:00
this.moveDown = true;
break;
}
};
Controls.FlyControls.prototype.onKeyUp = function ( event ) {
switch ( event.keyCode ) {
case 38: // up
case 87: // w
this.moveForward = false;
break;
case 37: // left
case 65: // a
this.moveLeft = false;
break;
case 40: // down
case 83: // s
this.moveBackward = false;
break;
case 39: // right
case 68: // d
this.moveRight = false;
break;
case 104: // keypad up arrow
2016-10-07 10:51:37 +02:00
this.moveUp = false;
break;
case 98: // keypad down arrow
2016-10-07 10:51:37 +02:00
this.moveDown = false;
break;
}
};
if (typeof module !== 'undefined') {
module.exports = Controls;
}