xonecas.ioDon't click! Sean Caetano Martin Sign in

space-browser / commits / e7675c2

fix: map split cells rendered weird

xonecas5 weeks agoe7675c23a1b6d649db0ad57e3393ad8665c29976parent b21f8a55 files changed

client/src/view/galaxy.ts+3-3
@@ -35,7 +35,7 @@ export class Galaxy {
3535
3636 // Marks are placed from the camera, like the order marker: a pan slides the map
3737 // under glyphs that never move, and a dot never straddles four of them.
38- show(cam: Camera, systems: System[], ships: Ship[], wpc: number): void {
38+ show(cam: Camera, systems: System[], ships: Ship[], wpc: number, cols: number, rows: number): void {
3939 const stars = new Set<number>();
4040 for (const s of systems) {
4141 stars.add(s.id);
@@ -46,7 +46,7 @@ export class Galaxy {
4646 this.scene.add(g);
4747 }
4848 g.scale.setScalar(wpc); // the mark is built in cells, the map is in world units
49- g.position.set(snapCell(s.x, cam.x, wpc), snapCell(s.y, cam.y, wpc), 0);
49+ g.position.set(snapCell(s.x, cam.x, wpc, cols), snapCell(s.y, cam.y, wpc, rows, -1), 0);
5050 }
5151 for (const [id, g] of this.stars) {
5252 if (stars.has(id)) continue;
@@ -68,7 +68,7 @@ export class Galaxy {
6868 this.scene.add(m);
6969 }
7070 m.scale.setScalar(wpc);
71- m.position.set(snapCell(s.x, cam.x, wpc), snapCell(s.y, cam.y, wpc), 0);
71+ m.position.set(snapCell(s.x, cam.x, wpc, cols), snapCell(s.y, cam.y, wpc, rows, -1), 0);
7272 }
7373 for (const [id, m] of this.ships) {
7474 if (live.has(id)) continue;
client/src/view/grid.ts+6-3
@@ -13,6 +13,9 @@ export const GALAXY_MIN = 0.012;
1313 export const GALAXY_MAX = 0.05;
1414
1515 // Snap a world point to the centre of the cell that holds it: a one-cell mark has
16-// to land on a glyph, not straddle four of them.
17-export const snapCell = (v: number, c: number, wpc: number) =>
18- c + (Math.floor((v - c) / wpc) + 0.5) * wpc;
16+// to land on a glyph, not straddle four of them. The cell grid hangs off the
17+// camera, so it is half a cell out from it whenever the canvas holds an odd number
18+// of cells: `n` is that count, and passing it keeps the snap on the raster. Rows
19+// count the other way from a world y, so the vertical snap passes sign = -1.
20+export const snapCell = (v: number, c: number, wpc: number, n: number, sign = 1) =>
21+ c + sign * (Math.floor(((v - c) * sign) / wpc + n / 2) + 0.5 - n / 2) * wpc;
client/src/view/index.ts+24-2
@@ -93,15 +93,26 @@ export class View {
9393 }
9494
9595 // Built once: a body keeps an identity for the whole run, only its place moves.
96+ // The mesh is rebuilt if that identity ever changes: a galaxy is generated per
97+ // server boot, and a client that stays connected through a restart must not keep
98+ // drawing the old planet under the new one.
9699 private bodies(bs: Body[], t: number): void {
97100 const seen = new Set<number>();
98101 for (const b of bs) {
99102 seen.add(b.id);
100103 let m = this.bodyMeshes.get(b.id);
104+ if (m && (m.userData.kind !== b.kind || m.userData.r !== b.r)) {
105+ free(m);
106+ this.scene.remove(m);
107+ this.bodyMeshes.delete(b.id);
108+ m = undefined;
109+ }
101110 if (!m) {
102111 m = b.kind === 'star' ? starMesh(b.r) : b.kind === 'shipyard' ? shipyardMesh(b.r) : barrenMesh(b.r);
103112 m.position.z = -1;
104113 m.userData.spin = b.kind === 'star' ? 0.08 : 0.15; // slow enough to read as alive
114+ m.userData.kind = b.kind;
115+ m.userData.r = b.r;
105116 this.scene.add(m);
106117 this.bodyMeshes.set(b.id, m);
107118 }
@@ -275,9 +286,13 @@ export class View {
275286 if (!m) {
276287 m = plate(SHOT_LEN, SHOT_W, 1);
277288 m.material.color.copy(lum(1, 7));
289+ m.renderOrder = 1;
278290 this.shotMeshes.set(s.id, m);
279291 this.scene.add(m);
280292 }
293+ // One cell wide at every zoom, like every other thing that is a mark rather
294+ // than a body: at the far zoom a world-sized line would be a dim smudge.
295+ m.scale.set(1, wpc / SHOT_W, 1);
281296 m.position.set(s.x - Math.cos(s.h) * SHOT_LEN * 0.5, s.y - Math.sin(s.h) * SHOT_LEN * 0.5, 0);
282297 m.rotation.z = s.h;
283298 }
@@ -289,7 +304,14 @@ export class View {
289304 }
290305 this.marker.visible = marker !== null;
291306 if (marker) {
292- this.marker.position.set(snapCell(marker.x, cam.x, wpc), snapCell(marker.y, cam.y, wpc), 0.5);
307+ // The marker is one cell per plate, so it is scaled into cells and snapped to
308+ // the cell it is on, exactly like the pointer ring.
309+ this.marker.scale.setScalar(wpc);
310+ this.marker.position.set(
311+ snapCell(marker.x, cam.x, wpc, this.cols),
312+ snapCell(marker.y, cam.y, wpc, this.rows, -1),
313+ 0.5,
314+ );
293315 }
294316 this.fuel(ships.find((s) => s.id === ownId), performance.now() / 1000);
295317
@@ -300,7 +322,7 @@ export class View {
300322 // the cell a hull, an ore or a round is not a glyph, it is noise.
301323 drawGalaxy(cam: Camera, systems: System[], ships: Ship[], ownId: number): void {
302324 const wpc = this.frame(cam);
303- this.galaxy.show(cam, systems, ships, wpc);
325+ this.galaxy.show(cam, systems, ships, wpc, this.cols, this.rows);
304326 // No map hint on the map itself: you are already there.
305327 this.fuel(ships.find((s) => s.id === ownId), performance.now() / 1000, false);
306328 this.present(this.galaxy.scene);
client/src/view/marks.ts+7-4
@@ -1,19 +1,21 @@
11 // The two one-cell marks that are not things in the world: the order the ship is
22 // flying to, and the pointer the dev build draws in place of the browser's cursor.
33 import * as THREE from 'three';
4-import { CELL_WORLD } from './grid';
54 import { lum } from './ink';
65 import { plate } from './shapes';
76
8-// A click target: a plus of 5 cells, bright enough to read past the dust.
7+// A click target: a plus of 5 cells, bright enough to read past the dust. It is
8+// built in cells and scaled by the view, so a plate is one glyph at every zoom
9+// instead of one glyph at the one zoom its world size happens to match.
910 export function crosshair(): THREE.Group {
1011 const g = new THREE.Group();
1112 for (const [dx, dy] of [[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1]]) {
12- const p = plate(CELL_WORLD * 1.25, CELL_WORLD * 1.25, dx || dy ? 0.75 : 1);
13- p.position.set(dx * CELL_WORLD, dy * CELL_WORLD, 0);
13+ const p = plate(1, 1, dx || dy ? 0.75 : 1);
14+ p.position.set(dx, dy, 0);
1415 g.add(p);
1516 }
1617 g.position.z = 0.5;
18+ g.renderOrder = 2; // over every body and piece: it is an order, not a thing
1719 return g;
1820 }
1921
@@ -33,6 +35,7 @@ export function reticle(): THREE.Group {
3335 }
3436 }
3537 g.position.z = 1; // over the fuel gauge
38+ g.renderOrder = 1; // and after it too: a cell of gauge must never hide the pointer
3639 return g;
3740 }
3841
client/src/view/pieces.ts+2-0
@@ -58,6 +58,7 @@ export function paint(g: THREE.Group, role: Role, w: Wear, hostile: boolean): vo
5858 // when a cell is several world units wide.
5959 export function piece(kind: Kind): THREE.Group {
6060 const g = new THREE.Group();
61+ g.renderOrder = 1; // over the bodies, which are the only thing behind a ship
6162 for (const [scale, v] of [[1.9, 0], [1.3, 0], [1, 1], [0.7, 0.5]]) {
6263 g.add(new THREE.Mesh(pieceGeometry(kind, scale), new THREE.MeshBasicMaterial({ color: lum(v) })));
6364 }
@@ -70,6 +71,7 @@ const ORE_SHELL = new THREE.ShapeGeometry(poly(6, ORE_R));
7071 const ORE_CORE = new THREE.ShapeGeometry(poly(6, ORE_R * 0.55, 0, 0.4));
7172 export function oreChunk(): THREE.Group {
7273 const g = new THREE.Group();
74+ g.renderOrder = 1; // ore never hides behind a planet it is orbiting
7375 g.add(new THREE.Mesh(ORE_SHELL, new THREE.MeshBasicMaterial({ color: SHADE.ore[0] })));
7476 g.add(new THREE.Mesh(ORE_CORE, new THREE.MeshBasicMaterial({ color: SHADE.ore[1] })));
7577 shade(g, 'ore');