diff --git a/client/input_manager.ts b/client/input_manager.ts index 96668b4..dd6b440 100644 --- a/client/input_manager.ts +++ b/client/input_manager.ts @@ -76,6 +76,16 @@ export type KeyCode = | "F11" | "F12"; +// the keys the game keeps in fullscreen, see "fullscreenchange". not every key: with all of them locked, chromium +// can keep alt+tab and the windows key from the os too +const FULLSCREEN_LOCKED_KEYS = ["Escape", "KeyW"]; + +// chromium's keyboard lock api, typescript's dom types don't have it yet +interface KeyboardLock { + lock(codes?: string[]): Promise; + unlock(): void; +} + export class InputManager { static keys_down = new Set(); static keys_pressed = new Set(); @@ -205,6 +215,18 @@ export class InputManager { this.pointer_lock_waiting = false; }); + // in fullscreen, chromium's keyboard lock gives the game escape (holding it still leaves fullscreen) and ctrl+w, + // instead of the browser leaving fullscreen or closing the tab. other browsers don't have it and work as before + document.addEventListener("fullscreenchange", () => { + const keyboard = (navigator as Navigator & { keyboard?: KeyboardLock }).keyboard; + if (!keyboard?.lock) return; + if (document.fullscreenElement) { + keyboard.lock(FULLSCREEN_LOCKED_KEYS).catch(() => {}); + } else { + keyboard.unlock(); + } + }); + document.addEventListener("pointerlockerror", () => { this.pointer_lock_waiting = false; }); diff --git a/client/main.ts b/client/main.ts index 70cb81c..1e05d03 100644 --- a/client/main.ts +++ b/client/main.ts @@ -23,7 +23,13 @@ import { TitleScreen } from "./gui/title_screen.ts"; import { DisconnectedScreen } from "./gui/disconnected_screen.ts"; // runs whatever is showing every frame: a menu screen before joining (and after leaving), or the game -export class ClientLoop { +export // while playing, the browser asks before leaving the page. sprinting is ctrl and walking is w, and ctrl+w closes the +// tab without the page getting a say. in fullscreen the keyboard lock catches it instead (see input_manager.ts) +function confirm_leaving(confirm: boolean) { + self.onbeforeunload = confirm ? (event) => event.preventDefault() : null; +} + +class ClientLoop { running = false; last_time = 0; client: Client | undefined; @@ -54,6 +60,7 @@ export class ClientLoop { show_screen(screen: GuiScreen) { this.screen?.on_close(); this.client = undefined; + confirm_leaving(false); this.screen = screen; InputManager.set_mouse_grabbed(false); } @@ -65,6 +72,7 @@ export class ClientLoop { this.screen?.on_close(); this.screen = undefined; this.client = client; + confirm_leaving(true); console.log("Game started"); } diff --git a/client/options.ts b/client/options.ts index 8364142..3e98290 100644 --- a/client/options.ts +++ b/client/options.ts @@ -9,9 +9,9 @@ export class Options { key_left: KeyCode = "KeyA"; key_right: KeyCode = "KeyD"; key_jump: KeyCode = "Space"; - key_sprint: KeyCode = "ShiftLeft"; - // not shift like minecraft, that's sprint. ctrl isn't either: ctrl+w closes the tab and a page can't stop it - key_sneak: KeyCode = "KeyC"; + // like minecraft. ctrl+w would close the tab, see confirm_leaving in main.ts + key_sprint: KeyCode = "ControlLeft"; + key_sneak: KeyCode = "ShiftLeft"; key_inventory: KeyCode = "KeyE"; // with ctrl it drops the whole stack key_drop: KeyCode = "KeyQ"; diff --git a/tests/local_player_test.ts b/tests/local_player_test.ts index 37eb154..42b7a6e 100644 --- a/tests/local_player_test.ts +++ b/tests/local_player_test.ts @@ -115,7 +115,7 @@ Deno.test("sneaking doesn't walk off edges, from any direction, but still jumps player.set_position(1.5, 0, 1.5); player.yaw = yaw; run(player, 1, () => false); - InputManager.keys_down.add("KeyC"); + InputManager.keys_down.add("ShiftLeft"); InputManager.keys_down.add(key); for (let tick = 0; tick < 60; tick++) player.tick(); assertEquals(player.y, 0, `fell off with ${key} at yaw ${yaw}`);