// Unified input: keyboard, touch joystick + action button, mouse/touch camera drag, gamepad. export interface InputState { moveX: number; moveY: number; // -1..1, y = forward camX: number; camY: number; // camera drag delta this frame (pixels) or stick action: boolean; // jump/catch button held actionPressed: boolean; // went down this frame pausePressed: boolean; usedTouch: boolean; moved: boolean; } export class Input { s: InputState = { moveX: 0, moveY: 0, camX: 0, camY: 0, action: false, actionPressed: false, pausePressed: false, usedTouch: false, moved: false }; private keys = new Set(); private joyId: number | null = null; private joyO = { x: 0, y: 0 }; private joyV = { x: 0, y: 0 }; private camId: number | null = null; private camLast = { x: 0, y: 0 }; private camAcc = { x: 0, y: 0 }; private btnDown = false; private prevAction = false; private pauseQ = false; private tapQ = false; private prevPadPause: boolean[] = []; /** Player 2: the second connected controller (null when there is none). */ p2: InputState | null = null; private p2Prev = false; stickEl: HTMLElement | null = null; enabled = true; constructor(private canvas: HTMLElement) { addEventListener('keydown', e => { if ((e.target as HTMLElement | null)?.tagName === 'INPUT') return; // typing a cat's name if (['Space', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(e.code)) e.preventDefault(); if (e.code === 'Escape' || e.code === 'KeyP') this.pauseQ = true; if (!e.repeat && (e.code === 'Space' || e.code === 'Enter' || e.code === 'KeyJ')) this.tapQ = true; this.keys.add(e.code); }); addEventListener('keyup', e => this.keys.delete(e.code)); addEventListener('blur', () => { this.keys.clear(); this.btnDown = false; this.joyId = null; this.joyV = { x: 0, y: 0 }; }); canvas.addEventListener('pointerdown', e => { if (e.pointerType === 'mouse') { this.camId = e.pointerId; this.camLast = { x: e.clientX, y: e.clientY }; canvas.setPointerCapture(e.pointerId); return; } this.s.usedTouch = true; if (e.clientX < innerWidth * 0.5 && this.joyId === null) { this.joyId = e.pointerId; this.joyO = { x: e.clientX, y: e.clientY }; this.joyV = { x: 0, y: 0 }; if (this.stickEl) { this.stickEl.style.display = 'block'; this.stickEl.style.left = e.clientX + 'px'; this.stickEl.style.top = e.clientY + 'px'; } } else if (this.camId === null) { this.camId = e.pointerId; this.camLast = { x: e.clientX, y: e.clientY }; } }); addEventListener('pointermove', e => { if (e.pointerId === this.joyId) { let dx = e.clientX - this.joyO.x, dy = e.clientY - this.joyO.y; const R = 55; const l = Math.hypot(dx, dy); if (l > R) { // dragging far moves the stick base along (floating joystick) this.joyO.x += dx - dx / l * R; this.joyO.y += dy - dy / l * R; dx = dx / l * R; dy = dy / l * R; if (this.stickEl) { this.stickEl.style.left = this.joyO.x + 'px'; this.stickEl.style.top = this.joyO.y + 'px'; } } this.joyV = { x: dx / R, y: -dy / R }; const knob = this.stickEl?.firstElementChild as HTMLElement | null; if (knob) knob.style.transform = `translate(${dx}px,${dy}px)`; } else if (e.pointerId === this.camId) { this.camAcc.x += e.clientX - this.camLast.x; this.camAcc.y += e.clientY - this.camLast.y; this.camLast = { x: e.clientX, y: e.clientY }; } }); const up = (e: PointerEvent) => { if (e.pointerId === this.joyId) { this.joyId = null; this.joyV = { x: 0, y: 0 }; if (this.stickEl) this.stickEl.style.display = 'none'; } if (e.pointerId === this.camId) this.camId = null; }; addEventListener('pointerup', up); addEventListener('pointercancel', up); } bindButton(el: HTMLElement) { el.addEventListener('pointerdown', e => { e.preventDefault(); e.stopPropagation(); this.btnDown = true; this.tapQ = true; this.s.usedTouch = e.pointerType !== 'mouse' || this.s.usedTouch; try { el.setPointerCapture(e.pointerId); } catch { /* ignore */ } }); const off = () => { this.btnDown = false; }; el.addEventListener('pointerup', off); el.addEventListener('pointercancel', off); el.addEventListener('lostpointercapture', off); } update(): InputState { const s = this.s, k = this.keys; let mx = (k.has('KeyD') || k.has('ArrowRight') ? 1 : 0) - (k.has('KeyA') || k.has('ArrowLeft') ? 1 : 0); let my = (k.has('KeyW') || k.has('ArrowUp') ? 1 : 0) - (k.has('KeyS') || k.has('ArrowDown') ? 1 : 0); let cx = this.camAcc.x + ((k.has('KeyE') ? 1 : 0) - (k.has('KeyQ') ? 1 : 0)) * 8, cy = this.camAcc.y; this.camAcc = { x: 0, y: 0 }; let act = k.has('Space') || k.has('Enter') || k.has('KeyJ') || this.btnDown; let pause = this.pauseQ; this.pauseQ = false; const pads = navigator.getGamepads ? navigator.getGamepads() : []; const dz = (v: number) => (Math.abs(v) < 0.18 ? 0 : v); // the first controller plays player 1 (with the keyboard and touch); the second controller plays player 2 let n = 0, p2: InputState | null = null; for (const p of pads) { if (!p) continue; n++; const pp = !!p.buttons[9]?.pressed; if (pp && !this.prevPadPause[p.index]) pause = true; this.prevPadPause[p.index] = pp; const b = [0, 1, 2, 3].some(i => p.buttons[i]?.pressed); if (n === 2) { let x = dz(p.axes[0] ?? 0), y = -dz(p.axes[1] ?? 0); if (p.buttons[14]?.pressed) x -= 1; if (p.buttons[15]?.pressed) x += 1; if (p.buttons[12]?.pressed) y += 1; if (p.buttons[13]?.pressed) y -= 1; const l2 = Math.hypot(x, y); if (l2 > 1) { x /= l2; y /= l2; } if (!this.enabled) { x = 0; y = 0; } const a = b && this.enabled; p2 = { moveX: x, moveY: y, camX: 0, camY: 0, action: a, actionPressed: a && !this.p2Prev, pausePressed: false, usedTouch: false, moved: l2 > 0.2 }; this.p2Prev = a; continue; } if (n > 2) continue; mx += dz(p.axes[0] ?? 0); my -= dz(p.axes[1] ?? 0); cx += dz(p.axes[2] ?? 0) * 14; cy += dz(p.axes[3] ?? 0) * 10; if (p.buttons[14]?.pressed) mx -= 1; if (p.buttons[15]?.pressed) mx += 1; if (p.buttons[12]?.pressed) my += 1; if (p.buttons[13]?.pressed) my -= 1; act = act || b; } if (!p2) this.p2Prev = false; this.p2 = p2; mx += this.joyV.x; my += this.joyV.y; const l = Math.hypot(mx, my); if (l > 1) { mx /= l; my /= l; } if (!this.enabled) { mx = 0; my = 0; act = false; } s.moveX = mx; s.moveY = my; s.camX = cx; s.camY = cy; // a tap that starts and ends between two frames still counts (slow devices) s.actionPressed = (act && !this.prevAction) || (this.tapQ && this.enabled); s.action = act || (this.tapQ && this.enabled); this.prevAction = act; this.tapQ = false; s.pausePressed = pause; if (l > 0.2) s.moved = true; return s; } }