Initial commit: 3D Hommie RPG game
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
45
js/game/Camera.js
Normal file
45
js/game/Camera.js
Normal file
@@ -0,0 +1,45 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user