xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / compass.js 12 lines450 bytes Raw
1import { hexToWorld, hexDistance } from './hex.js';
2
3const NAMES = ['E', 'SE', 'S', 'SW', 'W', 'NW', 'N', 'NE'];
4
5export function bearing(from, to) {
6  const n = hexDistance(from.x, from.y, to.x, to.y);
7  if (!n) return 'here';
8  const a = hexToWorld(from.x, from.y);
9  const b = hexToWorld(to.x, to.y);
10  const dir = NAMES[Math.round(Math.atan2(b.z - a.z, b.x - a.x) / (Math.PI / 4) + 8) % 8];
11  return `${n} ${n === 1 ? 'hex' : 'hexes'} ${dir}`;
12}