@johnmorrisdotca/wakusei-planets 0.28.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/NOTICE.md +20 -0
  3. package/README.md +61 -16
  4. package/dist/embed.d.ts +64 -0
  5. package/dist/embed.js +66 -0
  6. package/dist/index.d.ts +4 -1
  7. package/dist/index.js +4 -1
  8. package/dist/lineup/data.d.ts +160 -0
  9. package/dist/lineup/data.js +303 -0
  10. package/dist/lineup/element.d.ts +99 -0
  11. package/dist/lineup/element.js +782 -0
  12. package/dist/lineup/labels.d.ts +141 -0
  13. package/dist/lineup/labels.js +155 -0
  14. package/dist/lineup/layout.d.ts +174 -0
  15. package/dist/lineup/layout.js +162 -0
  16. package/dist/lineup/scene3d.d.ts +47 -0
  17. package/dist/lineup/scene3d.js +155 -0
  18. package/dist/portrait/element.d.ts +5 -2
  19. package/dist/portrait/element.js +57 -6
  20. package/dist/portrait/scene3d.d.ts +13 -0
  21. package/dist/portrait/scene3d.js +21 -2
  22. package/dist/scene/body.d.ts +3 -1
  23. package/dist/scene/body.js +2 -1
  24. package/dist/scene/detail.d.ts +8 -2
  25. package/dist/scene/detail.js +56 -28
  26. package/dist/scene/painter.d.ts +32 -0
  27. package/dist/scene/painter.js +94 -10
  28. package/dist/scene/textures.d.ts +4 -2
  29. package/dist/scene/textures.js +2 -0
  30. package/dist/scene/tiles.d.ts +46 -0
  31. package/dist/scene/tiles.js +165 -19
  32. package/dist/space/arrows.d.ts +87 -0
  33. package/dist/space/arrows.js +117 -0
  34. package/dist/space/beside.d.ts +74 -0
  35. package/dist/space/beside.js +76 -0
  36. package/dist/space/bodies.d.ts +83 -0
  37. package/dist/space/bodies.js +93 -0
  38. package/dist/space/clock.d.ts +31 -0
  39. package/dist/space/clock.js +45 -0
  40. package/dist/space/element.d.ts +98 -15
  41. package/dist/space/element.js +320 -63
  42. package/dist/space/frame.d.ts +45 -2
  43. package/dist/space/frame.js +56 -4
  44. package/dist/space/index.d.ts +6 -1
  45. package/dist/space/index.js +6 -1
  46. package/dist/space/labels.d.ts +52 -0
  47. package/dist/space/labels.js +40 -0
  48. package/dist/space/scale.d.ts +5 -5
  49. package/dist/space/scale.js +10 -4
  50. package/dist/space/scene.d.ts +50 -9
  51. package/dist/space/scene.js +313 -36
  52. package/dist/space/spin.d.ts +37 -0
  53. package/dist/space/spin.js +29 -0
  54. package/dist/version.d.ts +1 -1
  55. package/dist/version.js +1 -1
  56. package/package.json +8 -1
