diff --git a/client/client.ts b/client/client.ts index 7353989..230b7f6 100644 --- a/client/client.ts +++ b/client/client.ts @@ -156,7 +156,7 @@ export class Client { } if (InputManager.is_mouse_grabbed()) { - const mouse = InputManager.get_mouse_delta(); + const mouse = InputManager.get_look_delta(); player.turn(mouse.x, mouse.y); } InputManager.set_mouse_grabbed(!this.screen); diff --git a/client/input_manager.ts b/client/input_manager.ts index 2f5a21d..96668b4 100644 --- a/client/input_manager.ts +++ b/client/input_manager.ts @@ -88,8 +88,15 @@ export class InputManager { static mouse_x = 0; static mouse_y = 0; + // the cursor's movement over the page, while it isn't locked, like for dragging windows static mouse_delta_x = 0; static mouse_delta_y = 0; + // the mouse's movement while it's locked to the game, which turns the camera. kept apart so the cursor moving + // around a screen can never turn it + static look_delta_x = 0; + static look_delta_y = 0; + // chrome sometimes sends a huge movement with the first mouse event after the lock starts + static #skip_look_event = false; static wheel_delta = 0; // in the order they were typed. a list, since the same letter can be typed twice in one frame @@ -160,16 +167,23 @@ export class InputManager { document.addEventListener("mousemove", (e) => { e.preventDefault(); - if (document.pointerLockElement) { - this.mouse_delta_x += e.movementX; - this.mouse_delta_y += e.movementY; + if (document.pointerLockElement === canvas) { + if (this.#skip_look_event) { + this.#skip_look_event = false; + return; + } + this.look_delta_x += e.movementX; + this.look_delta_y += e.movementY; } else { const rect = canvas.getBoundingClientRect(); const new_x = e.clientX - rect.left; const new_y = e.clientY - rect.top; - this.mouse_delta_x += new_x - this.mouse_x; - this.mouse_delta_y += new_y - this.mouse_y; + // not from where it was before the mouse was grabbed, that's -1 + if (this.mouse_x >= 0 && this.mouse_y >= 0) { + this.mouse_delta_x += new_x - this.mouse_x; + this.mouse_delta_y += new_y - this.mouse_y; + } this.mouse_x = new_x; this.mouse_y = new_y; @@ -177,6 +191,7 @@ export class InputManager { }); document.addEventListener("pointerlockchange", () => { + this.#skip_look_event = document.pointerLockElement === canvas; self.setTimeout(() => { const grab = document.pointerLockElement === canvas; if (!grab) { @@ -228,6 +243,11 @@ export class InputManager { return { x: this.mouse_delta_x, y: this.mouse_delta_y }; } + // how far the locked mouse moved this frame, for turning the camera + static get_look_delta() { + return { x: this.look_delta_x, y: this.look_delta_y }; + } + static get_wheel_delta(): number { return this.wheel_delta; } @@ -259,6 +279,8 @@ export class InputManager { this.mouse_buttons_released.clear(); this.mouse_delta_x = 0; this.mouse_delta_y = 0; + this.look_delta_x = 0; + this.look_delta_y = 0; this.wheel_delta = 0; this.typed_characters = []; this.shortcuts = []; @@ -267,6 +289,16 @@ export class InputManager { static async set_mouse_grabbed(grab: boolean) { this.pointer_lock_flag = grab; + // a retry to grab it again mustn't happen once it shouldn't be grabbed + if (!grab && this.mouse_ungrab_timeout !== -1) { + self.clearTimeout(this.mouse_ungrab_timeout); + this.mouse_ungrab_timeout = -1; + } + // already how it should be, the game calls this every frame + const locked = document.pointerLockElement === canvas; + if (grab ? locked : !locked && !this.pointer_lock_waiting) { + return; + } const t = performance.now(); this.mouse_grab_timer = t; if (grab) { @@ -288,8 +320,6 @@ export class InputManager { }, 3100 - (t - this.mouse_ungrab_timer)); } } else { - if (this.mouse_ungrab_timeout !== -1) self.clearTimeout(this.mouse_ungrab_timeout); - this.mouse_ungrab_timeout = -1; if (!this.pointer_lock_waiting) { document.exitPointerLock(); }