Files
Hommie_RPG_Game/js/game/Camera.js
Maxim Dolgolyov fb5f09212b Initial commit: 3D Hommie RPG game
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-25 01:04:09 +03:00

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);
}
}