Split The code is public; the app answers only to the crew.
A whole match on a phone: fire while aiming, weapon switch, scoreboard and a way to leave #25
Check · Review · Land · Live done — Landed and live in production.
vesper/cs:vesper/20-a-whole-match-on-a-phone-fire-while-aimi → main 1 commit fork
8 files
changed +425
−96
README.md +22 −3
| @@ -71,9 +71,28 @@ The bundle in `static/` is committed, so a fresh clone plays without building; | ||
| 71 | 71 | <kbd>Shift</kbd> walk, mouse aims and fires, <kbd>R</kbd> reload, |
| 72 | 72 | <kbd>E</kbd> plants or defuses, <kbd>B</kbd> opens the buy menu in the freeze |
| 73 | 73 | time, <kbd>1</kbd>/<kbd>2</kbd>/<kbd>3</kbd> switch guns, <kbd>Tab</kbd> shows |
| 74 | the scoreboard, <kbd>Esc</kbd> pauses. On a touch screen the left of the view | |
| 75 | is a thumb stick, the right looks around, and the buttons along the bottom do | |
| 76 | the rest. | |
| 74 | the scoreboard, <kbd>Esc</kbd> pauses. | |
| 75 | ||
| 76 | On a touch screen the same actions are a pad, and the view is the aim: | |
| 77 | ||
| 78 | | what | key | thumb | | |
| 79 | | --- | --- | --- | | |
| 80 | | move | <kbd>WASD</kbd> | the left third of the view is a stick | | |
| 81 | | aim | mouse | drag anywhere on the rest of the view | | |
| 82 | | fire | mouse | **a second finger on the right of the view**, or the fire bar | | |
| 83 | | crouch, jump, use, reload | <kbd>Ctrl</kbd> <kbd>Space</kbd> <kbd>E</kbd> <kbd>R</kbd> | the pad under the view | | |
| 84 | | guns | <kbd>1</kbd>/<kbd>2</kbd>/<kbd>3</kbd> | `1 rifle`, `2 pistol`, `3 knife` | | |
| 85 | | scoreboard | <kbd>Tab</kbd> | hold `scores` | | |
| 86 | | buy | <kbd>B</kbd> | `buy`, and `Close` when you are done | | |
| 87 | | pause, leave the match | <kbd>Esc</kbd> | `menu` | | |
| 88 | ||
| 89 | Firing is a second finger rather than a button under your aiming thumb because | |
| 90 | a touch keeps the target it started on for its whole life: the finger already | |
| 91 | dragging the view goes on steering while another one anywhere on the right | |
| 92 | holds the trigger, and the trigger finger's own movement is thrown away. The | |
| 93 | pad lives under the view rather than on it — on a phone the view is 4:3 and | |
| 94 | the HUD already owns all four of its corners, so a button on the glass would | |
| 95 | be sitting on the radar, the health or the ammo. | |
| 77 | 96 | |
| 78 | 97 | When you die the camera moves to a teammate who is still up and the HUD becomes |
| 79 | 98 | theirs — their health, armour, ammo and gun, no crosshair — over a line naming |
src/client/input.test.ts added +202
| @@ -0,0 +1,202 @@ | ||
| 1 | /** | |
| 2 | * Input against a canvas and a document that are three lines of fake each: no | |
| 3 | * browser, just the events one would send. What is worth checking here is the | |
| 4 | * touch build, because that is where two fingers have to mean two different | |
| 5 | * things at the same moment — the one that steers and the one that fires. | |
| 6 | */ | |
| 7 | ||
| 8 | import { afterEach, expect, test } from "bun:test"; | |
| 9 | import { attachButtons, createControls, type Controls } from "./input.ts"; | |
| 10 | import { downAt } from "./press.ts"; | |
| 11 | ||
| 12 | type Listener = (event: unknown) => void; | |
| 13 | ||
| 14 | /** An EventTarget that remembers its listeners so a test can fire them. */ | |
| 15 | function target(extra: Record<string, unknown> = {}): Record<string, unknown> & { | |
| 16 | fire(type: string, event?: Record<string, unknown>): void; | |
| 17 | } { | |
| 18 | const listeners = new Map<string, Set<Listener>>(); | |
| 19 | return { | |
| 20 | ...extra, | |
| 21 | addEventListener(type: string, fn: Listener): void { | |
| 22 | const set = listeners.get(type) ?? new Set<Listener>(); | |
| 23 | set.add(fn); | |
| 24 | listeners.set(type, set); | |
| 25 | }, | |
| 26 | removeEventListener(type: string, fn: Listener): void { | |
| 27 | listeners.get(type)?.delete(fn); | |
| 28 | }, | |
| 29 | fire(type: string, event: Record<string, unknown> = {}): void { | |
| 30 | for (const fn of Array.from(listeners.get(type) ?? [])) { | |
| 31 | fn({ preventDefault: () => {}, ...event }); | |
| 32 | } | |
| 33 | }, | |
| 34 | }; | |
| 35 | } | |
| 36 | ||
| 37 | type Canvas = ReturnType<typeof target>; | |
| 38 | ||
| 39 | const globals = globalThis as unknown as Record<string, unknown>; | |
| 40 | const before = { window: globals.window, document: globals.document }; | |
| 41 | ||
| 42 | // The fakes are this file's and nobody else's: the rest of the suite runs in | |
| 43 | // the same process, and hud.ts asks whether `document` exists. | |
| 44 | afterEach(() => { | |
| 45 | globals.window = before.window; | |
| 46 | globals.document = before.document; | |
| 47 | }); | |
| 48 | ||
| 49 | /** A canvas 1000 wide: the stick is x < 380, the look and the trigger are past it. */ | |
| 50 | function harness(): { canvas: Canvas; controls: Controls } { | |
| 51 | const canvas = target({ clientWidth: 1000, clientHeight: 600 }); | |
| 52 | globals.window = target(); | |
| 53 | globals.document = target({ pointerLockElement: null }); | |
| 54 | const controls = createControls(canvas as unknown as HTMLCanvasElement, 2.2); | |
| 55 | return { canvas, controls }; | |
| 56 | } | |
| 57 | ||
| 58 | /** One touch point, as the browser would hand it over. */ | |
| 59 | const at = (identifier: number, clientX: number, clientY = 300): Record<string, unknown> => | |
| 60 | ({ identifier, clientX, clientY }); | |
| 61 | ||
| 62 | const touch = (canvas: Canvas, type: string, ...points: Record<string, unknown>[]): void => | |
| 63 | canvas.fire(type, { changedTouches: points }); | |
| 64 | ||
| 65 | /** Is `control` down on the first step of a frame of `steps` steps? */ | |
| 66 | const firstStep = (controls: Controls, control: string, steps = 2): boolean => | |
| 67 | downAt(controls.take(steps), control, 0); | |
| 68 | ||
| 69 | test("a second finger on the looking side fires, and the first keeps aiming", () => { | |
| 70 | const { canvas, controls } = harness(); | |
| 71 | touch(canvas, "touchstart", at(1, 700)); | |
| 72 | touch(canvas, "touchmove", at(1, 740)); | |
| 73 | const [turned] = controls.takeLook(); | |
| 74 | expect(turned).toBeGreaterThan(0); | |
| 75 | ||
| 76 | // The trigger lands while the aiming finger is still down. | |
| 77 | touch(canvas, "touchstart", at(2, 900, 500)); | |
| 78 | expect(firstStep(controls, "fire")).toBe(true); | |
| 79 | ||
| 80 | // ...and the aiming finger goes on steering, from where it actually is. | |
| 81 | touch(canvas, "touchmove", at(1, 780)); | |
| 82 | const [again] = controls.takeLook(); | |
| 83 | expect(again).toBeCloseTo(turned, 6); | |
| 84 | expect(firstStep(controls, "fire")).toBe(true); // still held | |
| 85 | ||
| 86 | touch(canvas, "touchend", at(2, 900, 500)); | |
| 87 | expect(firstStep(controls, "fire")).toBe(false); | |
| 88 | touch(canvas, "touchmove", at(1, 820)); | |
| 89 | expect(controls.takeLook()[0]).toBeCloseTo(turned, 6); | |
| 90 | }); | |
| 91 | ||
| 92 | test("the finger that fires does not steer", () => { | |
| 93 | const { canvas, controls } = harness(); | |
| 94 | touch(canvas, "touchstart", at(1, 700)); | |
| 95 | controls.takeLook(); | |
| 96 | touch(canvas, "touchstart", at(2, 900)); | |
| 97 | touch(canvas, "touchmove", at(2, 500, 100)); // a trigger finger twitching | |
| 98 | expect(controls.takeLook()).toEqual([0, 0]); | |
| 99 | }); | |
| 100 | ||
| 101 | test("a cancelled trigger releases: no stuck fire on a phone", () => { | |
| 102 | const { canvas, controls } = harness(); | |
| 103 | touch(canvas, "touchstart", at(1, 700)); | |
| 104 | touch(canvas, "touchstart", at(2, 900)); | |
| 105 | controls.take(1); | |
| 106 | touch(canvas, "touchcancel", at(2, 900)); | |
| 107 | expect(controls.take(1).held.has("fire")).toBe(false); | |
| 108 | }); | |
| 109 | ||
| 110 | test("two trigger fingers are one trigger: the last one up releases it", () => { | |
| 111 | const { canvas, controls } = harness(); | |
| 112 | touch(canvas, "touchstart", at(1, 700)); | |
| 113 | touch(canvas, "touchstart", at(2, 850), at(3, 950)); | |
| 114 | touch(canvas, "touchend", at(2, 850)); | |
| 115 | expect(controls.take(1).held.has("fire")).toBe(true); | |
| 116 | touch(canvas, "touchend", at(3, 950)); | |
| 117 | expect(controls.take(1).held.has("fire")).toBe(false); | |
| 118 | }); | |
| 119 | ||
| 120 | test("the left third is still the stick, and a second finger there does not fire", () => { | |
| 121 | const { canvas, controls } = harness(); | |
| 122 | touch(canvas, "touchstart", at(1, 100)); | |
| 123 | touch(canvas, "touchmove", at(1, 160)); | |
| 124 | const frame = controls.take(1); | |
| 125 | expect(controls.inputAt(frame, 0).side).toBeCloseTo(1, 6); | |
| 126 | expect(frame.held.has("fire")).toBe(false); | |
| 127 | touch(canvas, "touchend", at(1, 160)); | |
| 128 | expect(controls.inputAt(controls.take(1), 0).side).toBe(0); | |
| 129 | }); | |
| 130 | ||
| 131 | // --- the buttons on the pad, which are the same action names as the keys | |
| 132 | ||
| 133 | /** A fake `[data-action]` button, and a root that hands it to attachButtons. */ | |
| 134 | function pad(...actions: string[]): { | |
| 135 | root: ParentNode; | |
| 136 | press(action: string): void; | |
| 137 | release(action: string): void; | |
| 138 | classes(action: string): string[]; | |
| 139 | } { | |
| 140 | const buttons = new Map<string, Canvas>(); | |
| 141 | for (const action of actions) { | |
| 142 | const classes: string[] = []; | |
| 143 | buttons.set(action, target({ | |
| 144 | dataset: { action }, | |
| 145 | classList: { | |
| 146 | add: (name: string) => { if (!classes.includes(name)) classes.push(name); }, | |
| 147 | remove: (name: string) => { | |
| 148 | const at = classes.indexOf(name); | |
| 149 | if (at >= 0) classes.splice(at, 1); | |
| 150 | }, | |
| 151 | names: classes, | |
| 152 | }, | |
| 153 | })); | |
| 154 | } | |
| 155 | const root = { querySelectorAll: () => Array.from(buttons.values()) }; | |
| 156 | return { | |
| 157 | root: root as unknown as ParentNode, | |
| 158 | press: (action) => buttons.get(action)?.fire("pointerdown"), | |
| 159 | release: (action) => buttons.get(action)?.fire("pointerup"), | |
| 160 | classes: (action) => ((buttons.get(action)?.classList as { names: string[] }).names), | |
| 161 | }; | |
| 162 | } | |
| 163 | ||
| 164 | test("the menu button pauses without pointer lock", () => { | |
| 165 | const { controls } = harness(); | |
| 166 | const buttons = pad("menu"); | |
| 167 | attachButtons(buttons.root, controls); | |
| 168 | let paused = 0; | |
| 169 | controls.onPause = () => { paused += 1; }; | |
| 170 | expect(controls.locked).toBe(false); | |
| 171 | buttons.press("menu"); | |
| 172 | expect(paused).toBe(1); | |
| 173 | buttons.release("menu"); | |
| 174 | expect(paused).toBe(1); // the release is not a second pause | |
| 175 | }); | |
| 176 | ||
| 177 | test("the pad's slot, scoreboard and buy buttons are the keyboard's actions", () => { | |
| 178 | const { controls } = harness(); | |
| 179 | const buttons = pad("slot1", "slot2", "slot3", "scoreboard", "buy"); | |
| 180 | attachButtons(buttons.root, controls); | |
| 181 | let buys = 0; | |
| 182 | controls.onBuy = () => { buys += 1; }; | |
| 183 | ||
| 184 | expect(controls.slotWanted).toBe(null); | |
| 185 | for (const [action, slot] of [["slot1", 1], ["slot2", 2], ["slot3", 3]] as const) { | |
| 186 | buttons.press(action); | |
| 187 | expect(controls.slotWanted).toBe(slot); | |
| 188 | buttons.release(action); | |
| 189 | } | |
| 190 | expect(controls.slotWanted).toBe(null); | |
| 191 | ||
| 192 | expect(controls.scoreboard).toBe(false); | |
| 193 | buttons.press("scoreboard"); | |
| 194 | expect(controls.scoreboard).toBe(true); // held, not a toggle | |
| 195 | expect(buttons.classes("scoreboard")).toEqual(["on"]); // and it looks held | |
| 196 | buttons.release("scoreboard"); | |
| 197 | expect(controls.scoreboard).toBe(false); | |
| 198 | expect(buttons.classes("scoreboard")).toEqual([]); | |
| 199 | ||
| 200 | buttons.press("buy"); | |
| 201 | expect(buys).toBe(1); | |
| 202 | }); |
src/client/input.ts +41 −9
| @@ -6,6 +6,10 @@ | ||
| 6 | 6 | * Events are edges, and the loop only looks once a frame, so press.ts holds |
| 7 | 7 | * the edges in between: a tap that starts and ends between two frames still |
| 8 | 8 | * fires, exactly once. |
| 9 | * | |
| 10 | * `lookX`/`lookY` accumulate in exactly two places — a locked mouse move and a | |
| 11 | * touch whose role is `look` — and are drained once a frame by `takeLook`. | |
| 12 | * Buttons never touch them; they only ever name an action. | |
| 9 | 13 | */ |
| 10 | 14 | |
| 11 | 15 | import { clamp } from "./math.ts"; |
| @@ -78,7 +82,12 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co | ||
| 78 | 82 | setAction: (action, down) => { |
| 79 | 83 | if (down) press.down(action); |
| 80 | 84 | else press.up(action); |
| 81 | if (down && action === "buy") controls.onBuy(); | |
| 85 | if (!down) return; | |
| 86 | // The two actions that are a menu rather than a movement. `menu` is how a | |
| 87 | // touch screen reaches the pause overlay at all: pointer lock is the only | |
| 88 | // other thing that calls onPause, and a finger never takes the lock. | |
| 89 | if (action === "buy") controls.onBuy(); | |
| 90 | else if (action === "menu") controls.onPause(); | |
| 82 | 91 | }, |
| 83 | 92 | onBuy: () => {}, |
| 84 | 93 | onPause: () => {}, |
| @@ -132,14 +141,28 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co | ||
| 132 | 141 | press.clear(); |
| 133 | 142 | }); |
| 134 | 143 | |
| 135 | // --- touch: the left third is a stick, the rest of the canvas looks around | |
| 136 | const touches = new Map<number, { startX: number; startY: number; x: number; y: number; stick: boolean }>(); | |
| 144 | // --- touch: the left third is a stick, the rest of the canvas looks around, | |
| 145 | // and a second finger on the looking side is the trigger. That is the whole | |
| 146 | // of firing while aiming: the touch that steers keeps its own target for its | |
| 147 | // whole life, so a shot never costs the aim the thumb already has. A touch | |
| 148 | // that fires does not steer — a trigger finger twitches, and the aim should | |
| 149 | // not follow it. | |
| 150 | type Role = "stick" | "look" | "fire"; | |
| 151 | type Held = { startX: number; startY: number; x: number; y: number; role: Role }; | |
| 152 | const touches = new Map<number, Held>(); | |
| 153 | const anyRole = (role: Role): boolean => { | |
| 154 | for (const held of touches.values()) if (held.role === role) return true; | |
| 155 | return false; | |
| 156 | }; | |
| 137 | 157 | canvas.addEventListener("touchstart", (event) => { |
| 138 | 158 | for (const touch of Array.from(event.changedTouches)) { |
| 139 | const isStick = touch.clientX < canvas.clientWidth * 0.38; | |
| 159 | const role: Role = touch.clientX < canvas.clientWidth * 0.38 | |
| 160 | ? "stick" | |
| 161 | : anyRole("look") ? "fire" : "look"; | |
| 140 | 162 | touches.set(touch.identifier, { |
| 141 | startX: touch.clientX, startY: touch.clientY, x: touch.clientX, y: touch.clientY, stick: isStick, | |
| 163 | startX: touch.clientX, startY: touch.clientY, x: touch.clientX, y: touch.clientY, role, | |
| 142 | 164 | }); |
| 165 | if (role === "fire") controls.setAction("fire", true); | |
| 143 | 166 | } |
| 144 | 167 | event.preventDefault(); |
| 145 | 168 | }, { passive: false }); |
| @@ -147,10 +170,10 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co | ||
| 147 | 170 | for (const touch of Array.from(event.changedTouches)) { |
| 148 | 171 | const state = touches.get(touch.identifier); |
| 149 | 172 | if (state === undefined) continue; |
| 150 | if (state.stick) { | |
| 173 | if (state.role === "stick") { | |
| 151 | 174 | stick.x = clamp((touch.clientX - state.startX) / 60, -1, 1); |
| 152 | 175 | stick.y = clamp((state.startY - touch.clientY) / 60, -1, 1); |
| 153 | } else { | |
| 176 | } else if (state.role === "look") { | |
| 154 | 177 | lookX += (touch.clientX - state.x) * controls.sensitivity * 0.06; |
| 155 | 178 | lookY += (touch.clientY - state.y) * controls.sensitivity * 0.06; |
| 156 | 179 | } |
| @@ -162,11 +185,15 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co | ||
| 162 | 185 | const endTouch = (event: TouchEvent): void => { |
| 163 | 186 | for (const touch of Array.from(event.changedTouches)) { |
| 164 | 187 | const state = touches.get(touch.identifier); |
| 165 | if (state?.stick === true) { | |
| 188 | touches.delete(touch.identifier); | |
| 189 | if (state === undefined) continue; | |
| 190 | if (state.role === "stick") { | |
| 166 | 191 | stick.x = 0; |
| 167 | 192 | stick.y = 0; |
| 168 | 193 | } |
| 169 | touches.delete(touch.identifier); | |
| 194 | // The last finger off the trigger is the one that releases it: a stuck | |
| 195 | // trigger on a phone is unrecoverable without reloading the page. | |
| 196 | if (state.role === "fire" && !anyRole("fire")) controls.setAction("fire", false); | |
| 170 | 197 | } |
| 171 | 198 | }; |
| 172 | 199 | canvas.addEventListener("touchend", endTouch); |
| @@ -183,12 +210,17 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co | ||
| 183 | 210 | export function attachButtons(root: ParentNode, controls: Controls): void { |
| 184 | 211 | for (const element of Array.from(root.querySelectorAll<HTMLElement>("[data-action]"))) { |
| 185 | 212 | const action = element.dataset.action as string; |
| 213 | // preventDefault keeps the browser from making a second event out of a | |
| 214 | // touch, which also costs the button its :active state — so the class is | |
| 215 | // the one that says "held", and a held control looks held. | |
| 186 | 216 | const down = (event: Event): void => { |
| 187 | 217 | event.preventDefault(); |
| 218 | element.classList.add("on"); | |
| 188 | 219 | controls.setAction(action, true); |
| 189 | 220 | }; |
| 190 | 221 | const up = (event: Event): void => { |
| 191 | 222 | event.preventDefault(); |
| 223 | element.classList.remove("on"); | |
| 192 | 224 | controls.setAction(action, false); |
| 193 | 225 | }; |
| 194 | 226 | element.addEventListener("pointerdown", down); |
src/client/main.ts +42 −10
| @@ -71,6 +71,25 @@ function boot(): void { | ||
| 71 | 71 | element.hidden = !on; |
| 72 | 72 | }; |
| 73 | 73 | |
| 74 | // A touch screen has no pointer lock — asking for it there is a promise that | |
| 75 | // rejects — so the phone build never asks, and the look comes off the glass. | |
| 76 | const coarse = matchMedia("(pointer: coarse)").matches; | |
| 77 | const lock = (): void => { | |
| 78 | if (!coarse) void scene.requestPointerLock(); | |
| 79 | }; | |
| 80 | /** | |
| 81 | * The touch pad is up only while a match is being played and nothing is over | |
| 82 | * the view: an overlay is a dialog, and a jump button still live underneath | |
| 83 | * one is a player moving where they cannot see. | |
| 84 | */ | |
| 85 | const refreshPad = (): void => { | |
| 86 | show(touchPanel, coarse && running); | |
| 87 | // Hidden, not removed: an overlay comes and goes every freeze time, and a | |
| 88 | // pad that left the flow would drag the page under it up and down with it. | |
| 89 | touchPanel.classList.toggle( | |
| 90 | "blocked", !(pause.hidden && buyPanel.hidden && summaryPanel.hidden)); | |
| 91 | }; | |
| 92 | ||
| 74 | 93 | // --- the buy menu, rebuilt whenever it opens so the prices are honest |
| 75 | 94 | function renderBuy(): void { |
| 76 | 95 | if (world === null) return; |
| @@ -83,7 +102,9 @@ function boot(): void { | ||
| 83 | 102 | ]; |
| 84 | 103 | buyPanel.innerHTML = |
| 85 | 104 | `<h2>Buy <small>$${me.money}</small></h2><div class="grid"></div>` + |
| 86 | `<p class="hint">B or Escape closes. You can only buy in the freeze time.</p>`; | |
| 105 | `<div class="row"><button type="button" id="buy-close">Close</button></div>` + | |
| 106 | `<p class="hint">B, Escape or Close closes. You can only buy in the freeze time.</p>`; | |
| 107 | $("buy-close").addEventListener("click", () => openBuy(false)); | |
| 87 | 108 | const grid = buyPanel.querySelector(".grid") as HTMLElement; |
| 88 | 109 | for (const [item, label] of items) { |
| 89 | 110 | const price = priceOf(item, me); |
| @@ -104,18 +125,23 @@ function boot(): void { | ||
| 104 | 125 | if (world === null) return; |
| 105 | 126 | if (open && !canBuy(world, world.me)) return; |
| 106 | 127 | show(buyPanel, open); |
| 128 | refreshPad(); | |
| 107 | 129 | if (open) { |
| 108 | 130 | renderBuy(); |
| 109 | 131 | document.exitPointerLock(); |
| 110 | 132 | (buyPanel.querySelector("button") as HTMLButtonElement | null)?.focus(); |
| 111 | 133 | } else if (running) { |
| 112 | void scene.requestPointerLock(); | |
| 134 | lock(); | |
| 113 | 135 | } |
| 114 | 136 | } |
| 115 | 137 | |
| 116 | 138 | controls.onBuy = () => openBuy(buyPanel.hidden); |
| 117 | 139 | controls.onPause = () => { |
| 118 | if (running && buyPanel.hidden) show(pause, true); | |
| 140 | if (running && buyPanel.hidden) { | |
| 141 | show(pause, true); | |
| 142 | refreshPad(); | |
| 143 | $("resume").focus(); | |
| 144 | } | |
| 119 | 145 | }; |
| 120 | 146 | |
| 121 | 147 | // --- results, sent as the match goes rather than only at the end |
| @@ -179,24 +205,27 @@ function boot(): void { | ||
| 179 | 205 | show(menu, false); |
| 180 | 206 | show(pause, false); |
| 181 | 207 | show(summaryPanel, false); |
| 182 | show(touchPanel, matchMedia("(pointer: coarse)").matches); | |
| 208 | show(buyPanel, false); | |
| 209 | refreshPad(); | |
| 183 | 210 | audio.resume(); |
| 184 | void scene.requestPointerLock(); | |
| 211 | lock(); | |
| 185 | 212 | localStorage.setItem("cs.name", name); |
| 186 | 213 | } |
| 187 | 214 | |
| 188 | 215 | $("play").addEventListener("click", start); |
| 189 | 216 | $("resume").addEventListener("click", () => { |
| 190 | 217 | show(pause, false); |
| 191 | void scene.requestPointerLock(); | |
| 218 | refreshPad(); | |
| 219 | lock(); | |
| 192 | 220 | }); |
| 193 | 221 | const toMenu = (): void => { |
| 194 | 222 | running = false; |
| 195 | 223 | world = null; |
| 196 | 224 | show(pause, false); |
| 197 | 225 | show(summaryPanel, false); |
| 226 | show(buyPanel, false); | |
| 198 | 227 | show(menu, true); |
| 199 | show(touchPanel, false); | |
| 228 | refreshPad(); | |
| 200 | 229 | $("play").focus(); |
| 201 | 230 | }; |
| 202 | 231 | $("quit").addEventListener("click", toMenu); |
| @@ -243,7 +272,10 @@ function boot(): void { | ||
| 243 | 272 | |
| 244 | 273 | const me = world.me; |
| 245 | 274 | const [dx, dy] = controls.takeLook(); |
| 246 | if (me.alive && controls.locked) { | |
| 275 | // Whatever look has accumulated is aim: the mouse only adds to it under | |
| 276 | // lock and a finger only adds to it on the canvas, so there is nothing to | |
| 277 | // gate here — and gating on the lock is what left a phone unable to turn. | |
| 278 | if (me.alive) { | |
| 247 | 279 | me.yaw -= dx; |
| 248 | 280 | me.pitch = clamp(me.pitch + dy, -89, 89); |
| 249 | 281 | } |
| @@ -263,7 +295,7 @@ function boot(): void { | ||
| 263 | 295 | for (let step = 0; step < steps; step += 1) { |
| 264 | 296 | const input = controls.inputAt(pressed, step); |
| 265 | 297 | input.switchTo = switchTo; |
| 266 | if (!buyPanel.hidden) input.fire = false; | |
| 298 | if (!buyPanel.hidden || !pause.hidden) input.fire = false; | |
| 267 | 299 | if (!world.me.alive && input.fire) cycle = true; |
| 268 | 300 | update(world, TICK, input); |
| 269 | 301 | accumulator -= TICK; |
| @@ -283,7 +315,7 @@ function boot(): void { | ||
| 283 | 315 | posted = played; |
| 284 | 316 | document.exitPointerLock(); |
| 285 | 317 | show(buyPanel, false); |
| 286 | show(touchPanel, false); | |
| 318 | refreshPad(); | |
| 287 | 319 | // The last post is the only one that says the match finished, and the |
| 288 | 320 | // score it carries is the one the overlay is about to show. |
| 289 | 321 | const summary = summarize(world); |
src/pages/game.ts +48 −16
| @@ -51,7 +51,10 @@ small.commit { color:var(--dim); font-size:.75rem; } | ||
| 51 | 51 | , { position:absolute; inset:0; width:100%; height:100%; display:block; } |
| 52 | 52 | { pointer-events:none; } |
| 53 | 53 | .overlay { position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center; |
| 54 | gap:.75rem; background:rgba(8,10,14,.82); text-align:center; padding:1rem; overflow:auto; } | |
| 54 | gap:.75rem; background:rgba(8,10,14,.82); text-align:center; padding:1rem; overflow:auto; | |
| 55 | /* #stage is touch-action:none for the look drag; an overlay taller | |
| 56 | than the stage still has to scroll under a thumb. */ | |
| 57 | touch-action:pan-y; overscroll-behavior:contain; } | |
| 55 | 58 | .overlay[hidden] { display:none; } |
| 56 | 59 | .overlay h2 { margin:0; font-size:1.3rem; } |
| 57 | 60 | .overlay p { margin:.2rem 0; color:var(--dim); max-width:34rem; } |
| @@ -70,10 +73,26 @@ button.primary { background:#2563eb; border-color:#2563eb; font-weight:600; } | ||
| 70 | 73 | h2 { font-size:1.8rem; } |
| 71 | 74 | -line { color:var(--text); font-size:.95rem; } |
| 72 | 75 | { position:absolute; right:.5rem; bottom:.4rem; font-size:.7rem; color:ea; opacity:.7; pointer-events:none; } |
| 73 | { position:absolute; inset:auto 0 0 0; display:flex; justify-content:space-between; padding:.6rem; pointer-events:none; } | |
| 76 | /* The pad is under the view, not over it: the stage is 4:3 on a phone and the | |
| 77 | HUD already owns all four of its corners, so a button on the glass would sit | |
| 78 | on the radar, the health or the ammo. Aim, move and fire stay on the canvas | |
| 79 | — the pad is only the discrete actions. */ | |
| 80 | { max-width:34rem; margin:0 auto 1.5rem; display:flex; flex-direction:column; gap:.4rem; | |
| 81 | touch-action:manipulation; -webkit-user-select:none; user-select:none; -webkit-touch-callout:none; } | |
| 74 | 82 | [hidden] { display:none; } |
| 75 | .pad { display:flex; gap:.4rem; flex-wrap:wrap; pointer-events:auto; } | |
| 76 | button { opacity:.72; background:f131acc; } | |
| 83 | .blocked { visibility:hidden; } | |
| 84 | .pad { display:grid; grid-template-columns:repeat(auto-fit,minmax(3.4rem,1fr)); gap:.4rem; } | |
| 85 | .pad.wide { grid-template-columns:1fr; } | |
| 86 | button { display:flex; flex-direction:column; align-items:center; justify-content:center; gap:.05rem; | |
| 87 | min-height:48px; min-width:44px; padding:.4rem .25rem; font-size:.82rem; line-height:1.15; } | |
| 88 | button b { color:var(--dim); font-size:.68rem; font-weight:600; } | |
| 89 | button.fire { background:f1d1d; border-color:f2222; font-weight:600; } | |
| 90 | /* A thumb has no hover, so held has to look held — and the "on" class is put | |
| 91 | there by the same handler that presses the control, since preventDefault | |
| 92 | costs a touched button its :active. */ | |
| 93 | button.on { background:3405a; border-color:a5c7d; } | |
| 94 | button.fire.on { background:; border-color:; } | |
| 95 | .hint { margin:0; text-align:center; color:var(--dim); font-size:.74rem; } | |
| 77 | 96 | main section { margin:1.5rem 0; } |
| 78 | 97 | table { width:100%; border-collapse:collapse; font-size:.9rem; } |
| 79 | 98 | th, td { text-align:left; padding:.35rem .5rem; border-bottom:1px solid var(--line); } |
| @@ -114,18 +133,6 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 114 | 133 | <canvas id="scene"></canvas> |
| 115 | 134 | <canvas id="hud"></canvas> |
| 116 | 135 | <div id="fps"></div> |
| 117 | <div id="touch" hidden> | |
| 118 | <div class="pad"> | |
| 119 | <button type="button" data-action="duck">crouch</button> | |
| 120 | <button type="button" data-action="jump">jump</button> | |
| 121 | <button type="button" data-action="use">use</button> | |
| 122 | </div> | |
| 123 | <div class="pad"> | |
| 124 | <button type="button" data-action="reload">reload</button> | |
| 125 | <button type="button" data-action="buy">buy</button> | |
| 126 | <button type="button" data-action="fire">fire</button> | |
| 127 | </div> | |
| 128 | </div> | |
| 129 | 136 | <div class="overlay" id="menu"> |
| 130 | 137 | <h2>Defuse or detonate</h2> |
| 131 | 138 | <p>Five against five on <b>de_forge</b>. Bomb rounds, CS movement and economy, bots that push the sites.</p> |
| @@ -139,6 +146,9 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 139 | 146 | <p class="hint"><kbd>WASD</kbd> move · <kbd>Space</kbd> jump · <kbd>Ctrl</kbd> crouch · <kbd>Shift</kbd> walk · |
| 140 | 147 | mouse to aim and fire · <kbd>R</kbd> reload · <kbd>E</kbd> plant or defuse · <kbd>B</kbd> buy · |
| 141 | 148 | <kbd>1</kbd><kbd>2</kbd><kbd>3</kbd> guns · <kbd>Tab</kbd> scores · <kbd>Esc</kbd> pause</p> |
| 149 | <p class="hint">On a phone: the left of the view is a thumb stick, the right aims, a second | |
| 150 | finger on the right fires, and the pad under the view does the rest — guns, scores, buy | |
| 151 | and a menu you can leave the match from.</p> | |
| 142 | 152 | </div> |
| 143 | 153 | <div class="overlay" id="pause" hidden> |
| 144 | 154 | <h2>Paused</h2> |
| @@ -157,6 +167,28 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 157 | 167 | <div class="overlay" id="buy" hidden></div> |
| 158 | 168 | </div> |
| 159 | 169 | |
| 170 | <div id="touch" hidden> | |
| 171 | <div class="pad"> | |
| 172 | <button type="button" data-action="slot1"><b>1</b>rifle</button> | |
| 173 | <button type="button" data-action="slot2"><b>2</b>pistol</button> | |
| 174 | <button type="button" data-action="slot3"><b>3</b>knife</button> | |
| 175 | <button type="button" data-action="scoreboard">scores</button> | |
| 176 | <button type="button" data-action="menu">menu</button> | |
| 177 | </div> | |
| 178 | <div class="pad"> | |
| 179 | <button type="button" data-action="duck">crouch</button> | |
| 180 | <button type="button" data-action="jump">jump</button> | |
| 181 | <button type="button" data-action="use">use</button> | |
| 182 | <button type="button" data-action="reload">reload</button> | |
| 183 | <button type="button" data-action="buy">buy</button> | |
| 184 | </div> | |
| 185 | <div class="pad wide"> | |
| 186 | <button type="button" class="fire" data-action="fire">fire</button> | |
| 187 | </div> | |
| 188 | <p class="hint">Left of the view is the stick, the right aims — and a second | |
| 189 | finger anywhere on the right fires without moving your aim.</p> | |
| 190 | </div> | |
| 191 | ||
| 160 | 192 | <section id="stats">${statsHtml(db)}</section> |
| 161 | 193 | |
| 162 | 194 | <section> |
src/server.test.ts +12
| @@ -34,6 +34,18 @@ test("serves the game page with the leaderboard in the first paint", async () => | ||
| 34 | 34 | expect(body).toMatch(/<script type="module" src="\/static\/app\.[0-9a-f]+\.js">/); |
| 35 | 35 | }); |
| 36 | 36 | |
| 37 | test("the touch pad is in the first paint, and it names every action a match needs", async () => { | |
| 38 | // Markup asks for behaviour: the CSP forbids inline script, so a control is | |
| 39 | // a [data-action] in the page and nothing else. A pad missing one of these | |
| 40 | // is a phone that cannot switch guns, see the scores or leave the match. | |
| 41 | const body = await (await fetch(base + "/")).text(); | |
| 42 | const pad = body.slice(body.indexOf(`<div id="touch"`), body.indexOf("</div>\n\n <section")); | |
| 43 | for (const action of ["slot1", "slot2", "slot3", "scoreboard", "menu", "duck", "jump", "use", "reload", "buy", "fire"]) | |
| 44 | expect(pad).toContain(`data-action="${action}"`); | |
| 45 | // ...and it is under the view, not on it: the HUD owns the corners of #stage. | |
| 46 | expect(body.indexOf(`<div id="touch"`)).toBeGreaterThan(body.indexOf(`<div class="overlay" id="buy"`)); | |
| 47 | }); | |
| 48 | ||
| 37 | 49 | test("a first visit is given a session cookie that javascript cannot read", async () => { |
| 38 | 50 | const response = await fetch(base + "/"); |
| 39 | 51 | const cookie = response.headers.get("set-cookie") ?? ""; |
static/app.642d83d9b8.js deleted −58
Diff not shown — a line 62,866 characters wide (limit 2,000). −58
View filestatic/app.942a1ec39a.js added +58
Diff not shown — a line 63,210 characters wide (limit 2,000). +58
View file
new above
offline