space-browser / commits / 97d3d90
feat: cursor
@@ -5,7 +5,7 @@
55 <meta name="viewport" content="width=device-width, initial-scale=1" />
66 <title>Space Browser</title>
77 <style>
8- html, body { margin: 0; height: 100%; background: #0c0803; overflow: hidden; cursor: crosshair; }
8+ html, body { margin: 0; height: 100%; background: #0c0803; overflow: hidden; cursor: none; }
99 canvas { display: block; }
1010 #dev { position: fixed; left: 6px; top: 6px; margin: 0; font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; color: #c78a20; pointer-events: none; white-space: pre; }
1111 </style>
@@ -3,7 +3,7 @@
33 // to screen pixel, screen pixel to world point, and every clickable thing already
44 // converted. It also gets the pointer drawn in cells, because a screenshot cannot
55 // show the browser's own cursor. Nothing here runs in a build a player sees.
6-import { CELL, type View } from './view';
6+import { CELL } from './view';
77 import { toWorld } from './sim';
88 import { at, cam, cellOf, rect, screen, screenOf } from './stage';
99 import { loose, ore, ships, state } from './world';
@@ -74,7 +74,6 @@ const targets = (): Target[] => {
7474 let pointer: { px: number; py: number; cellX: number; cellY: number; world: { x: number; y: number } } | null = null;
7575 const dev = import.meta.env.DEV;
7676 const label = dev ? document.createElement('pre') : null;
77-let view: View | null = null;
7877
7978 const game = {
8079 get cam() {
@@ -98,23 +97,20 @@ declare global {
9897 }
9998 }
10099
101-// Called once the view exists: the dev build needs it to draw the pointer it owns.
102-export function install(v: View): void {
100+export function install(): void {
103101 if (!label) return;
104- view = v;
105102 label.id = 'dev';
106103 document.body.append(label);
107104 window.__game = game;
108105 }
109106
110-// Drawn every move: the reticle in cells, and one line of text spelling out the
111-// cell, the world point and the thing under it.
107+// Drawn every move: one line of text spelling out the cell, the world point and
108+// the thing under it.
112109 export function aim(e: MouseEvent): void {
113110 const c = cellOf(e.clientX, e.clientY);
114111 const w = at(e);
115112 pointer = { px: e.clientX, py: e.clientY, ...c, world: w };
116- if (!label || !view) return;
117- view.setPointer(c.cellX, c.cellY);
113+ if (!label) return;
118114 const hit = targets().find((t) => t.cellX === c.cellX && t.cellY === c.cellY);
119115 label.textContent =
120116 `cell ${c.cellX},${c.cellY} world ${Math.round(w.x)},${Math.round(w.y)} px ${Math.round(e.clientX)},${Math.round(e.clientY)}` +
@@ -10,9 +10,15 @@ let pan0: { x: number; y: number } | null = null; // right/middle drag moves the
1010 let pulling = false; // left button down: keep reporting where the piece is
1111 let pullTo: { x: number; y: number } | null = null;
1212 let pullIn = 0;
13+let 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.
17+export const pointer = () => hover;
1318
1419 export function bind(): void {
1520 addEventListener('pointerdown', (e) => {
21+ hover = e;
1622 aim(e);
1723 if (e.button !== 0) {
1824 pan0 = { x: e.clientX, y: e.clientY };
@@ -25,6 +31,7 @@ export function bind(): void {
2531 });
2632
2733 addEventListener('pointermove', (e) => {
34+ hover = e;
2835 aim(e);
2936 if (pan0) {
3037 pan(-(e.clientX - pan0.x) * wpp(), (e.clientY - pan0.y) * wpp());
@@ -47,6 +54,7 @@ export function bind(): void {
4754 const p = at(e);
4855 send({ t: 'drop', x: p.x, y: p.y });
4956 });
57+ addEventListener('pointerleave', () => (hover = null));
5058 addEventListener('contextmenu', (e) => e.preventDefault());
5159
5260 addEventListener('wheel', (e) => zoomAt(e));
@@ -1,16 +1,30 @@
11 // Space Browser, the game. The client draws what the server sends and asks for
22 // nothing else: this file is only the wiring, and the one loop that eases and draws.
33 import { install } from './dev';
4-import { bind, step } from './input';
5-import { cam, canvas } from './stage';
6-import { View } from './view';
4+import { bind, pointer, step } from './input';
5+import { toWorld } from './sim';
6+import { at, cam, canvas, cellOf } from './stage';
7+import { PIECE_R, TONE, View } from './view';
78 import { apply, follow, loose, ore, own, shots, ships, state } from './world';
89
910 const view = new View(canvas);
1011 addEventListener('resize', () => view.resize());
11-if (import.meta.env.DEV) install(view); // the dev bridge needs the view to draw the pointer
12+if (import.meta.env.DEV) install();
1213 bind();
1314
15+// What the pointer is over, in world units: a piece of a hull is round, a body is
16+// as round as it is drawn. Nothing here decides anything, it only names the cell.
17+const over = (x: number, y: number, p: { x: number; y: number }, r: number) =>
18+ (x - p.x) ** 2 + (y - p.y) ** 2 <= r * r;
19+const spot = (x: number, y: number) => {
20+ for (const s of ships.values()) {
21+ if (s.id === state?.you) continue; // your own hull is not a target
22+ if (s.pieces.some((p) => over(x, y, toWorld(s, p), PIECE_R))) return TONE.hull;
23+ }
24+ for (const b of state?.bodies ?? []) if (over(x, y, b, b.r)) return TONE.body;
25+ return TONE.space;
26+};
27+
1428 let last = performance.now();
1529 function frame(): void {
1630 const now = performance.now();
@@ -21,6 +35,13 @@ function frame(): void {
2135 apply(dt);
2236 follow(dt);
2337
38+ // The pointer is a cell mark like the order marker, so it is placed from the
39+ // camera every frame: a pan slides the world under a pointer that never moved.
40+ const p = pointer();
41+ const c = p && cellOf(p.clientX, p.clientY);
42+ const w = p && at(p);
43+ view.setPointer(c ? c.cellX : null, c?.cellY, w ? spot(w.x, w.y) : undefined);
44+
2445 // The marker is the order the server is flying: never a click of our own.
2546 const me = own();
2647 view.draw(
@@ -6,11 +6,13 @@ import { toWorld, type Body, type Kind, type Loose, type Ore, type Ship, type Sh
66 import { barrenMesh, shipyardMesh, starMesh } from './bodies';
77 import { CELL, CELL_WORLD, snapCell } from './grid';
88 import { FRAG, GLYPHS, PALETTE, VERT, lum, paletteTexture, textAtlas } from './ink';
9-import { crosshair, reticle } from './marks';
9+import { crosshair, reticle, TONE } from './marks';
1010 import { oreChunk, paint, piece, shade, type Part, type Role, type Wear } from './pieces';
1111 import { free, plate } from './shapes';
1212
1313 export { CELL, CELL_WORLD, ZOOM_MAX, ZOOM_MIN } from './grid';
14+export { TONE } from './marks';
15+export { PIECE_R } from './shapes';
1416
1517 const SHOT_LEN = 13; // world units of laser line behind a round
1618 const SHOT_W = 1.6; // world units thick: one bright cell wide
@@ -157,11 +159,13 @@ export class View {
157159 }
158160
159161 // The pointer, in cells from the top left corner: one cell is one glyph, so the
160- // mark sits on the cell the click will resolve to and nowhere between them.
161- setPointer(cx: number | null, cy = 0): void {
162+ // mark sits on the cell the click will resolve to and nowhere between them. The
163+ // tone says what that cell holds.
164+ setPointer(cx: number | null, cy = 0, tone: THREE.Color = TONE.space): void {
162165 this.cursor.visible = cx !== null;
163166 if (cx === null) return;
164167 this.cursor.position.set(cx - this.cols / 2 + 0.5, this.rows / 2 - cy - 0.5, 1);
168+ for (const p of this.cursor.children) (p as Part).material.color.copy(tone);
165169 }
166170
167171 // The fuel gauge is a row of cells two rows off the bottom of the screen, in a
@@ -17,17 +17,17 @@ export function crosshair(): THREE.Group {
1717 return g;
1818 }
1919
20-// The dev cursor: a ring of cells around the pointer cell, one cell per plate. The
21-// browser never paints its own pointer into a screenshot, so the game draws one
22-// that lands on the exact cell a click will resolve to. It is a ring and not a
23-// plus, so it can never be read as the order marker the ship is flying to.
20+// The pointer: a ring of whole cells around the pointer cell, one cell per plate.
21+// The browser's own pointer is hidden, so the game draws one that lands on the cell
22+// a click will resolve to. It is a ring and not a plus, so it can never be read as
23+// the order marker the ship is flying to.
2424 export function reticle(): THREE.Group {
2525 const g = new THREE.Group();
2626 for (const dx of [-1, 0, 1]) {
2727 for (const dy of [-1, 0, 1]) {
2828 if (!dx && !dy) continue;
29- const p = plate(0.8, 0.8, 1);
30- p.material.color.copy(lum(0.85, 6));
29+ const p = plate(1, 1, 1); // a full cell: a smaller plate is averaged away
30+ p.material.color.copy(TONE.space);
3131 p.position.set(dx, dy, 0);
3232 g.add(p);
3333 }
@@ -35,3 +35,8 @@ export function reticle(): THREE.Group {
3535 g.position.z = 1; // over the fuel gauge
3636 return g;
3737 }
38+
39+// What the ring says about the cell it is on: nothing is just space, a body is a
40+// place to orbit, and a hull that is not yours is a fight. Never the cream of your
41+// own hull, or the pointer would vanish on the one thing it sits on most.
42+export const TONE = { space: lum(1, 4), body: lum(1, 6), hull: lum(1, 14) };
@@ -4,7 +4,7 @@ import * as THREE from 'three';
44 import type { Kind } from '../sim';
55 import { lum } from './ink';
66
7-const SHIP_R = 11; // world radius of a hull
7+export const PIECE_R = 11; // world radius of a hull piece
88
99 // Polygon of n points on a circle of radius r. Every piece kind has the same
1010 // footprint so slots pack the hull evenly.
@@ -29,7 +29,7 @@ const shapes = new Map<string, THREE.BufferGeometry>();
2929 export function pieceGeometry(kind: Kind, scale: number): THREE.BufferGeometry {
3030 const key = `${kind}:${scale}`;
3131 let g = shapes.get(key);
32- if (!g) shapes.set(key, (g = buildPiece(kind, SHIP_R * scale)));
32+ if (!g) shapes.set(key, (g = buildPiece(kind, PIECE_R * scale)));
3333 return g;
3434 }