import { clamp } from "./ControlTypes.js";
export class FirstPersonControls {
    camera;
    enabled = true;
    yaw = 0;
    pitch = 0;
    moveSpeed;
    lookSpeed;
    minPitch;
    maxPitch;
    constructor(camera, options = {}) {
        this.camera = camera;
        this.moveSpeed = options.moveSpeed ?? 1;
        this.lookSpeed = options.lookSpeed ?? 0.002;
        this.minPitch = options.minPitch ?? -Math.PI / 2 + 0.001;
        this.maxPitch = options.maxPitch ?? Math.PI / 2 - 0.001;
        this.pitch = clamp(camera.rotation?.x ?? 0, this.minPitch, this.maxPitch);
        this.yaw = camera.rotation?.y ?? 0;
    }
    update(snapshot, deltaSeconds) {
        if (!this.enabled) {
            return;
        }
        if (snapshot.button(0).down) {
            this.yaw -= snapshot.pointer.deltaX * this.lookSpeed;
            this.pitch = clamp(this.pitch - snapshot.pointer.deltaY * this.lookSpeed, this.minPitch, this.maxPitch);
            if (this.camera.rotation) {
                this.camera.rotation.x = this.pitch;
                this.camera.rotation.y = this.yaw;
            }
        }
        const forward = (snapshot.key("KeyW").down ? 1 : 0) - (snapshot.key("KeyS").down ? 1 : 0);
        const strafe = (snapshot.key("KeyD").down ? 1 : 0) - (snapshot.key("KeyA").down ? 1 : 0);
        const step = this.moveSpeed * deltaSeconds;
        const sin = Math.sin(this.yaw);
        const cos = Math.cos(this.yaw);
        this.camera.position.x += (strafe * cos + forward * sin) * step;
        this.camera.position.z += (forward * cos - strafe * sin) * step;
    }
    dispose() {
        this.enabled = false;
    }
}
//# sourceMappingURL=FirstPersonControls.js.map