physics
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
import {
|
||||
Clock,
|
||||
Vector3
|
||||
} from 'three';
|
||||
|
||||
import { PointerLockControls } from 'three/examples/jsm/Addons.js';
|
||||
|
||||
class PointerControls {
|
||||
constructor(camera, hero, domElement) {
|
||||
this.moveForward = false;
|
||||
this.moveBackward = false;
|
||||
this.moveLeft = false;
|
||||
this.moveRight = false;
|
||||
this.moveUp = false;
|
||||
this.moveDown = false;
|
||||
this.rotateLeft = false;
|
||||
this.rotateRight = false;
|
||||
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.clock = new Clock();
|
||||
this.click = false;
|
||||
|
||||
this.controls = new PointerLockControls(camera, domElement);
|
||||
|
||||
const onKeyDown = (event) => {
|
||||
switch (event.code) {
|
||||
case 'ArrowUp':
|
||||
case 'KeyW':
|
||||
this.moveForward = true;
|
||||
break;
|
||||
case 'ArrowLeft':
|
||||
case 'KeyA':
|
||||
this.moveLeft = true;
|
||||
this.rotateLeft = true;
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
case 'KeyS':
|
||||
this.moveBackward = true;
|
||||
break;
|
||||
case 'ArrowRight':
|
||||
case 'KeyD':
|
||||
this.moveRight = true;
|
||||
this.rotateRight = true;
|
||||
break;
|
||||
case 'KeyQ':
|
||||
this.rotateLeft = true;
|
||||
break;
|
||||
case 'KeyE':
|
||||
this.rotateRight = true;
|
||||
break;
|
||||
case 'KeyR':
|
||||
this.moveUp = true;
|
||||
break;
|
||||
case 'KeyF':
|
||||
this.moveDown = true;
|
||||
break;
|
||||
case 'Space':
|
||||
if (this.canJump === true) this.velocity.y += 350;
|
||||
this.canJump = false;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
const onKeyUp = (event) => {
|
||||
switch (event.code) {
|
||||
case 'ArrowUp':
|
||||
case 'KeyW':
|
||||
this.moveForward = false;
|
||||
break;
|
||||
case 'ArrowLeft':
|
||||
case 'KeyA':
|
||||
this.moveLeft = false;
|
||||
this.rotateLeft = false;
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
case 'KeyS':
|
||||
this.moveBackward = false;
|
||||
break;
|
||||
case 'ArrowRight':
|
||||
case 'KeyD':
|
||||
this.moveRight = false;
|
||||
this.rotateRight = false;
|
||||
break;
|
||||
case 'KeyQ':
|
||||
this.rotateLeft = false;
|
||||
break;
|
||||
case 'KeyE':
|
||||
this.rotateRight = false;
|
||||
break;
|
||||
case 'KeyR':
|
||||
this.moveUp = false;
|
||||
break;
|
||||
case 'KeyF':
|
||||
this.moveDown = false;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
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 = () => {
|
||||
// const delta = this.clock.getDelta() * 10;
|
||||
// if (this.gp) {
|
||||
// this.gp = navigator.getGamepads()[this.gp.index];
|
||||
// this.gp.pressed = this.gp.buttons[4].pressed || this.gp.buttons[5].pressed || this.gp.buttons[6].pressed || this.gp.buttons[7].pressed || this.gp.buttons[9].pressed;
|
||||
// if (!this.click && this.gp.pressed) {
|
||||
// this.click = true;
|
||||
// this.clicked?.();
|
||||
// //console.log(this.gp.buttons.map((b,i)=>[i, b.pressed]));
|
||||
// }
|
||||
// if (this.click && !this.gp.pressed) this.click = false;
|
||||
// }
|
||||
// //this.velocity.x -= this.velocity.x * 5.0 * delta;
|
||||
// this.velocity.z -= this.velocity.z * 5.0 * delta;
|
||||
// this.rvelo -= this.rvelo * 5 * delta;
|
||||
// // this.velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
|
||||
// this.direction.z = Number(this.moveForward) - Number(this.moveBackward) - Math.floor((this.gp && this.gp.axes[1] || 0) * 100) / 100 + (this.touchControls && this.touchControls.move || 0);
|
||||
// this.rotationY = Number(this.rotateLeft) - Number(this.rotateRight) - Math.floor((this.gp && this.gp.axes[0] || 0) * 100) / 110 + (this.touchControls && this.touchControls.rotate || 0);
|
||||
// //this.direction.x = Number( this.moveRight ) - Number( this.moveLeft );
|
||||
// //this.direction.normalize(); // this ensures consistent movements in all directions
|
||||
// if (this.direction.z) this.velocity.z -= this.direction.z * 5.0 * delta;
|
||||
// //if ( this.moveLeft || this.moveRight ) this.velocity.x -= this.direction.x * 5.0 * delta;
|
||||
// if (this.rotationY) this.rvelo -= this.rotationY * 8 * delta;
|
||||
|
||||
//this.velocity.x && this.controls.moveRight( - this.velocity.x * delta );
|
||||
// if (this.velocity.z) {
|
||||
// this.controls.moveForward(-this.velocity.z * delta);
|
||||
// this.hero.position.z += -this.velocity.z * delta;
|
||||
// }
|
||||
// this.controls.moveRight( this.direction.x * delta );
|
||||
// this.controls.moveForward( this.direction.z * delta );
|
||||
// this.hero.position.y += (Number(this.moveUp) - Number(this.moveDown)) * delta;
|
||||
// this.hero.rotation.y += -this.rvelo * delta;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export { PointerControls };
|
||||
Reference in New Issue
Block a user