Skip to content

gitflarePublicPartly agent-ready40

neon-drift

Real-time multiplayer arcade racer on Cloudflare Workers and Durable Objects.

Add the Apache-2.0 licence3efe3da
JavaScript51 lines2.1 KB
export let activeInput = null;export function createInput(target) {  const input = { turn: 0, boost: 0, touch: { active: 0, side: 0, boost: 0, isTouch: 0 } };  activeInput = input;  const t = input.touch;  const reduce = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;  input.reduceMotion = reduce ? 1 : 0;  if (typeof matchMedia === 'function' && matchMedia('(pointer: coarse)').matches) t.isTouch = 1;  let left = 0, right = 0, key = 0, touchTurn = 0, touchBoost = 0, lastTap = 0, ptrs = 0;  const upd = () => {    input.turn = right - left || touchTurn;    input.boost = key || touchBoost;    t.side = touchTurn; t.boost = touchBoost; t.active = ptrs > 0 ? 1 : 0;  };  const btn = new Set();  const set = (e, down) => {    const k = e.key;    if (k === 'ArrowLeft' || k === 'a' || k === 'A') left = down ? 1 : 0;    else if (k === 'ArrowRight' || k === 'd' || k === 'D') right = down ? 1 : 0;    else if (k === ' ') key = down ? 1 : 0;    else return;    e.preventDefault();    upd();  };  addEventListener('keydown', (e) => set(e, true));  addEventListener('keyup', (e) => set(e, false));  target.addEventListener('pointerdown', (e) => {    if (e.pointerType === 'touch') t.isTouch = 1;    ptrs++;    const now = performance.now();    if (now - lastTap < 300 || ptrs >= 2) touchBoost = 1;    lastTap = now;    const bx = innerWidth - 56, by = innerHeight - 150;    if ((e.clientX - bx) * (e.clientX - bx) + (e.clientY - by) * (e.clientY - by) < 52 * 52) { touchBoost = 1; ptrs--; btn.add(e.pointerId); }    else touchTurn = e.clientX < innerWidth / 2 ? -1 : 1;    upd();  });  const end = (e) => {    if (btn.delete(e.pointerId)) { touchBoost = 0; upd(); return; }    ptrs = ptrs > 0 ? ptrs - 1 : 0;    if (ptrs === 0) { touchTurn = 0; touchBoost = 0; }    else touchBoost = ptrs >= 2 ? 1 : 0;    upd();  };  target.addEventListener('pointerup', end);  target.addEventListener('pointercancel', end);  target.addEventListener('contextmenu', (e) => e.preventDefault());  input.read = () => input;  return input;}