xonecas.ioDon't click! Sean Caetano Martin Sign in
space-browser / client / src / view / galaxy.ts 82 lines3.0 kB Raw
1// Scene 2: the galaxy map. One mark per star system and one per living player,
2// drawn in cells rather than in world units, so a dot is a glyph at every map
3// zoom instead of a sub-cell smudge the raster averages away. The systems are the
4// rings the generator lays out; nothing here adds a shape the galaxy does not have.
5import * as THREE from 'three';
6import type { Ship, System } from '../sim';
7import { snapCell } from './grid';
8import type { Camera } from './index';
9import { lum } from './ink';
10import type { Part } from './pieces';
11import { free, plate } from './shapes';
12
13// A system is the one bright thing in its space: a gold core with four dim rays,
14// which is also the only mark that is more than a single cell, so a system is
15// never read as a player. Every ship is one white cell, and yours is the only one
16// at full brightness: on a map of the population you have to find yourself first.
17const CORE = lum(0.95, 6);
18const RAY = lum(0.45, 6);
19const OWN = lum(1, 12);
20const OTHER = lum(0.7, 12);
21
22const systemMark = (): THREE.Group => {
23  const g = new THREE.Group();
24  for (const [dx, dy] of [[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1]]) {
25    const p = plate(1, 1, 1);
26    p.material.color.copy(dx || dy ? RAY : CORE);
27    p.position.set(dx, dy, 0);
28    g.add(p);
29  }
30  return g;
31};
32
33export class Galaxy {
34  readonly scene = new THREE.Scene();
35  private stars = new Map<number, THREE.Group>();
36  private ships = new Map<number, Part>();
37
38  // Marks are placed from the camera, like the order marker: a pan slides the map
39  // under glyphs that never move, and a dot never straddles four of them.
40  show(cam: Camera, systems: System[], ships: Ship[], ownId: number, wpc: number, cols: number, rows: number): void {
41    const stars = new Set<number>();
42    for (const s of systems) {
43      stars.add(s.id);
44      let g = this.stars.get(s.id);
45      if (!g) {
46        g = systemMark();
47        this.stars.set(s.id, g);
48        this.scene.add(g);
49      }
50      g.scale.setScalar(wpc); // the mark is built in cells, the map is in world units
51      g.position.set(snapCell(s.x, cam.x, wpc, cols), snapCell(s.y, cam.y, wpc, rows, -1), 0);
52    }
53    for (const [id, g] of this.stars) {
54      if (stars.has(id)) continue;
55      free(g);
56      this.scene.remove(g);
57      this.stars.delete(id);
58    }
59    // Only real players: an AI hull never leaves its system, so it says nothing
60    // about where the population is.
61    const live = new Set<number>();
62    for (const s of ships) {
63      if (s.ai) continue;
64      live.add(s.id);
65      let m = this.ships.get(s.id);
66      if (!m) {
67        m = plate(1, 1, 1);
68        this.ships.set(s.id, m);
69        this.scene.add(m);
70      }
71      m.material.color.copy(s.id === ownId ? OWN : OTHER);
72      m.scale.setScalar(wpc);
73      m.position.set(snapCell(s.x, cam.x, wpc, cols), snapCell(s.y, cam.y, wpc, rows, -1), 0);
74    }
75    for (const [id, m] of this.ships) {
76      if (live.has(id)) continue;
77      free(m);
78      this.scene.remove(m);
79      this.ships.delete(id);
80    }
81  }
82}