Change run key to ctrl and some other messy stuff to make that work,,

This commit is contained in:
2026-09-27 20:10:03 -03:00
parent 966b517bbd
commit 2207eea1e1
4 changed files with 35 additions and 5 deletions
+22
View File
@@ -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<void>;
unlock(): void;
}
export class InputManager {
static keys_down = new Set<string>();
static keys_pressed = new Set<string>();
@@ -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;
});
+9 -1
View File
@@ -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");
}
+3 -3
View File
@@ -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";
+1 -1
View File
@@ -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}`);