space-browser / commits / f137b9b
feat: star, baren planet, shipyard planet
@@ -0,0 +1 @@
1+/.dev/
@@ -3,5 +3,6 @@
33 - See ./GAME_DESIGN.md for the high level design choices.
44 - `../space-browser-exploration` and `../browser-game-exploration` are the prototypes referenced in the design doc.
55 - Do not confuse the prototypes scope and features with this final implementation design. They were explorations with different objectives.
6+- Start and stop the dev servers only with `./dev-start.sh` and `./dev-stop.sh`. Never run `go run`, `vite` or `npm run dev` directly.
67 - Use `agent-browser` to experience the game from a players point of view.
78 - Do not add new designs, systems or mechanics without the user asking explicitly.
@@ -12,12 +12,18 @@ import {
1212 type Ship,
1313 } from './sim';
1414 import { View, CELL, CELL_WORLD, ZOOM_MIN, ZOOM_MAX, type Camera } from './view';
15+import { makeSystem, stepSystem } from './world';
1516
1617 const canvas = document.getElementById('view') as HTMLCanvasElement;
1718 const view = new View(canvas);
1819 addEventListener('resize', () => view.resize());
1920
20-const own = makeShip(0, '', 0, 0);
21+const sys = makeSystem();
22+view.system(sys);
23+
24+// Clear of the star, out where the whole system is in reach of the camera.
25+const SPAWN = { x: 0, y: -70 };
26+const own = makeShip(0, '', SPAWN.x, SPAWN.y);
2127 const others = new Map<number, Ship>();
2228 // A few pieces lying around the spawn, to build onto the starting triangle.
2329 const loose: Loose[] = [
@@ -28,7 +34,7 @@ const loose: Loose[] = [
2834 ];
2935 let held: Loose | null = null;
3036 let marker: { x: number; y: number } | null = null;
31-const cam: Camera & { follow: boolean } = { x: 0, y: 0, zoom: 1, follow: true };
37+const cam: Camera & { follow: boolean } = { x: own.x, y: own.y, zoom: 0.5, follow: true };
3238 const keys = new Set<string>();
3339 let drag: { x: number; y: number } | null = null;
3440 let sendIn = 0;
@@ -53,7 +59,7 @@ const net = connect(
5359 if (own.id) return; // spawn once
5460 own.id = w.id;
5561 own.name = w.name;
56- own.x = own.tx = (w.id - 1) * 40;
62+ own.x = own.tx = SPAWN.x;
5763 for (const l of loose) {
5864 l.x += own.x; // the wreckage lies where we wake up
5965 l.y += own.y;
@@ -155,6 +161,7 @@ function frame(): void {
155161 const dt = Math.min(0.05, Math.max(0, now - last) / 1000);
156162 last = now;
157163
164+ stepSystem(sys, dt);
158165 step(own, dt);
159166 const k = 1 - Math.exp(-dt * 12);
160167 for (const s of others.values()) {
@@ -3,21 +3,27 @@
33 // Two channels carry the shading: R = luminance (which glyph), G = palette index.
44 import * as THREE from 'three';
55 import { toWorld, type Kind, type Loose, type Ship } from './sim';
6+import type { Planet, System } from './world';
67
78 export const CELL = 12; // css px per character cell
89 export const CELL_WORLD = 2; // world units per character cell at zoom 1
9-export const ZOOM_MIN = 0.6; // furthest out: past this a hull stops reading as a hull
10+export const ZOOM_MIN = 0.3; // furthest out: far enough to frame a whole system
1011 export const ZOOM_MAX = 2; // closest in
1112 const SHIP_R = 11; // world radius of a hull
1213
1314 export type Camera = { x: number; y: number; zoom: number };
1415
1516 const GLYPHS = ' .:-=+*#%@';
16-const PALETTE = [0x0c0803, 0x241806, 0x3d2709, 0x57370c, 0x754c11, 0x98631a, 0xc78a20, 0xffe9a8];
17+// 0-7 is the amber ramp used for everything that flies. 8-12 are reserved for
18+// celestial bodies, so a rust planet and a steel planet never read as each other.
19+const PALETTE = [
20+ 0x0c0803, 0x241806, 0x3d2709, 0x57370c, 0x754c11, 0x98631a, 0xc78a20, 0xffe9a8,
21+ 0x4a1d0f, 0x9c4f28, 0x1f4453, 0x7fc0d4, 0xfff6dd,
22+];
1723 const ATLAS_CELL = 32;
1824
1925 // R = luminance, G = palette index.
20-const lum = (v: number, idx = 7) => new THREE.Color(v, idx / 7, 0);
26+const lum = (v: number, idx = 7) => new THREE.Color(v, idx / (PALETTE.length - 1), 0);
2127
2228 function glyphAtlas(): THREE.Texture {
2329 const c = document.createElement('canvas');
@@ -60,7 +66,7 @@ void main() {
6066 vec2 uv = gl_FragCoord.xy / uCellPx;
6167 vec3 s = texture2D(uScene, (floor(uv) + 0.5) / uCells).rgb;
6268 float band = floor(s.r * ${GLYPHS.length - 1}.0 + 0.5);
63- vec3 col = texture2D(uPalette, vec2((s.g * 7.0 + 0.5) / ${PALETTE.length}.0, 0.5)).rgb;
69+ vec3 col = texture2D(uPalette, vec2((s.g * ${PALETTE.length - 1}.0 + 0.5) / ${PALETTE.length}.0, 0.5)).rgb;
6470 vec2 local = clamp(fract(uv), 0.02, 0.98);
6571 float ink = texture2D(uAtlas, vec2((band + local.x) / ${GLYPHS.length}.0, local.y)).a;
6672 gl_FragColor = vec4(mix(uBg, col, ink), 1.0);
@@ -130,6 +136,94 @@ function crosshair(): THREE.Group {
130136 return g;
131137 }
132138
139+const disk = (r: number, v: number, idx: number, x = 0, y = 0): THREE.Mesh => {
140+ const m = new THREE.Mesh(
141+ new THREE.CircleGeometry(r, 48),
142+ new THREE.MeshBasicMaterial({ color: lum(v, idx) }),
143+ );
144+ m.position.set(x, y, 0);
145+ return m;
146+};
147+
148+// A lit limb: the bright edge that makes a disk pop off the black.
149+const limb = (r: number, v: number, idx: number): THREE.Mesh =>
150+ new THREE.Mesh(
151+ new THREE.RingGeometry(r * 0.88, r, 64),
152+ new THREE.MeshBasicMaterial({ color: lum(v, idx), side: THREE.DoubleSide }),
153+ );
154+
155+// A star reads as a hot core with long rays, so it can never be mistaken for a
156+// planet at any zoom: it is the only thing in the scene that shines.
157+function starMesh(r: number): THREE.Group {
158+ const g = new THREE.Group();
159+ g.add(disk(r, 0.8, 6));
160+ g.add(disk(r * 0.62, 1, 7));
161+ g.add(disk(r * 0.3, 1, 12));
162+ const rays = new THREE.Group();
163+ for (let i = 0; i < 8; i++) {
164+ const a = (i / 8) * Math.PI * 2;
165+ const len = (i % 2 ? 0.5 : 0.9) * r;
166+ const m = plate(r * 0.24, len, 0.7);
167+ (m.material as THREE.MeshBasicMaterial).color.copy(lum(0.6, 6));
168+ m.position.set(Math.cos(a) * (r * 0.8 + len / 2), Math.sin(a) * (r * 0.8 + len / 2), 0);
169+ m.rotation.z = a - Math.PI / 2;
170+ rays.add(m);
171+ }
172+ g.add(rays);
173+ return g;
174+}
175+
176+// A barren planet: a rust disk lit from one side, with craters punched out of it.
177+// Nothing on it is usable.
178+function barrenMesh(r: number): THREE.Group {
179+ const g = new THREE.Group();
180+ g.add(disk(r, 0.7, 9));
181+ g.add(limb(r, 1, 9));
182+ for (const [dx, dy, cr] of [[0.3, 0.28, 0.34], [-0.12, 0.5, 0.2], [0.36, -0.34, 0.16]]) {
183+ g.add(disk(r * cr, 0.3, 8, dx * r, dy * r)); // craters
184+ }
185+ return g;
186+}
187+
188+// A shipyard planet: cool steel, a lit cap, and a cream ring studded with gold
189+// docks. It is the only body in the system with anything bolted around it.
190+function shipyardMesh(r: number): THREE.Group {
191+ const g = new THREE.Group();
192+ g.add(disk(r, 0.55, 10));
193+ g.add(disk(r * 0.82, 0.9, 11, -r * 0.07, r * 0.09));
194+ // The berth ring floats clear of the hull: a station, not a moon.
195+ const ring = new THREE.Mesh(
196+ new THREE.RingGeometry(r * 1.18, r * 1.38, 64),
197+ new THREE.MeshBasicMaterial({ color: lum(1, 12), side: THREE.DoubleSide }),
198+ );
199+ g.add(ring);
200+ for (let i = 0; i < 4; i++) {
201+ const a = (i / 4) * Math.PI * 2 + Math.PI / 4;
202+ const d = plate(r * 0.34, r * 0.34, 1);
203+ (d.material as THREE.MeshBasicMaterial).color.copy(lum(1, 7));
204+ d.position.set(Math.cos(a) * r * 1.5, Math.sin(a) * r * 1.5, 0.06);
205+ d.rotation.z = a;
206+ g.add(d);
207+ }
208+ return g;
209+}
210+
211+// A faint dot ring: the planet's path, dim enough to read as background.
212+function orbitPath(radius: number): THREE.Group {
213+ const g = new THREE.Group();
214+ const n = Math.round(radius * 1.1);
215+ const geo = new THREE.PlaneGeometry(CELL_WORLD, CELL_WORLD);
216+ const mat = new THREE.MeshBasicMaterial({ color: lum(0.5, 3) });
217+ for (let i = 0; i < n; i++) {
218+ const a = (i / n) * Math.PI * 2;
219+ const m = new THREE.Mesh(geo, mat);
220+ m.position.set(Math.cos(a) * radius, Math.sin(a) * radius, 0);
221+ g.add(m);
222+ }
223+ g.position.z = -2;
224+ return g;
225+}
226+
133227 export class View {
134228 private renderer: THREE.WebGLRenderer;
135229 private scene = new THREE.Scene();
@@ -139,6 +233,8 @@ export class View {
139233 private quad = new THREE.Scene();
140234 private pieceMeshes = new Map<number, THREE.Group>();
141235 private marker = crosshair();
236+ private star: THREE.Group | null = null;
237+ private planets: { planet: Planet; mesh: THREE.Group }[] = [];
142238 private cols = 0;
143239 private rows = 0;
144240
@@ -172,6 +268,20 @@ export class View {
172268 this.resize();
173269 }
174270
271+ // Built once: bodies keep an identity for the whole run, only their orbit moves.
272+ system(sys: System): void {
273+ this.star = starMesh(sys.star);
274+ this.star.position.z = -1;
275+ this.scene.add(this.star);
276+ for (const planet of sys.planets) {
277+ const mesh = planet.kind === 'shipyard' ? shipyardMesh(planet.r) : barrenMesh(planet.r);
278+ mesh.position.z = -1;
279+ this.scene.add(mesh);
280+ this.scene.add(orbitPath(planet.orbit));
281+ this.planets.push({ planet, mesh });
282+ }
283+ }
284+
175285 resize(): void {
176286 // Whole cells only: the canvas, the frustum and the glyph grid must describe
177287 // the same rectangle, or world positions land half a cell off their glyph.
@@ -220,6 +330,14 @@ export class View {
220330 this.camera.top = hh;
221331 this.camera.bottom = -hh;
222332 this.camera.updateProjectionMatrix();
333+ // Bodies sit behind the ships, drifting and turning slowly enough to read as alive.
334+ const t = performance.now() / 1000;
335+ if (this.star) this.star.rotation.z = t * 0.08;
336+ for (const { planet, mesh } of this.planets) {
337+ mesh.position.set(planet.x, planet.y, -1);
338+ mesh.rotation.z = t * 0.15;
339+ }
340+
223341 const seen = new Set<number>();
224342 for (const s of ships) {
225343 for (const p of s.pieces) {
@@ -0,0 +1,41 @@
1+// The one star system we render while tuning POIs: a star with two planets that
2+// actually revolve around it. Client-side only until galaxy generation lands.
3+export type PlanetKind = 'shipyard' | 'barren';
4+
5+export type Planet = {
6+ kind: PlanetKind;
7+ r: number; // world radius
8+ orbit: number; // distance from the star
9+ angle: number; // current orbital angle
10+ speed: number; // radians / second
11+ x: number;
12+ y: number;
13+};
14+
15+export type System = { star: number; planets: Planet[] };
16+
17+const STAR_R = 30;
18+
19+export function makeSystem(): System {
20+ const sys: System = {
21+ star: STAR_R,
22+ planets: [
23+ { kind: 'shipyard', r: 20, orbit: 120, angle: -1.57, speed: 0.06, x: 0, y: 0 },
24+ { kind: 'barren', r: 28, orbit: 215, angle: 0.6, speed: 0.035, x: 0, y: 0 },
25+ ],
26+ };
27+ place(sys);
28+ return sys;
29+}
30+
31+function place(sys: System): void {
32+ for (const p of sys.planets) {
33+ p.x = Math.cos(p.angle) * p.orbit;
34+ p.y = Math.sin(p.angle) * p.orbit;
35+ }
36+}
37+
38+export function stepSystem(sys: System, dt: number): void {
39+ for (const p of sys.planets) p.angle += p.speed * dt;
40+ place(sys);
41+}
@@ -0,0 +1,59 @@
1+#!/usr/bin/env bash
2+# Dev orchestrator.
3+# - backend runs in the background and restarts when its sources change
4+# - frontend runs vite, which already hot-reloads its own sources
5+set -euo pipefail
6+cd "$(dirname "$0")"
7+
8+PIDDIR=.dev
9+SERVER_LOG=$PIDDIR/server.log
10+FRONTEND_LOG=$PIDDIR/frontend.log
11+
12+mkdir -p "$PIDDIR"
13+
14+# clear anything a previous run left behind
15+./dev-stop.sh >/dev/null 2>&1 || true
16+
17+: > "$SERVER_LOG"
18+: > "$FRONTEND_LOG"
19+
20+start_server() {
21+ setsid bash -c "echo \$\$ > $PIDDIR/server.pid; cd server && exec go run ." >>"$SERVER_LOG" 2>&1 &
22+}
23+
24+stop_server() {
25+ [ -f "$PIDDIR/server.pid" ] || return 0
26+ kill -TERM -- "-$(cat "$PIDDIR/server.pid")" 2>/dev/null || true
27+ rm -f "$PIDDIR/server.pid"
28+}
29+
30+start_frontend() {
31+ setsid bash -c "echo \$\$ > $PIDDIR/frontend.pid; cd client && exec npm run dev" >>"$FRONTEND_LOG" 2>&1 &
32+}
33+
34+server_signature() {
35+ find server -type f \( -name '*.go' -o -name 'go.mod' -o -name 'go.sum' \) \
36+ -printf '%T@ %s %p\n' 2>/dev/null | sort
37+}
38+
39+watch_server() {
40+ echo "$BASHPID" > "$PIDDIR/watch.pid"
41+ local last now
42+ last=$(server_signature)
43+ while sleep 1; do
44+ now=$(server_signature)
45+ if [ "$now" != "$last" ]; then
46+ last=$now
47+ echo "dev-start: server sources changed, restarting" >>"$SERVER_LOG"
48+ stop_server
49+ sleep 1
50+ start_server
51+ fi
52+ done
53+}
54+
55+start_server
56+start_frontend
57+watch_server >/dev/null 2>&1 &
58+
59+echo "dev-start: server -> $SERVER_LOG, frontend -> $FRONTEND_LOG"
@@ -0,0 +1,20 @@
1+#!/usr/bin/env bash
2+# Stop the dev orchestrator started by ./dev-start.sh.
3+set -uo pipefail
4+cd "$(dirname "$0")"
5+
6+PIDDIR=.dev
7+
8+stop() {
9+ local name="$1" f="$PIDDIR/$1.pid" pid
10+ [ -f "$f" ] || return 0
11+ pid=$(cat "$f" 2>/dev/null || true)
12+ if [ -n "${pid:-}" ]; then
13+ kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null || true
14+ fi
15+ rm -f "$f"
16+}
17+
18+stop watch
19+stop server
20+stop frontend