// Keys, a thumb stick on touch screens, and gamepads, all read as one input: // { throttle, brake, steer, boost, hop }. export function createInput(target, { onAny, onPause, onRestart } = {}) { const keys = new Set(); let hop = false; let boostHeld = false; const touch = { id: null, x0: 0, y0: 0, x: 0, y: 0 }; const stick = document.createElement("div"); stick.className = "stick"; stick.innerHTML = ""; const hopBtn = document.createElement("button"); hopBtn.className = "hop"; hopBtn.textContent = "Hop"; const boostBtn = document.createElement("button"); boostBtn.className = "boost"; boostBtn.textContent = "Boost"; const isTouch = matchMedia("(pointer: coarse)").matches || "ontouchstart" in window; if (isTouch) document.body.append(stick, hopBtn, boostBtn); boostBtn.addEventListener("touchstart", (e) => { boostHeld = true; onAny?.(); e.preventDefault(); e.stopPropagation(); }); for (const type of ["touchend", "touchcancel"]) boostBtn.addEventListener(type, () => (boostHeld = false)); addEventListener("keydown", (e) => { if (e.repeat) return; keys.add(e.code); if (e.code === "Space") { hop = true; e.preventDefault(); } if (e.code === "Escape" || e.code === "KeyP") onPause?.(); else if (e.code === "KeyR") onRestart?.(); else onAny?.(); if (e.code.startsWith("Arrow")) e.preventDefault(); }); addEventListener("keyup", (e) => keys.delete(e.code)); addEventListener("blur", () => keys.clear()); target.addEventListener( "touchstart", (e) => { onAny?.(); for (const t of e.changedTouches) { if (t.clientX < innerWidth * 0.6 && touch.id === null) { touch.id = t.identifier; touch.x0 = touch.x = t.clientX; touch.y0 = touch.y = t.clientY; stick.style.left = `${t.clientX}px`; stick.style.top = `${t.clientY}px`; stick.classList.add("on"); } else if (t.clientX >= innerWidth * 0.6) hop = true; } e.preventDefault(); }, { passive: false }, ); target.addEventListener( "touchmove", (e) => { for (const t of e.changedTouches) if (t.identifier === touch.id) { touch.x = t.clientX; touch.y = t.clientY; const dx = Math.max(-50, Math.min(50, touch.x - touch.x0)); const dy = Math.max(-50, Math.min(50, touch.y - touch.y0)); stick.querySelector("i").style.transform = `translate(${dx}px, ${dy}px)`; } e.preventDefault(); }, { passive: false }, ); const end = (e) => { for (const t of e.changedTouches) if (t.identifier === touch.id) { touch.id = null; stick.classList.remove("on"); stick.querySelector("i").style.transform = ""; } }; target.addEventListener("touchend", end); target.addEventListener("touchcancel", end); hopBtn.addEventListener("touchstart", (e) => { hop = true; onAny?.(); e.preventDefault(); e.stopPropagation(); }); target.addEventListener("mousedown", () => onAny?.()); return { isTouch, read() { let throttle = keys.has("ArrowUp") || keys.has("KeyW") ? 1 : 0; let brake = keys.has("ArrowDown") || keys.has("KeyS") ? 1 : 0; let steer = (keys.has("ArrowRight") || keys.has("KeyD") ? 1 : 0) - (keys.has("ArrowLeft") || keys.has("KeyA") ? 1 : 0); let boost = keys.has("ShiftLeft") || keys.has("ShiftRight") || boostHeld; if (touch.id !== null) { const dx = (touch.x - touch.x0) / 50; const dy = (touch.y - touch.y0) / 50; steer = Math.max(-1, Math.min(1, dx)); throttle = Math.max(0, Math.min(1, -dy * 1.3)); brake = Math.max(0, Math.min(1, dy * 1.3)); } for (const pad of navigator.getGamepads?.() ?? []) { if (!pad) continue; const ax = pad.axes[0] ?? 0; if (Math.abs(ax) > 0.15) steer = ax; throttle = Math.max(throttle, pad.buttons[7]?.value ?? 0); brake = Math.max(brake, pad.buttons[6]?.value ?? 0); if (pad.buttons[0]?.pressed) hop = true; if (pad.buttons[5]?.pressed || pad.buttons[2]?.pressed) boost = true; } // Boosting means going: Shift (or the Boost button) is full throttle and then some. if (boost && brake === 0) throttle = 1; const out = { throttle, brake, steer, boost, hop }; hop = false; return out; }, }; }