120 lines
2.9 KiB
JavaScript
120 lines
2.9 KiB
JavaScript
import { Vector3 } from 'three';
|
|
import { PointerLockControls } from 'three/examples/jsm/Addons.js';
|
|
|
|
class PointerControls {
|
|
constructor(camera, hero, domElement) {
|
|
this.kb = {};
|
|
|
|
this.canJump = false;
|
|
this.velocity = new Vector3();
|
|
this.direction = new Vector3();
|
|
this.rotationY = 0;
|
|
this.vertex = new Vector3();
|
|
this.rvelo = 0;
|
|
|
|
this.camera = camera;
|
|
this.hero = hero;
|
|
this.click = false;
|
|
|
|
this.controls = new PointerLockControls(camera, domElement);
|
|
|
|
const onKeyDown = (event) => {
|
|
this.kb[event.code] = true;
|
|
switch (event.code) {
|
|
case 'Space':
|
|
if (this.canJump === true) this.velocity.y += 350;
|
|
this.canJump = false;
|
|
break;
|
|
}
|
|
};
|
|
|
|
const onKeyUp = (event) => {
|
|
this.kb[event.code] = false;
|
|
};
|
|
|
|
document.addEventListener('keydown', onKeyDown);
|
|
document.addEventListener('keyup', onKeyUp);
|
|
|
|
window.addEventListener("gamepadconnected", (e) => {
|
|
this.gp = navigator.getGamepads()[e.gamepad.index];
|
|
console.log("Gamepad connected", this.gp);
|
|
});
|
|
|
|
domElement.addEventListener('click', () => {
|
|
this.controls.isLocked && this.clicked && this.clicked();
|
|
});
|
|
|
|
domElement.addEventListener('mousedown', () => {
|
|
this.controls.isLocked && this.onpointer && this.onpointer('start');
|
|
});
|
|
|
|
domElement.addEventListener('mousemove', () => {
|
|
this.controls.isLocked && this.onpointer && this.onpointer('drag');
|
|
});
|
|
|
|
domElement.addEventListener('mouseup', () => {
|
|
this.controls.isLocked && this.onpointer && this.onpointer('end');
|
|
});
|
|
|
|
this.update = () => { };
|
|
|
|
}
|
|
|
|
get moveForward(){
|
|
return this.kb['ArrowUp'] || this.kb['KeyW'] || false
|
|
}
|
|
|
|
get moveLeft(){
|
|
return this.kb['ArrowLeft'] || this.kb['KeyA'] || false
|
|
}
|
|
|
|
get rotateLeft(){
|
|
return this.moveLeft;
|
|
}
|
|
|
|
get moveRight(){
|
|
return this.kb['ArrowRight'] || this.kb['KeyD'] || false
|
|
}
|
|
|
|
get rotateRight(){
|
|
return this.moveRight;
|
|
}
|
|
|
|
get moveBackward(){
|
|
return this.kb['ArrowDown'] || this.kb['KeyS'] || false
|
|
}
|
|
|
|
get moveUp(){
|
|
return this.kb['KeyR'] || false
|
|
}
|
|
|
|
get moveDown(){
|
|
return this.kb['KeyF'] || false
|
|
}
|
|
|
|
get cameraLeft(){
|
|
return this.kb['KeyQ'] || false
|
|
}
|
|
|
|
get cameraRight(){
|
|
return this.kb['KeyE'] || false
|
|
}
|
|
|
|
get moving(){
|
|
return this.moveForward || this.moveBackward;
|
|
}
|
|
|
|
get rotating(){
|
|
return this.rotateLeft || this.rotateRight;
|
|
}
|
|
|
|
get motion(){
|
|
return this.moving || this.rotating;
|
|
}
|
|
|
|
get running(){
|
|
return this.moving && this.kb.ShiftLeft;
|
|
}
|
|
}
|
|
|
|
export { PointerControls }; |