Fix delta mouse bug while when closing inventory
This commit is contained in:
+1
-1
@@ -156,7 +156,7 @@ export class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (InputManager.is_mouse_grabbed()) {
|
if (InputManager.is_mouse_grabbed()) {
|
||||||
const mouse = InputManager.get_mouse_delta();
|
const mouse = InputManager.get_look_delta();
|
||||||
player.turn(mouse.x, mouse.y);
|
player.turn(mouse.x, mouse.y);
|
||||||
}
|
}
|
||||||
InputManager.set_mouse_grabbed(!this.screen);
|
InputManager.set_mouse_grabbed(!this.screen);
|
||||||
|
|||||||
+37
-7
@@ -88,8 +88,15 @@ export class InputManager {
|
|||||||
|
|
||||||
static mouse_x = 0;
|
static mouse_x = 0;
|
||||||
static mouse_y = 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_x = 0;
|
||||||
static mouse_delta_y = 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;
|
static wheel_delta = 0;
|
||||||
|
|
||||||
// in the order they were typed. a list, since the same letter can be typed twice in one frame
|
// 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) => {
|
document.addEventListener("mousemove", (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (document.pointerLockElement) {
|
if (document.pointerLockElement === canvas) {
|
||||||
this.mouse_delta_x += e.movementX;
|
if (this.#skip_look_event) {
|
||||||
this.mouse_delta_y += e.movementY;
|
this.#skip_look_event = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.look_delta_x += e.movementX;
|
||||||
|
this.look_delta_y += e.movementY;
|
||||||
} else {
|
} else {
|
||||||
const rect = canvas.getBoundingClientRect();
|
const rect = canvas.getBoundingClientRect();
|
||||||
const new_x = e.clientX - rect.left;
|
const new_x = e.clientX - rect.left;
|
||||||
const new_y = e.clientY - rect.top;
|
const new_y = e.clientY - rect.top;
|
||||||
|
|
||||||
this.mouse_delta_x += new_x - this.mouse_x;
|
// not from where it was before the mouse was grabbed, that's -1
|
||||||
this.mouse_delta_y += new_y - this.mouse_y;
|
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_x = new_x;
|
||||||
this.mouse_y = new_y;
|
this.mouse_y = new_y;
|
||||||
@@ -177,6 +191,7 @@ export class InputManager {
|
|||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener("pointerlockchange", () => {
|
document.addEventListener("pointerlockchange", () => {
|
||||||
|
this.#skip_look_event = document.pointerLockElement === canvas;
|
||||||
self.setTimeout(() => {
|
self.setTimeout(() => {
|
||||||
const grab = document.pointerLockElement === canvas;
|
const grab = document.pointerLockElement === canvas;
|
||||||
if (!grab) {
|
if (!grab) {
|
||||||
@@ -228,6 +243,11 @@ export class InputManager {
|
|||||||
return { x: this.mouse_delta_x, y: this.mouse_delta_y };
|
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 {
|
static get_wheel_delta(): number {
|
||||||
return this.wheel_delta;
|
return this.wheel_delta;
|
||||||
}
|
}
|
||||||
@@ -259,6 +279,8 @@ export class InputManager {
|
|||||||
this.mouse_buttons_released.clear();
|
this.mouse_buttons_released.clear();
|
||||||
this.mouse_delta_x = 0;
|
this.mouse_delta_x = 0;
|
||||||
this.mouse_delta_y = 0;
|
this.mouse_delta_y = 0;
|
||||||
|
this.look_delta_x = 0;
|
||||||
|
this.look_delta_y = 0;
|
||||||
this.wheel_delta = 0;
|
this.wheel_delta = 0;
|
||||||
this.typed_characters = [];
|
this.typed_characters = [];
|
||||||
this.shortcuts = [];
|
this.shortcuts = [];
|
||||||
@@ -267,6 +289,16 @@ export class InputManager {
|
|||||||
|
|
||||||
static async set_mouse_grabbed(grab: boolean) {
|
static async set_mouse_grabbed(grab: boolean) {
|
||||||
this.pointer_lock_flag = grab;
|
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();
|
const t = performance.now();
|
||||||
this.mouse_grab_timer = t;
|
this.mouse_grab_timer = t;
|
||||||
if (grab) {
|
if (grab) {
|
||||||
@@ -288,8 +320,6 @@ export class InputManager {
|
|||||||
}, 3100 - (t - this.mouse_ungrab_timer));
|
}, 3100 - (t - this.mouse_ungrab_timer));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.mouse_ungrab_timeout !== -1) self.clearTimeout(this.mouse_ungrab_timeout);
|
|
||||||
this.mouse_ungrab_timeout = -1;
|
|
||||||
if (!this.pointer_lock_waiting) {
|
if (!this.pointer_lock_waiting) {
|
||||||
document.exitPointerLock();
|
document.exitPointerLock();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user