46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
import * as THREE from 'three';
|
|
|
|
export class CameraController {
|
|
constructor(game) {
|
|
this.game = game;
|
|
this.camera = game.camera;
|
|
|
|
this.yaw = 0;
|
|
this.pitch = 0;
|
|
this.sensitivity = 0.002;
|
|
this.maxPitch = Math.PI / 2 - 0.1;
|
|
|
|
this.isLocked = false;
|
|
|
|
this.setupPointerLock();
|
|
}
|
|
|
|
setupPointerLock() {
|
|
const canvas = this.game.canvas;
|
|
|
|
canvas.addEventListener('click', () => {
|
|
if (!this.isLocked && this.game.running) {
|
|
canvas.requestPointerLock();
|
|
}
|
|
});
|
|
|
|
document.addEventListener('pointerlockchange', () => {
|
|
this.isLocked = document.pointerLockElement === canvas;
|
|
});
|
|
|
|
document.addEventListener('mousemove', (e) => {
|
|
if (!this.isLocked) return;
|
|
|
|
this.yaw -= e.movementX * this.sensitivity;
|
|
this.pitch -= e.movementY * this.sensitivity;
|
|
this.pitch = THREE.MathUtils.clamp(this.pitch, -this.maxPitch, this.maxPitch);
|
|
});
|
|
}
|
|
|
|
update(dt) {
|
|
// Применяем вращение камеры
|
|
const euler = new THREE.Euler(this.pitch, this.yaw, 0, 'YXZ');
|
|
this.camera.quaternion.setFromEuler(euler);
|
|
}
|
|
}
|