Skip to content
1        10        20        30        40        50        60        70        801// Keyboard, an on-screen stick, and a use button. Input is sent to the server2// as a direction vector; use is a separate POST.3import { bus, emit } from './bus.js';4import { DIRS, FLAVOR } from './food.js';5import { myId, sendAction, sendInput } from './net.js';67const KEY_DIRS = {8  ArrowUp: [0, -1], KeyW: [0, -1],9  ArrowDown: [0, 1], KeyS: [0, 1],10  ArrowLeft: [-1, 0], KeyA: [-1, 0],11  ArrowRight: [1, 0], KeyD: [1, 0],12};13const USE_KEYS = new Set(['Space', 'Enter', 'KeyE']);14const DEADZONE = 0.2;1516export function startControls(root, map) {17  const stick = root.querySelector('#stick');18  const knob = root.querySelector('#knob');19  const useButton = root.querySelector('#use');20  const held = new Set();21  let stickVec = [0, 0];22  let stickPointer = null;23  let latest = null;24  let lastSent = { dx: 9, dy: 9, at: 0 };2526  bus.addEventListener('state', (e) => { latest = e.detail; });2728  const typing = () => document.activeElement?.tagName === 'INPUT';2930  const keyVec = () => {31    let dx = 0;32    let dy = 0;33    for (const code of held) {34      const d = KEY_DIRS[code];35      if (d) { dx += d[0]; dy += d[1]; }36    }37    const len = Math.hypot(dx, dy) || 1;38    return [dx / len, dy / len];39  };4041  const updateStick = (e) => {42    const rect = stick.getBoundingClientRect();43    const max = rect.width / 2;44    let dx = (e.clientX - (rect.left + max)) / max;45    let dy = (e.clientY - (rect.top + max)) / max;46    const len = Math.hypot(dx, dy);47    if (len > 1) { dx /= len; dy /= len; }48    knob.style.transform = `translate(${dx * max * 0.6}px, ${dy * max * 0.6}px)`;49    stickVec = Math.hypot(dx, dy) < DEADZONE ? [0, 0] : [dx, dy];50  };5152  stick.addEventListener('pointerdown', (e) => {53    stickPointer = e.pointerId;54    stick.setPointerCapture(e.pointerId);55    updateStick(e);56  });57  stick.addEventListener('pointermove', (e) => {58    if (e.pointerId === stickPointer) updateStick(e);59  });60  const releaseStick = (e) => {61    if (e.pointerId !== stickPointer) return;62    stickPointer = null;63    stickVec = [0, 0];64    knob.style.transform = '';65  };66  stick.addEventListener('pointerup', releaseStick);67  stick.addEventListener('pointercancel', releaseStick);6869  // Facing tile for the local player, mirroring the server's rule, so we can70  // show a short note on decor the server doesn't act on.71  const useNow = () => {72    const me = latest?.players.find((p) => p.id === myId());73    if (me && map) {74      const [fx, fy] = DIRS[me.dir];75      const tx = Math.floor(me.x / 16) + fx;76      const ty = Math.floor(me.y / 16) + fy;77      const ch = map.rows[ty]?.[tx];78      if (FLAVOR[ch]) emit('toast', FLAVOR[ch]);79    }80    sendAction();81  };8283  useButton.addEventListener('pointerdown', (e) => {84    e.preventDefault();85    useNow();86  });8788  window.addEventListener('keydown', (e) => {89    if (typing()) return;90    if (KEY_DIRS[e.code]) {91      e.preventDefault();92      held.add(e.code);93    }94    if (USE_KEYS.has(e.code) && !e.repeat) {95      e.preventDefault();96      useNow();97    }98  });99  window.addEventListener('keyup', (e) => held.delete(e.code));100  window.addEventListener('blur', () => held.clear());101102  // Send the current direction when it changes, plus a heartbeat once a second.103  setInterval(() => {104    if (!myId()) return;105    const [dx, dy] = stickVec[0] || stickVec[1] ? stickVec : keyVec();106    const now = Date.now();107    const changed = dx !== lastSent.dx || dy !== lastSent.dy;108    if (changed || now - lastSent.at > 1000) {109      lastSent = { dx, dy, at: now };110      sendInput(dx, dy);111    }112  }, 60);113}