@@ -0,0 +1,117 @@
1
+ // The little arrows on an orbit line that show which way the body goes, and the shapes the help mode draws round the body looked at. Pure: lines in, places and directions out; the scene turns them into lines on the screen.
2
+ import { add, cross, dot, RAD, scale, unit } from "../vec.js";
3
+ /**
4
+ * How many arrowheads an orbit line carries.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { ARROWS_PER_ORBIT } from "@johnmorrisdotca/wakusei-planets/space";
9
+ *
10
+ * console.log(ARROWS_PER_ORBIT);
11
+ * ```
12
+ */
13
+ export const ARROWS_PER_ORBIT = 6;
14
+ /**
15
+ * Where the arrowheads go on an orbit line, and which way each points. The line is the points of `planetOrbit` as the view places them: they run from where the body is now once round in the order the body
16
+ * goes, so the direction of each arrow is the line's own direction there, from the point before to the point after. The arrows are spread evenly round the line and stay clear of the body, which is at its first point.
17
+ * They show the real way the body travels, which is the same whichever way the clock runs.
18
+ *
19
+ * @param line - The orbit line, `count + 1` points whose last is near the first.
20
+ * @param arrows - How many arrowheads (`ARROWS_PER_ORBIT` by default).
21
+ * @returns The arrows, each at a point of the line.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { arrowSites } from "@johnmorrisdotca/wakusei-planets/space";
26
+ *
27
+ * const circle = Array.from({ length: 97 }, (_, i) => [Math.cos((i / 96) * 2 * Math.PI), 0, Math.sin((i / 96) * 2 * Math.PI)] as const);
28
+ * console.log(arrowSites(circle, 4).map((a) => a.direction.map((n) => n.toFixed(2)).join(",")).join(" | "));
29
+ * ```
30
+ */
31
+ export function arrowSites(line, arrows = ARROWS_PER_ORBIT) {
32
+ const n = line.length - 1;
33
+ const out = [];
34
+ if (n < 4 || arrows < 1)
35
+ return out;
36
+ for (let k = 0; k < arrows; k += 1) {
37
+ // k + 1/2 of the way round in equal steps: never at the body (the first point) or at the join
38
+ const i = Math.min(n - 1, Math.max(1, Math.round(((k + 0.5) / arrows) * n)));
39
+ const before = line[i - 1];
40
+ const after = line[i + 1];
41
+ const direction = unit([after[0] - before[0], after[1] - before[1], after[2] - before[2]]);
42
+ out.push({ position: line[i], direction });
43
+ }
44
+ return out;
45
+ }
46
+ /**
47
+ * The three points of a small arrowhead that is `size` long in the picture: its tip at `position`, and the ends of its two wings, laid in the plane of the picture so it points the way `direction` goes on the screen.
48
+ * A direction straight into or out of the picture has no way on the screen and gives none.
49
+ *
50
+ * @param position - The tip, in the scene's units.
51
+ * @param direction - The way it points in the scene.
52
+ * @param right - The camera's right-hand direction (a unit vector).
53
+ * @param up - The camera's up direction (a unit vector).
54
+ * @param size - The length of a wing, in the scene's units at the tip (pixels times the size of a pixel there).
55
+ * @returns The tip and the two wing ends, or `null`.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * import { chevron } from "@johnmorrisdotca/wakusei-planets/space";
60
+ *
61
+ * console.log(chevron([0, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], 2)?.map((p) => p.map((n) => n.toFixed(1)).join(",")).join(" "));
62
+ * ```
63
+ */
64
+ export function chevron(position, direction, right, up, size) {
65
+ const dx = dot(direction, right);
66
+ const dy = dot(direction, up);
67
+ const l = Math.hypot(dx, dy);
68
+ if (l < 1e-3)
69
+ return null;
70
+ const [ux, uy] = [dx / l, dy / l];
71
+ const alpha = 28 * RAD;
72
+ const wing = (sign) => {
73
+ // the direction on the screen, turned back by `alpha` to one side or the other
74
+ const vx = -(ux * Math.cos(alpha) - sign * uy * Math.sin(alpha));
75
+ const vy = -(uy * Math.cos(alpha) + sign * ux * Math.sin(alpha));
76
+ return add(position, add(scale(right, vx * size), scale(up, vy * size)));
77
+ };
78
+ return [position, wing(1), wing(-1)];
79
+ }
80
+ /**
81
+ * The curved arrow of the help mode: points on a circle round the axis a body spins about, turning the way it turns (by the right-hand rule about `axis`), as `segments + 1` points from its tail to its head, and the
82
+ * two ends of the head's wings. The circle has radius `radius` and sits `lift` along the axis from `centre`; the arc covers 250 degrees, ending where it started 250 degrees on.
83
+ *
84
+ * @param centre - The body's centre.
85
+ * @param axis - The unit axis it spins about (the right-hand rule: the fingers curl the way it turns).
86
+ * @param radius - The circle's radius, in the scene's units.
87
+ * @param lift - How far along the axis the circle is from the centre.
88
+ * @param segments - How many pieces the arc is cut into.
89
+ * @returns The points of the arc, and the points of the arrowhead's wings (the head is at the arc's last point).
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * import { spinArrow } from "@johnmorrisdotca/wakusei-planets/space";
94
+ *
95
+ * const { arc } = spinArrow([0, 0, 0], [0, 1, 0], 1.3, 0.5, 20);
96
+ * console.log(arc.length);
97
+ * ```
98
+ */
99
+ export function spinArrow(centre, axis, radius, lift, segments = 28) {
100
+ const k = unit(axis);
101
+ const seed = Math.abs(k[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];
102
+ const e1 = unit(cross(seed, k));
103
+ const e2 = cross(k, e1);
104
+ const span = 250 * RAD;
105
+ const at = (phi) => add(add(centre, scale(k, lift)), add(scale(e1, radius * Math.cos(phi)), scale(e2, radius * Math.sin(phi))));
106
+ const arc = [];
107
+ for (let i = 0; i <= segments; i += 1)
108
+ arc.push(at(-span / 2 + (i / segments) * span));
109
+ // the head: two short strokes back from the tip, in the plane of the circle, either side of the way it goes (the tangent, increasing phi)
110
+ const tip = arc[segments];
111
+ const phi = span / 2;
112
+ const tangent = unit(add(scale(e1, -Math.sin(phi)), scale(e2, Math.cos(phi))));
113
+ const outward = unit(add(scale(e1, Math.cos(phi)), scale(e2, Math.sin(phi))));
114
+ const back = 0.16 * radius;
115
+ const head = [add(tip, add(scale(tangent, -back), scale(outward, back * 0.55))), add(tip, add(scale(tangent, -back), scale(outward, -back * 0.55)))];
116
+ return { arc, head };
117
+ }
@@ -0,0 +1,74 @@
1
+ import { type Vec3 } from "../vec.ts";
2
+ /**
3
+ * How many radii of the moon the camera keeps from it at zoom 1: the moon then spans about 20 degrees of the view.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { BESIDE_RADII } from "@johnmorrisdotca/wakusei-planets/space";
8
+ *
9
+ * console.log(BESIDE_RADII);
10
+ * ```
11
+ */
12
+ export declare const BESIDE_RADII = 5.8;
13
+ /**
14
+ * What `besideMoon` is asked: where the moon, the planet it goes round and the Sun are and how big the moon and the planet are (one unit for all), the shape of the view, and the shared controls' zoom, yaw and tilt.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import type { BesideInput } from "@johnmorrisdotca/wakusei-planets/space";
19
+ *
20
+ * const input: BesideInput = { moon: [60, 0, 0], moonRadius: 0.273, host: [0, 0, 0], hostRadius: 1, sun: [-23000, 0, 0], aspect: 1.6, zoom: 1, yaw: 0, tilt: 0 };
21
+ * console.log(input.moonRadius);
22
+ * ```
23
+ */
24
+ export interface BesideInput {
25
+ moon: Vec3;
26
+ moonRadius: number;
27
+ host: Vec3;
28
+ hostRadius: number;
29
+ sun: Vec3;
30
+ /** The view's width over its height. */
31
+ aspect: number;
32
+ /** 1 keeps `BESIDE_RADII` moon radii from the moon; more goes closer, less goes further. */
33
+ zoom: number;
34
+ /** Degrees the camera swings round the line from the planet through the moon (a drag across the view is 180). */
35
+ yaw: number;
36
+ /** Degrees the camera opens further off that line (a drag down the view is 60). */
37
+ tilt: number;
38
+ }
39
+ /**
40
+ * The camera `besideMoon` works out: where it is, which way it looks and its up, its vertical field of view in degrees, how far it is from the moon, and the angle in degrees between the moon and the planet as the camera sees them.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * import type { BesideCamera } from "@johnmorrisdotca/wakusei-planets/space";
45
+ *
46
+ * const describe = (camera: BesideCamera): string => `${camera.separation.toFixed(0)} degrees apart in a ${camera.fov.toFixed(0)} degree view`;
47
+ * console.log(typeof describe);
48
+ * ```
49
+ */
50
+ export interface BesideCamera {
51
+ position: Vec3;
52
+ forward: Vec3;
53
+ up: Vec3;
54
+ fov: number;
55
+ distance: number;
56
+ separation: number;
57
+ }
58
+ /**
59
+ * The camera for a view from near a moon with its planet still in view: beyond the moon on the side away from the planet and a little off the line to it, so the moon is large in the foreground and the planet is behind it,
60
+ * at the angular size it really has from there. The camera sits on the Sun's side of that line where it can, so the moon shows a lit face. It opens off the line, from 25 degrees, until the two discs no longer touch
61
+ * (with 2 degrees to spare), turns the picture so the planet is to one side of the moon, and widens the field of view until both fit the view's narrower side.
62
+ *
63
+ * @param input - The places and sizes, the view's shape, and the zoom, yaw and tilt.
64
+ * @returns The camera.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * import { besideMoon } from "@johnmorrisdotca/wakusei-planets/space";
69
+ *
70
+ * const camera = besideMoon({ moon: [60.3, 0, 0], moonRadius: 0.273, host: [0, 0, 0], hostRadius: 1, sun: [-23000, 5000, 9000], aspect: 1.6, zoom: 1, yaw: 0, tilt: 0 });
71
+ * console.log(camera.distance.toFixed(2), camera.separation.toFixed(0));
72
+ * ```
73
+ */
74
+ export declare function besideMoon(input: BesideInput): BesideCamera;
@@ -0,0 +1,76 @@
1
+ // A camera placed beside and beyond a moon, off the line to the planet it goes round: the moon is large in the foreground and the planet is in view behind it, at its real angular size. Pure: places in, a camera out.
2
+ import { add, cross, dot, length, RAD, scale, unit } from "../vec.js";
3
+ /**
4
+ * How many radii of the moon the camera keeps from it at zoom 1: the moon then spans about 20 degrees of the view.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { BESIDE_RADII } from "@johnmorrisdotca/wakusei-planets/space";
9
+ *
10
+ * console.log(BESIDE_RADII);
11
+ * ```
12
+ */
13
+ export const BESIDE_RADII = 5.8;
14
+ /** The angle off the line from the planet through the moon, in degrees, the camera first tries; it opens wider until the planet clears the moon. */
15
+ const FIRST_ANGLE = 25;
16
+ const angleBetween = (a, b) => Math.acos(Math.max(-1, Math.min(1, dot(unit(a), unit(b))))) / RAD;
17
+ const minus = (a, b) => add(a, scale(b, -1));
18
+ /**
19
+ * The camera for a view from near a moon with its planet still in view: beyond the moon on the side away from the planet and a little off the line to it, so the moon is large in the foreground and the planet is behind it,
20
+ * at the angular size it really has from there. The camera sits on the Sun's side of that line where it can, so the moon shows a lit face. It opens off the line, from 25 degrees, until the two discs no longer touch
21
+ * (with 2 degrees to spare), turns the picture so the planet is to one side of the moon, and widens the field of view until both fit the view's narrower side.
22
+ *
23
+ * @param input - The places and sizes, the view's shape, and the zoom, yaw and tilt.
24
+ * @returns The camera.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { besideMoon } from "@johnmorrisdotca/wakusei-planets/space";
29
+ *
30
+ * const camera = besideMoon({ moon: [60.3, 0, 0], moonRadius: 0.273, host: [0, 0, 0], hostRadius: 1, sun: [-23000, 5000, 9000], aspect: 1.6, zoom: 1, yaw: 0, tilt: 0 });
31
+ * console.log(camera.distance.toFixed(2), camera.separation.toFixed(0));
32
+ * ```
33
+ */
34
+ export function besideMoon(input) {
35
+ const { moon, moonRadius, host, hostRadius } = input;
36
+ const away = unit(minus(moon, host));
37
+ const toSun = unit(minus(input.sun, moon));
38
+ let side = minus(toSun, scale(away, dot(toSun, away)));
39
+ if (length(side) < 1e-3)
40
+ side = cross(away, [0, 1, 0]);
41
+ if (length(side) < 1e-3)
42
+ side = cross(away, [1, 0, 0]);
43
+ side = unit(side);
44
+ const distance = Math.max(1.05 * moonRadius, (BESIDE_RADII * moonRadius) / Math.max(1e-9, input.zoom));
45
+ const swing = (v) => {
46
+ // the camera swings round the line from the planet through the moon by `yaw`
47
+ const turn = input.yaw * RAD;
48
+ return add(add(scale(v, Math.cos(turn)), scale(cross(away, v), Math.sin(turn))), scale(away, dot(away, v) * (1 - Math.cos(turn))));
49
+ };
50
+ const place = (angle) => {
51
+ const direction = swing(add(scale(away, Math.cos(angle * RAD)), scale(side, Math.sin(angle * RAD))));
52
+ const position = add(moon, scale(direction, distance));
53
+ return { position, separation: angleBetween(minus(moon, position), minus(host, position)) };
54
+ };
55
+ const moonHalf = (position) => Math.asin(Math.min(1, moonRadius / Math.max(moonRadius * 1.0001, length(minus(moon, position))))) / RAD;
56
+ const hostHalf = (position) => Math.asin(Math.min(1, hostRadius / Math.max(hostRadius * 1.0001, length(minus(host, position))))) / RAD;
57
+ let angle = Math.max(5, Math.min(85, FIRST_ANGLE + input.tilt));
58
+ let spot = place(angle);
59
+ while (angle < 85 && spot.separation < moonHalf(spot.position) + hostHalf(spot.position) + 2) {
60
+ angle += 2;
61
+ spot = place(angle);
62
+ }
63
+ const { position } = spot;
64
+ const toMoon = unit(minus(moon, position));
65
+ const toHost = unit(minus(host, position));
66
+ // look between the two, and turn the picture so the planet is to the side of the moon, not above or below it
67
+ const forward = unit(add(toMoon, toHost));
68
+ const shift = minus(toHost, toMoon);
69
+ const across = unit(minus(shift, scale(forward, dot(shift, forward))));
70
+ const up = unit(cross(across, forward));
71
+ // both discs fit: the half-angle from the middle to the far edge of each, across and up
72
+ const reach = Math.max(angleBetween(forward, toMoon) + moonHalf(position), angleBetween(forward, toHost) + hostHalf(position)) + 3;
73
+ const half = Math.max(Math.tan(reach * RAD) / Math.min(1.6, Math.max(0.3, input.aspect)), Math.tan((moonHalf(position) + 3) * RAD));
74
+ const fov = Math.min(110, Math.max(35, 2 * Math.atan(half) / RAD));
75
+ return { position, forward, up, fov, distance, separation: spot.separation };
76
+ }
@@ -0,0 +1,83 @@
1
+ import { type BodyId, type HostId } from "../facts.ts";
2
+ /**
3
+ * What a body is: the Sun, one of the eight planets, a dwarf planet (Pluto among them) or a moon.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { bodyKindOf, type SpaceKind } from "@johnmorrisdotca/wakusei-planets/space";
8
+ *
9
+ * const kind: SpaceKind = bodyKindOf("titan");
10
+ * console.log(kind);
11
+ * ```
12
+ */
13
+ export type SpaceKind = "star" | "planet" | "dwarf-planet" | "moon";
14
+ /**
15
+ * Every body the package knows, in the order a hopper goes: the Sun, then each planet and dwarf planet from the Sun outwards, each followed by its moons nearest first (Mercury, Venus, the Earth and the
16
+ * Moon, Mars, Phobos and Deimos, Ceres, Jupiter and its four large moons, and on to Sedna).
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { HOP_ORDER } from "@johnmorrisdotca/wakusei-planets/space";
21
+ *
22
+ * console.log(HOP_ORDER.slice(0, 7).join(", "));
23
+ * ```
24
+ */
25
+ export declare const HOP_ORDER: readonly BodyId[];
26
+ /**
27
+ * What kind of body a name is.
28
+ *
29
+ * @param id - A body's name.
30
+ * @returns `star` for the Sun, `planet` for the eight planets, `dwarf-planet` for Pluto and the others, or `moon`.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { bodyKindOf } from "@johnmorrisdotca/wakusei-planets/space";
35
+ *
36
+ * console.log(bodyKindOf("ceres"), bodyKindOf("moon"));
37
+ * ```
38
+ */
39
+ export declare function bodyKindOf(id: BodyId): SpaceKind;
40
+ /**
41
+ * The planet or dwarf planet a moon goes round, or `undefined` for a body that is not a moon.
42
+ *
43
+ * @param id - A body's name.
44
+ * @returns The host's name for a moon, `undefined` for any other body.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * import { hostOf } from "@johnmorrisdotca/wakusei-planets/space";
49
+ *
50
+ * console.log(hostOf("titan"), hostOf("mars"));
51
+ * ```
52
+ */
53
+ export declare function hostOf(id: BodyId): HostId | undefined;
54
+ /**
55
+ * Read a body from what a reader or a page might write: its name in the package's lower case, in English or in Japanese, in any case.
56
+ *
57
+ * @param text - `saturn`, `Saturn`, `土星`.
58
+ * @returns The body's name, or `undefined` when no body has it.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { findBody } from "@johnmorrisdotca/wakusei-planets/space";
63
+ *
64
+ * console.log(findBody("Saturn"), findBody("土星"), findBody("nothing"));
65
+ * ```
66
+ */
67
+ export declare function findBody(text: string): BodyId | undefined;
68
+ /**
69
+ * The body after `id` in the hopper's order (or before it with a negative `step`), among those `can` accepts, going round from the last to the first.
70
+ *
71
+ * @param id - Where the hopper is.
72
+ * @param step - `1` for the next body, `-1` for the previous one.
73
+ * @param can - Which bodies the view can go to (all of them by default).
74
+ * @returns The next body, or `id` itself when no other is accepted.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * import { hopFrom } from "@johnmorrisdotca/wakusei-planets/space";
79
+ *
80
+ * console.log(hopFrom("earth", 1), hopFrom("earth", -1), hopFrom("sun", -1, (id) => id !== "sedna"));
81
+ * ```
82
+ */
83
+ export declare function hopFrom(id: BodyId, step: 1 | -1, can?: (id: BodyId) => boolean): BodyId;
@@ -0,0 +1,93 @@
1
+ // Which bodies the space view can put at the middle of its view, and in what order a reader hops from one to the next. Pure: names in, names out.
2
+ import { BODIES, MOONS_OF } from "../facts.js";
3
+ /** Planets, then dwarf planets, ordered by the distance of their orbits from the Sun, each followed by its moons nearest first. */
4
+ const SUN_ORDER = ["mercury", "venus", "earth", "mars", "ceres", "jupiter", "saturn", "uranus", "neptune", "pluto", "orcus", "haumea", "quaoar", "makemake", "gonggong", "eris", "sedna"];
5
+ /**
6
+ * Every body the package knows, in the order a hopper goes: the Sun, then each planet and dwarf planet from the Sun outwards, each followed by its moons nearest first (Mercury, Venus, the Earth and the
7
+ * Moon, Mars, Phobos and Deimos, Ceres, Jupiter and its four large moons, and on to Sedna).
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { HOP_ORDER } from "@johnmorrisdotca/wakusei-planets/space";
12
+ *
13
+ * console.log(HOP_ORDER.slice(0, 7).join(", "));
14
+ * ```
15
+ */
16
+ export const HOP_ORDER = ["sun", ...SUN_ORDER.flatMap((id) => [id, ...(MOONS_OF[id] ?? [])])];
17
+ /**
18
+ * What kind of body a name is.
19
+ *
20
+ * @param id - A body's name.
21
+ * @returns `star` for the Sun, `planet` for the eight planets, `dwarf-planet` for Pluto and the others, or `moon`.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { bodyKindOf } from "@johnmorrisdotca/wakusei-planets/space";
26
+ *
27
+ * console.log(bodyKindOf("ceres"), bodyKindOf("moon"));
28
+ * ```
29
+ */
30
+ export function bodyKindOf(id) {
31
+ return BODIES[id].kind;
32
+ }
33
+ /**
34
+ * The planet or dwarf planet a moon goes round, or `undefined` for a body that is not a moon.
35
+ *
36
+ * @param id - A body's name.
37
+ * @returns The host's name for a moon, `undefined` for any other body.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * import { hostOf } from "@johnmorrisdotca/wakusei-planets/space";
42
+ *
43
+ * console.log(hostOf("titan"), hostOf("mars"));
44
+ * ```
45
+ */
46
+ export function hostOf(id) {
47
+ return BODIES[id].kind === "moon" ? BODIES[id].parent ?? "earth" : undefined;
48
+ }
49
+ /**
50
+ * Read a body from what a reader or a page might write: its name in the package's lower case, in English or in Japanese, in any case.
51
+ *
52
+ * @param text - `saturn`, `Saturn`, `土星`.
53
+ * @returns The body's name, or `undefined` when no body has it.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * import { findBody } from "@johnmorrisdotca/wakusei-planets/space";
58
+ *
59
+ * console.log(findBody("Saturn"), findBody("土星"), findBody("nothing"));
60
+ * ```
61
+ */
62
+ export function findBody(text) {
63
+ const wanted = text.trim().toLowerCase();
64
+ for (const id of HOP_ORDER)
65
+ if (id === wanted || BODIES[id].name.en.toLowerCase() === wanted || BODIES[id].name.ja === text.trim())
66
+ return id;
67
+ return undefined;
68
+ }
69
+ /**
70
+ * The body after `id` in the hopper's order (or before it with a negative `step`), among those `can` accepts, going round from the last to the first.
71
+ *
72
+ * @param id - Where the hopper is.
73
+ * @param step - `1` for the next body, `-1` for the previous one.
74
+ * @param can - Which bodies the view can go to (all of them by default).
75
+ * @returns The next body, or `id` itself when no other is accepted.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * import { hopFrom } from "@johnmorrisdotca/wakusei-planets/space";
80
+ *
81
+ * console.log(hopFrom("earth", 1), hopFrom("earth", -1), hopFrom("sun", -1, (id) => id !== "sedna"));
82
+ * ```
83
+ */
84
+ export function hopFrom(id, step, can = () => true) {
85
+ const n = HOP_ORDER.length;
86
+ const at = HOP_ORDER.indexOf(id);
87
+ for (let i = 1; i < n; i += 1) {
88
+ const next = HOP_ORDER[(((at + step * i) % n) + n) % n];
89
+ if (can(next))
90
+ return next;
91
+ }
92
+ return id;
93
+ }
@@ -0,0 +1,31 @@
1
+ /** A game clock's two sources of time: the page's monotonic clock (`performance.now()`) and the wall clock (`Date.now()`), both in milliseconds. */
2
+ export interface ClockSource {
3
+ /** The page's monotonic clock, from which the time that has passed is taken. */
4
+ now(): number;
5
+ /** The wall clock, for a clock that follows the real time (`"now"` at speed 1). */
6
+ wall(): number;
7
+ }
8
+ /** The game clock `createGameClock` returns. */
9
+ export interface GameClock {
10
+ /** The moment shown as of the last look, in milliseconds since 1970. */
11
+ readonly time: number;
12
+ /** Game seconds for each real second: 1 is real time, 0 holds, negative runs backwards. */
13
+ readonly speed: number;
14
+ /** Bring the moment up to the page's clock at `now` and return it: the time that really passed since the last look, at the speed it ran at, however few frames were drawn in between. */
15
+ advance(now?: number): number;
16
+ /** Set the moment shown: milliseconds since 1970, or `"now"` to follow the wall clock. */
17
+ set(time: number | "now", now?: number): void;
18
+ /** Change the speed. The time up to this moment is worked out at the old speed first, so a hold keeps the moment the reader saw last. */
19
+ setSpeed(speed: number, now?: number): void;
20
+ }
21
+ /**
22
+ * A game clock that advances by the time that really passed between two looks, never by a frame's step: at 86,400 times real time it moves a day for every real second on any machine, however
23
+ * slowly it draws. It has no cap of its own: a page that was out of sight, or a machine that slept, finds the moment where the speed would have taken it. Time that does not pass is not counted:
24
+ * a hold (`speed` 0) keeps the moment it had, and `set` starts the count again from the moment it is given.
25
+ *
26
+ * @param start - The moment to start from, in milliseconds since 1970, or `"now"` to follow the wall clock.
27
+ * @param speed - Game seconds for each real second.
28
+ * @param source - The page's clocks; the browser's by default (a test passes its own, such as a slowed one).
29
+ * @returns The clock.
30
+ */
31
+ export declare function createGameClock(start: number | "now", speed: number, source?: ClockSource): GameClock;
@@ -0,0 +1,45 @@
1
+ // The space view's game clock: the moment shown, kept by the page's own clock between one look and the next. Pure, no DOM: the element gives it `performance.now()`, a test gives it a clock of its own.
2
+ /**
3
+ * A game clock that advances by the time that really passed between two looks, never by a frame's step: at 86,400 times real time it moves a day for every real second on any machine, however
4
+ * slowly it draws. It has no cap of its own: a page that was out of sight, or a machine that slept, finds the moment where the speed would have taken it. Time that does not pass is not counted:
5
+ * a hold (`speed` 0) keeps the moment it had, and `set` starts the count again from the moment it is given.
6
+ *
7
+ * @param start - The moment to start from, in milliseconds since 1970, or `"now"` to follow the wall clock.
8
+ * @param speed - Game seconds for each real second.
9
+ * @param source - The page's clocks; the browser's by default (a test passes its own, such as a slowed one).
10
+ * @returns The clock.
11
+ */
12
+ export function createGameClock(start, speed, source = { now: () => performance.now(), wall: () => Date.now() }) {
13
+ let mode = start;
14
+ let time = start === "now" ? source.wall() : start;
15
+ let rate = Number.isFinite(speed) ? speed : 1;
16
+ let last = source.now();
17
+ const advance = (now) => {
18
+ if (rate === 1 && mode === "now")
19
+ time = source.wall();
20
+ else
21
+ time += Math.max(0, now - last) * rate;
22
+ last = Math.max(last, now);
23
+ return time;
24
+ };
25
+ return {
26
+ get time() {
27
+ return time;
28
+ },
29
+ get speed() {
30
+ return rate;
31
+ },
32
+ advance: (now = source.now()) => advance(now),
33
+ set(value, now = source.now()) {
34
+ mode = value;
35
+ time = value === "now" ? source.wall() : value;
36
+ last = now;
37
+ },
38
+ setSpeed(value, now = source.now()) {
39
+ if (rate !== 0)
40
+ advance(now);
41
+ rate = Number.isFinite(value) ? value : 1;
42
+ last = now;
43
+ },
44
+ };
45
+ }