xonecas.ioDon't click! Sean Caetano Martin Sign in
space-browser / client / src / dev.ts 127 lines4.1 kB Raw
1// An agent driving this page sees no cursor and cannot know where the camera is,
2// so it gets the transform the game itself uses, never a copy of it: world point
3// to screen pixel, screen pixel to world point, and every clickable thing already
4// converted. It also gets the pointer drawn in cells, because a screenshot cannot
5// show the browser's own cursor. Nothing here runs in a build a player sees.
6import { CELL } from './view';
7import { toWorld } from './sim';
8import { at, cam, cellOf, rect, screen, screenOf } from './stage';
9import { bodies, loose, ore, ships, state } from './world';
10
11type Target = {
12  id: number;
13  kind: string; // me, ship, piece, loose, ore, or a body kind
14  owner: number; // the hull a piece belongs to, 0 for anything loose
15  x: number;
16  y: number;
17  px: number;
18  py: number;
19  cellX: number;
20  cellY: number;
21  cellPx: number; // the centre of that cell: a click lands on a glyph, not between four
22  cellPy: number;
23  d: number; // world distance from your hull, nearest first
24};
25
26// The canvas box and the world<->screen transform, read once per use: a layout read
27// per target would make the pointer cost a forced reflow for every thing on screen.
28const boxOf = (r: DOMRect) => ({
29  left: r.left,
30  top: r.top,
31  width: r.width,
32  height: r.height,
33  cell: CELL,
34  cols: r.width / CELL,
35  rows: r.height / CELL,
36});
37const canvasBox = () => boxOf(rect());
38
39const targets = (): Target[] => {
40  const me = ships.get(state?.you ?? 0);
41  const galaxy = cam.scene === 'galaxy';
42  const r = rect();
43  const box = boxOf(r);
44  const out: Target[] = [];
45  const add = (id: number, kind: string, owner: number, x: number, y: number) => {
46    const s = screenOf(r, x, y);
47    const c = cellOf(s.px, s.py, r);
48    out.push({
49      id,
50      kind,
51      owner,
52      x,
53      y,
54      px: s.px,
55      py: s.py,
56      ...c,
57      cellPx: box.left + (c.cellX + 0.5) * CELL,
58      cellPy: box.top + (c.cellY + 0.5) * CELL,
59      d: me ? Math.hypot(x - me.x, y - me.y) : 0,
60    });
61  };
62  for (const s of ships.values()) {
63    if (galaxy && s.ai) continue; // the map draws players only
64    add(s.id, s.id === state?.you ? 'me' : s.ai ? 'ai' : 'ship', 0, s.x, s.y);
65    if (galaxy) continue; // and one dot per ship, not its hull
66    for (const p of s.pieces) {
67      const w = toWorld(s, p);
68      add(p.id, 'piece', s.id, w.x, w.y);
69    }
70  }
71  if (galaxy) {
72    // The map draws systems and nothing else: a body, an ore or a round is not a
73    // glyph at this scale, so it is not a target either.
74    for (const s of state?.systems ?? []) add(s.id, 'system', 0, s.x, s.y);
75    return out.sort((a, b) => a.d - b.d);
76  }
77  for (const l of loose.values()) add(l.id, 'loose', 0, l.x, l.y);
78  for (const o of ore.values()) add(o.id, 'ore', 0, o.x, o.y);
79  for (const b of bodies.values()) add(b.id, b.kind, 0, b.x, b.y);
80  return out.sort((a, b) => a.d - b.d);
81};
82
83let pointer: { px: number; py: number; cellX: number; cellY: number; world: { x: number; y: number } } | null = null;
84const dev = import.meta.env.DEV;
85const label = dev ? document.createElement('pre') : null;
86
87const game = {
88  get cam() {
89    return { x: cam.x, y: cam.y, zoom: cam.zoom, follow: cam.follow, scene: cam.scene };
90  },
91  get state() {
92    return state;
93  },
94  get pointer() {
95    return pointer;
96  },
97  canvas: canvasBox,
98  cellOf,
99  toScreen: screen,
100  toWorld: (px: number, py: number) => at({ clientX: px, clientY: py } as MouseEvent),
101  targets,
102};
103declare global {
104  interface Window {
105    __game: typeof game;
106  }
107}
108
109export function install(): void {
110  if (!label) return;
111  label.id = 'dev';
112  document.body.append(label);
113  window.__game = game;
114}
115
116// Drawn every move: one line of text spelling out the cell, the world point and
117// the thing under it.
118export function aim(e: MouseEvent): void {
119  const c = cellOf(e.clientX, e.clientY);
120  const w = at(e);
121  pointer = { px: e.clientX, py: e.clientY, ...c, world: w };
122  if (!label) return;
123  const hit = targets().find((t) => t.cellX === c.cellX && t.cellY === c.cellY);
124  label.textContent =
125    `cell ${c.cellX},${c.cellY}  world ${Math.round(w.x)},${Math.round(w.y)}  px ${Math.round(e.clientX)},${Math.round(e.clientY)}` +
126    (hit ? `\n${hit.kind} ${hit.id}` : '');
127}