xonecas.ioDon't click! Sean Caetano Martin Sign in
space-browser / client / src / input.ts 101 lines3.4 kB Raw
1// Intents: every pointer and key here becomes a message for the server, and the
2// server decides what happened. The only local decisions are the camera's.
3import { aim } from './dev';
4import { pull } from './pull';
5import { at, cam, pan, switchScene, wpc, wpp, zoomAt } from './stage';
6import { send } from './world';
7
8const keys = new Set<string>();
9let pan0: { x: number; y: number } | null = null; // right/middle drag moves the camera
10let pulling = false; // left button down: keep reporting where the piece is
11let pullTo: { x: number; y: number } | null = null;
12let pullIn = 0;
13let hover: { clientX: number; clientY: number } | null = null; // the pointer on screen
14
15// The browser's own pointer is hidden; the game draws this one, and it has to be
16// re-read every frame because panning moves the cell under a still pointer.
17export const pointer = () => hover;
18
19// Whatever is in hand goes back to the server where it was let go, so a piece can
20// never ride a scene change out to a galaxy-scale coordinate.
21const release = () => {
22  if (!pulling) return;
23  pulling = false;
24  const p = pull.to;
25  pullTo = null;
26  pull.to = null;
27  if (p) send({ t: 'drop', x: p.x, y: p.y });
28};
29
30export function bind(): void {
31  addEventListener('pointerdown', (e) => {
32    hover = e;
33    aim(e);
34    if (e.button !== 0) {
35      pan0 = { x: e.clientX, y: e.clientY };
36      return;
37    }
38    if (cam.scene === 'galaxy') return; // the map has nothing to pick up yet
39    const p = at(e);
40    pulling = true;
41    pull.to = p; // the piece in hand sits under the pointer, not a tick behind it
42    send({ t: 'pick', x: p.x, y: p.y });
43  });
44
45  addEventListener('pointermove', (e) => {
46    hover = e;
47    aim(e);
48    if (pan0) {
49      pan(-(e.clientX - pan0.x) * wpp(), (e.clientY - pan0.y) * wpp());
50      pan0 = { x: e.clientX, y: e.clientY };
51      return;
52    }
53    if (!pulling) return;
54    pull.to = pullTo = at(e);
55  });
56
57  addEventListener('pointerup', (e) => {
58    if (e.button !== 0) {
59      pan0 = null;
60      return;
61    }
62    if (!pulling) return;
63    pulling = false;
64    pullTo = null;
65    pull.to = null;
66    const p = at(e);
67    send({ t: 'drop', x: p.x, y: p.y });
68  });
69  addEventListener('pointerleave', () => (hover = null));
70  addEventListener('contextmenu', (e) => e.preventDefault());
71
72  addEventListener('wheel', (e) => zoomAt(e));
73
74  addEventListener('keydown', (e) => {
75    keys.add(e.code);
76    if (e.code === 'Space') cam.follow = true;
77    // The map is a map: it opens on the galaxy as a whole, and whatever you panned
78    // and zoomed it to the last time is exactly where it still is.
79    if (e.code === 'KeyM') {
80      release();
81      switchScene(cam.scene === 'local' ? 'galaxy' : 'local');
82    }
83  });
84  addEventListener('keyup', (e) => keys.delete(e.code));
85}
86
87// Per frame: pan on screen-space speed so it feels the same at every zoom, and
88// report a drag no faster than the server needs it.
89export function step(dt: number): void {
90  const px = (keys.has('KeyD') || keys.has('ArrowRight') ? 1 : 0) - (keys.has('KeyA') || keys.has('ArrowLeft') ? 1 : 0);
91  const py = (keys.has('KeyW') || keys.has('ArrowUp') ? 1 : 0) - (keys.has('KeyS') || keys.has('ArrowDown') ? 1 : 0);
92  if (px || py) {
93    const s = (30 * wpc() * dt) / Math.hypot(px, py); // 30 cells / second
94    pan(px * s, py * s);
95  }
96  if (pullTo && (pullIn += dt) > 1 / 30) {
97    pullIn = 0;
98    send({ t: 'drag', x: pullTo.x, y: pullTo.y });
99    pullTo = null;
100  }
101}