@typeonce/ritmo-math 0.14.0 → 0.15.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 (108) hide show
  1. package/CHANGELOG.md +104 -0
  2. package/README.md +17 -12
  3. package/dist/Angle.d.ts +44 -0
  4. package/dist/Angle.d.ts.map +1 -0
  5. package/dist/Angle.js +62 -0
  6. package/dist/Angle.js.map +1 -0
  7. package/dist/Box2.d.ts +16 -4
  8. package/dist/Box2.d.ts.map +1 -1
  9. package/dist/Box2.js +16 -1
  10. package/dist/Box2.js.map +1 -1
  11. package/dist/Collide2.d.ts +11 -1
  12. package/dist/Collide2.d.ts.map +1 -1
  13. package/dist/Collide2.js +16 -1
  14. package/dist/Collide2.js.map +1 -1
  15. package/dist/FlowField.d.ts +24 -38
  16. package/dist/FlowField.d.ts.map +1 -1
  17. package/dist/FlowField.js +18 -5
  18. package/dist/FlowField.js.map +1 -1
  19. package/dist/Scalar.d.ts +19 -166
  20. package/dist/Scalar.d.ts.map +1 -1
  21. package/dist/Scalar.js +51 -51
  22. package/dist/Scalar.js.map +1 -1
  23. package/dist/Vec2.d.ts +47 -7
  24. package/dist/Vec2.d.ts.map +1 -1
  25. package/dist/Vec2.js +56 -8
  26. package/dist/Vec2.js.map +1 -1
  27. package/dist/index.d.ts +9 -22
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +9 -22
  30. package/dist/index.js.map +1 -1
  31. package/package.json +3 -3
  32. package/src/Angle.ts +60 -0
  33. package/src/Box2.ts +20 -4
  34. package/src/Collide2.ts +18 -2
  35. package/src/FlowField.ts +34 -10
  36. package/src/Scalar.ts +43 -165
  37. package/src/Vec2.ts +58 -8
  38. package/src/index.ts +9 -22
  39. package/dist/Aabb.d.ts +0 -182
  40. package/dist/Aabb.d.ts.map +0 -1
  41. package/dist/Aabb.js +0 -109
  42. package/dist/Aabb.js.map +0 -1
  43. package/dist/InputAxis.d.ts +0 -44
  44. package/dist/InputAxis.d.ts.map +0 -1
  45. package/dist/InputAxis.js +0 -71
  46. package/dist/InputAxis.js.map +0 -1
  47. package/dist/Ops.d.ts +0 -209
  48. package/dist/Ops.d.ts.map +0 -1
  49. package/dist/Ops.js +0 -210
  50. package/dist/Ops.js.map +0 -1
  51. package/dist/Random.d.ts +0 -124
  52. package/dist/Random.d.ts.map +0 -1
  53. package/dist/Random.js +0 -126
  54. package/dist/Random.js.map +0 -1
  55. package/dist/Size2.d.ts +0 -118
  56. package/dist/Size2.d.ts.map +0 -1
  57. package/dist/Size2.js +0 -54
  58. package/dist/Size2.js.map +0 -1
  59. package/dist/SpatialGrid.d.ts +0 -68
  60. package/dist/SpatialGrid.d.ts.map +0 -1
  61. package/dist/SpatialGrid.js +0 -112
  62. package/dist/SpatialGrid.js.map +0 -1
  63. package/dist/Vector2.d.ts +0 -214
  64. package/dist/Vector2.d.ts.map +0 -1
  65. package/dist/Vector2.js +0 -134
  66. package/dist/Vector2.js.map +0 -1
  67. package/dist/internal/aabb.d.ts +0 -27
  68. package/dist/internal/aabb.d.ts.map +0 -1
  69. package/dist/internal/aabb.js +0 -52
  70. package/dist/internal/aabb.js.map +0 -1
  71. package/dist/internal/codec.d.ts +0 -9
  72. package/dist/internal/codec.d.ts.map +0 -1
  73. package/dist/internal/codec.js +0 -16
  74. package/dist/internal/codec.js.map +0 -1
  75. package/dist/internal/fdlibm.d.ts +0 -43
  76. package/dist/internal/fdlibm.d.ts.map +0 -1
  77. package/dist/internal/fdlibm.js +0 -1291
  78. package/dist/internal/fdlibm.js.map +0 -1
  79. package/dist/internal/inputAxis.d.ts +0 -11
  80. package/dist/internal/inputAxis.d.ts.map +0 -1
  81. package/dist/internal/inputAxis.js +0 -13
  82. package/dist/internal/inputAxis.js.map +0 -1
  83. package/dist/internal/scalar.d.ts +0 -11
  84. package/dist/internal/scalar.d.ts.map +0 -1
  85. package/dist/internal/scalar.js +0 -68
  86. package/dist/internal/scalar.js.map +0 -1
  87. package/dist/internal/size2.d.ts +0 -22
  88. package/dist/internal/size2.d.ts.map +0 -1
  89. package/dist/internal/size2.js +0 -38
  90. package/dist/internal/size2.js.map +0 -1
  91. package/dist/internal/vector2.d.ts +0 -39
  92. package/dist/internal/vector2.d.ts.map +0 -1
  93. package/dist/internal/vector2.js +0 -78
  94. package/dist/internal/vector2.js.map +0 -1
  95. package/src/Aabb.ts +0 -180
  96. package/src/InputAxis.ts +0 -81
  97. package/src/Ops.ts +0 -232
  98. package/src/Random.ts +0 -225
  99. package/src/Size2.ts +0 -122
  100. package/src/SpatialGrid.ts +0 -172
  101. package/src/Vector2.ts +0 -216
  102. package/src/internal/aabb.ts +0 -81
  103. package/src/internal/codec.ts +0 -21
  104. package/src/internal/fdlibm.ts +0 -1256
  105. package/src/internal/inputAxis.ts +0 -21
  106. package/src/internal/scalar.ts +0 -104
  107. package/src/internal/size2.ts +0 -54
  108. package/src/internal/vector2.ts +0 -121
package/dist/Vec2.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Vec2.js","sourceRoot":"","sources":["../src/Vec2.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,GAAG,MAAM,UAAU,CAAA;AAY/B;;;;GAIG;AACH,MAAM,CAAC,MAAM,IAAI,GAAS,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAA;AAEvD;;;;GAIG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,CAAO,EAAE,CAAO,EAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;AAE/E;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,CAAO,EAAE,CAAO,EAAQ,EAAE,CA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AY,EAAE,EAAU,EAAQ,EAAE;IACzF,MAAM,CAAC,GAAG,IAAI,GAAG,EAAE,CAAA;IACnB,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAAE,OAAO,OAAO,CAAA;IAC5B,IAAI,CAAC,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAA;IACjC,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1B,OAAO,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAA;AACzG,CAAC,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,32 +1,19 @@
1
1
  /**
2
- * Math for game code.
2
+ * The 2D helpers most games would otherwise rewrite, as plain data in and
3
+ * plain data out, built on `Ops` so they replay the same on every engine.
3
4
  *
4
- * Validated values (`Scalar`, `Vector2`, `Size2`, `Aabb`) expose
5
- * Result-returning constructors, a guard (`is`), and one `codec` each, so
6
- * they plug into `Descriptor.ConstructedComponent(Vector2.codec)` /
7
- * `ConstructedResource(Random.codec)` and are validated once at the boundary. `InputAxis` normalizes directional input.
8
- *
9
- * Hot-path helpers work on plain numbers every frame: `Vec2` arithmetic,
10
- * `Box2` edges, `Segment2` walls at any angle, `Collide2` swept circle
11
- * collision against both, a `SpatialGrid` broad
12
- * phase, a `FlowField` for finding the way around walls, and a deterministic
13
- * `Random` whose state is one integer.
14
- *
15
- * `Ops` holds the math functions engines are allowed to approximate (`sin`,
16
- * `cos`, `atan2`, `exp`, `pow`, `hypot`, ...) as ports that return the same
17
- * bits everywhere: use them in simulation code instead of `Math`'s.
5
+ * `Vec2` arithmetic and angles of vectors, `Angle` for headings, `Scalar` for
6
+ * numbers moving toward a target, `Box2` edge boxes and `Segment2` walls at
7
+ * any angle, `Collide2` swept circle collision against both that slides
8
+ * along walls and never tunnels, and a `FlowField` that leads bodies around
9
+ * walls. Values are validated by the game's own schema when they enter the
10
+ * world; `Ops` and `Random` are in `@typeonce/ritmo`.
18
11
  */
19
- export * as Aabb from "./Aabb.ts";
12
+ export * as Angle from "./Angle.ts";
20
13
  export * as Box2 from "./Box2.ts";
21
14
  export * as Collide2 from "./Collide2.ts";
22
15
  export * as FlowField from "./FlowField.ts";
23
- export * as InputAxis from "./InputAxis.ts";
24
- export * as Ops from "./Ops.ts";
25
- export * as Random from "./Random.ts";
26
16
  export * as Scalar from "./Scalar.ts";
27
17
  export * as Segment2 from "./Segment2.ts";
28
- export * as Size2 from "./Size2.ts";
29
- export * as SpatialGrid from "./SpatialGrid.ts";
30
18
  export * as Vec2 from "./Vec2.ts";
31
- export * as Vector2 from "./Vector2.ts";
32
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,GAAG,MAAM,UAAU,CAAA;AAC/B,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA"}
package/dist/index.js CHANGED
@@ -1,32 +1,19 @@
1
1
  /**
2
- * Math for game code.
2
+ * The 2D helpers most games would otherwise rewrite, as plain data in and
3
+ * plain data out, built on `Ops` so they replay the same on every engine.
3
4
  *
4
- * Validated values (`Scalar`, `Vector2`, `Size2`, `Aabb`) expose
5
- * Result-returning constructors, a guard (`is`), and one `codec` each, so
6
- * they plug into `Descriptor.ConstructedComponent(Vector2.codec)` /
7
- * `ConstructedResource(Random.codec)` and are validated once at the boundary. `InputAxis` normalizes directional input.
8
- *
9
- * Hot-path helpers work on plain numbers every frame: `Vec2` arithmetic,
10
- * `Box2` edges, `Segment2` walls at any angle, `Collide2` swept circle
11
- * collision against both, a `SpatialGrid` broad
12
- * phase, a `FlowField` for finding the way around walls, and a deterministic
13
- * `Random` whose state is one integer.
14
- *
15
- * `Ops` holds the math functions engines are allowed to approximate (`sin`,
16
- * `cos`, `atan2`, `exp`, `pow`, `hypot`, ...) as ports that return the same
17
- * bits everywhere: use them in simulation code instead of `Math`'s.
5
+ * `Vec2` arithmetic and angles of vectors, `Angle` for headings, `Scalar` for
6
+ * numbers moving toward a target, `Box2` edge boxes and `Segment2` walls at
7
+ * any angle, `Collide2` swept circle collision against both that slides
8
+ * along walls and never tunnels, and a `FlowField` that leads bodies around
9
+ * walls. Values are validated by the game's own schema when they enter the
10
+ * world; `Ops` and `Random` are in `@typeonce/ritmo`.
18
11
  */
19
- export * as Aabb from "./Aabb.js";
12
+ export * as Angle from "./Angle.js";
20
13
  export * as Box2 from "./Box2.js";
21
14
  export * as Collide2 from "./Collide2.js";
22
15
  export * as FlowField from "./FlowField.js";
23
- export * as InputAxis from "./InputAxis.js";
24
- export * as Ops from "./Ops.js";
25
- export * as Random from "./Random.js";
26
16
  export * as Scalar from "./Scalar.js";
27
17
  export * as Segment2 from "./Segment2.js";
28
- export * as Size2 from "./Size2.js";
29
- export * as SpatialGrid from "./SpatialGrid.js";
30
18
  export * as Vec2 from "./Vec2.js";
31
- export * as Vector2 from "./Vector2.js";
32
19
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,GAAG,MAAM,UAAU,CAAA;AAC/B,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@typeonce/ritmo-math",
3
- "version": "0.14.0",
4
- "description": "Validated math values and hot-path game helpers (collision, spatial grid, flow field, random) for Ritmo games",
3
+ "version": "0.15.0",
4
+ "description": "The 2D helpers most Ritmo games would otherwise rewrite (vectors, angles, swept collision, flow field), plain data in and out, built on Ops so they replay the same on every engine",
5
5
  "author": "Sandro Maglione",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -47,7 +47,7 @@
47
47
  "provenance": true
48
48
  },
49
49
  "dependencies": {
50
- "@typeonce/ritmo": "^0.14.0"
50
+ "@typeonce/ritmo": "^0.15.0"
51
51
  },
52
52
  "engines": {
53
53
  "node": ">=20"
package/src/Angle.ts ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Angles in radians, for headings a system stores as one number: a turret's
3
+ * aim, a body's facing.
4
+ *
5
+ * Angles wrap: 350° and -10° are the same heading, so comparing or turning
6
+ * them needs the wrap first. `wrap` brings any angle into [-π, π), and the
7
+ * difference between two headings is `wrap(to - from)`, the short way round.
8
+ * Both helpers are exact (`%` and `+ -` only), so they replay the same on
9
+ * every engine. For the angle of a vector use `Vec2.angle`, for the unit
10
+ * vector of an angle `Vec2.fromAngle`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * // Turn a turret toward its target at a capped rate.
15
+ * const wanted = Vec2.angle(Vec2.subtract(target, turret.position))
16
+ * const aim = Angle.turnToward(turret.aim, wanted, turnRate * dt)
17
+ * ```
18
+ *
19
+ * @module Angle
20
+ * @docGroup helpers
21
+ *
22
+ * @categoryDescription Operations
23
+ * Wrapping and turning angles.
24
+ */
25
+
26
+ const TAU = 2 * Math.PI
27
+
28
+ /**
29
+ * `angle` brought into [-π, π) by whole turns: π and -π both give -π. Use it
30
+ * to keep a stored heading bounded, and for the signed short-way difference
31
+ * between two headings, `wrap(to - from)`. Exact on every engine.
32
+ *
33
+ * @category Operations
34
+ */
35
+ export const wrap = (angle: number): number => {
36
+ // `%` is exact, and so is adding or removing one turn from a value within
37
+ // one turn of it, so the result is the exact remainder.
38
+ let wrapped = angle % TAU
39
+ if (wrapped >= Math.PI) wrapped -= TAU
40
+ else if (wrapped < -Math.PI) wrapped += TAU
41
+ return wrapped === 0 ? 0 : wrapped
42
+ }
43
+
44
+ /**
45
+ * `angle` turned toward `target` the short way by at most `maxStep` radians,
46
+ * never overshooting: `wrap(target)` once it is within `maxStep`, and
47
+ * `wrap(angle)` when `maxStep` is 0 or less. Headings exactly half a turn
48
+ * apart turn the negative way. For a capped turn rate (a turret, a homing
49
+ * shot); for plain numbers use `Scalar.moveToward`, for points
50
+ * `Vec2.moveToward`.
51
+ *
52
+ * @category Operations
53
+ */
54
+ export const turnToward = (angle: number, target: number, maxStep: number): number => {
55
+ const from = wrap(angle)
56
+ if (!(maxStep > 0)) return from
57
+ const gap = wrap(target - from)
58
+ if (Math.abs(gap) <= maxStep) return wrap(target)
59
+ return wrap(gap > 0 ? from + maxStep : from - maxStep)
60
+ }
package/src/Box2.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Plain axis-aligned boxes stored by their edges, for hot-path collision.
3
3
  *
4
- * `Aabb` is the validated, centered box for authored and stored values;
5
4
  * `Box2` is the edge form the collision helpers (`Collide2`, `FlowField`)
6
- * read every frame, built once from whatever the game stores.
5
+ * read every frame, built once from whatever the game stores (a center and a
6
+ * size, a tile's corner).
7
7
  *
8
8
  * @example
9
9
  * ```ts
@@ -18,7 +18,7 @@
18
18
  * The structural box shape.
19
19
  *
20
20
  * @categoryDescription Operations
21
- * Building boxes and testing them against circles.
21
+ * Building boxes, testing them against circles, and the closest point.
22
22
  */
23
23
  import type { Vec2 } from "./Vec2.ts"
24
24
 
@@ -49,7 +49,9 @@ export const around = (x: number, y: number, halfWidth: number, halfHeight: numb
49
49
  })
50
50
 
51
51
  /**
52
- * Whether a circle overlaps (or touches) the box.
52
+ * Whether a circle overlaps (or touches) the box. For two circles use
53
+ * `Collide2.overlapsCircleCircle`, for a wall at any angle
54
+ * `Segment2.overlapsCircle`.
53
55
  *
54
56
  * @category Operations
55
57
  */
@@ -58,3 +60,17 @@ export const overlapsCircle = (box: Box2, center: Vec2, radius: number): boolean
58
60
  const dy = center.y - Math.max(box.minY, Math.min(center.y, box.maxY))
59
61
  return dx * dx + dy * dy <= radius * radius
60
62
  }
63
+
64
+ /**
65
+ * The point of `box` closest to `point`: `point` clamped to the box's edges.
66
+ * A point inside the box comes back unchanged, so this keeps a body inside an
67
+ * arena, or gives the direction from an obstacle to a body outside it
68
+ * (`Vec2.subtract(body, closestPoint(box, body))`). To push a circle out of
69
+ * walls it overlaps, use `Collide2.pushOut`.
70
+ *
71
+ * @category Operations
72
+ */
73
+ export const closestPoint = (box: Box2, point: Vec2): Vec2 => ({
74
+ x: Math.max(box.minX, Math.min(point.x, box.maxX)),
75
+ y: Math.max(box.minY, Math.min(point.y, box.maxY))
76
+ })
package/src/Collide2.ts CHANGED
@@ -25,13 +25,13 @@
25
25
  * Contacts and movement results.
26
26
  *
27
27
  * @categoryDescription Queries
28
- * First-contact sweeps and overlap resolution.
28
+ * First-contact sweeps and overlap tests.
29
29
  *
30
30
  * @categoryDescription Movement
31
31
  * Moving a circle through walls with sliding.
32
32
  */
33
33
  import type { Box2 } from "./Box2.ts"
34
- import * as Ops from "./Ops.ts"
34
+ import * as Ops from "@typeonce/ritmo/Ops"
35
35
  import type { Segment2 } from "./Segment2.ts"
36
36
  import type { Vec2 } from "./Vec2.ts"
37
37
 
@@ -291,6 +291,22 @@ export const sweepCircleCircle = (
291
291
  return rayCircle(origin.x, origin.y, delta.x, delta.y, target.x, target.y, r)
292
292
  }
293
293
 
294
+ /**
295
+ * Whether a circle of `radius` at `center` overlaps (or touches) a circle of
296
+ * `otherRadius` at `other`, without a square root. Touching counts, as in
297
+ * `Box2.overlapsCircle` and `Segment2.overlapsCircle`. For a pickup or a hit
298
+ * between bodies that have already moved; when one moves fast enough to pass
299
+ * through the other in one step, use {@link sweepCircleCircle}.
300
+ *
301
+ * @category Queries
302
+ */
303
+ export const overlapsCircleCircle = (center: Vec2, radius: number, other: Vec2, otherRadius: number): boolean => {
304
+ const r = radius + otherRadius
305
+ const dx = center.x - other.x
306
+ const dy = center.y - other.y
307
+ return dx * dx + dy * dy <= r * r
308
+ }
309
+
294
310
  /**
295
311
  * Pushes a circle out of every wall it overlaps, boxes first and then
296
312
  * segments, each in order, leaving `skin` between it and each wall. Returns
package/src/FlowField.ts CHANGED
@@ -14,7 +14,9 @@
14
14
  *
15
15
  * @example
16
16
  * ```ts
17
- * const paths = FlowField.make(cellSize)
17
+ * const made = FlowField.make(cellSize)
18
+ * if (!made.ok) return made
19
+ * const paths = made.value
18
20
  * // Each step:
19
21
  * paths.setWalls(walls, bodyRadius)
20
22
  * paths.setGoal(player)
@@ -30,9 +32,9 @@
30
32
  * @categoryDescription Construction
31
33
  * Making a flow field.
32
34
  */
35
+ import * as Ops from "@typeonce/ritmo/Ops"
36
+ import * as Result from "@typeonce/ritmo/Result"
33
37
  import { type Box2, overlapsCircle } from "./Box2.ts"
34
- import * as Ops from "./Ops.ts"
35
- import type * as Scalar from "./Scalar.ts"
36
38
  import type { Vec2 } from "./Vec2.ts"
37
39
 
38
40
  /**
@@ -41,8 +43,8 @@ import type { Vec2 } from "./Vec2.ts"
41
43
  * @category Field Types
42
44
  */
43
45
  export interface FlowField {
44
- /** The side of one square cell, in world units. */
45
- readonly cellSize: Scalar.Positive
46
+ /** The side of one square cell, in world units: finite and above 0. */
47
+ readonly cellSize: number
46
48
  /**
47
49
  * Takes `boxes` as the walls, blocking every cell whose center is within
48
50
  * `clearance` of one, over the area the walls span. Does nothing when the
@@ -72,7 +74,7 @@ const stepY = Int8Array.of(0, 0, 1, -1, 1, -1, 1, -1)
72
74
  const stepCost = Float64Array.of(1, 1, 1, 1, Math.SQRT2, Math.SQRT2, Math.SQRT2, Math.SQRT2)
73
75
 
74
76
  class Field implements FlowField {
75
- readonly cellSize: Scalar.Positive
77
+ readonly cellSize: number
76
78
  private columns = 0
77
79
  private rows = 0
78
80
  private originX = 0
@@ -90,7 +92,7 @@ class Field implements FlowField {
90
92
  private keys = new Float64Array(0)
91
93
  private queued = 0
92
94
 
93
- constructor(cellSize: Scalar.Positive) {
95
+ constructor(cellSize: number) {
94
96
  this.cellSize = cellSize
95
97
  }
96
98
 
@@ -269,11 +271,33 @@ class Field implements FlowField {
269
271
  }
270
272
  }
271
273
 
274
+ /**
275
+ * Why {@link make} made no flow field: `value`, the cell size it was given,
276
+ * is not a finite number above 0.
277
+ *
278
+ * @category Construction
279
+ */
280
+ export interface InvalidCellSize {
281
+ readonly _tag: "InvalidCellSize"
282
+ readonly value: number
283
+ }
284
+
272
285
  /**
273
286
  * An empty flow field (no walls, no goal) with square cells of `cellSize`
274
- * world units. Smaller cells follow walls more closely and cost more to
275
- * search; around a body's diameter is a good start.
287
+ * world units, or `InvalidCellSize` when `cellSize` is not a finite number
288
+ * above 0. Smaller cells follow walls more closely and cost more to search;
289
+ * around a body's diameter is a good start. Make it once (in a service or a
290
+ * system's setup) and reuse it every step.
291
+ *
292
+ * @example
293
+ * ```ts
294
+ * const paths = FlowField.make(30)
295
+ * if (!paths.ok) throw new Error(`bad cell size ${paths.error.value}`)
296
+ * ```
276
297
  *
277
298
  * @category Construction
278
299
  */
279
- export const make = (cellSize: Scalar.Positive): FlowField => new Field(cellSize)
300
+ export const make = (cellSize: number): Result.Result<FlowField, InvalidCellSize> =>
301
+ cellSize > 0 && cellSize < Infinity
302
+ ? Result.success(new Field(cellSize))
303
+ : Result.failure({ _tag: "InvalidCellSize", value: cellSize })
package/src/Scalar.ts CHANGED
@@ -1,190 +1,68 @@
1
1
  /**
2
- * Validated scalar brands and pure scalar helpers.
2
+ * One-dimensional helpers on plain numbers, for values a system moves toward
3
+ * a target every step: a speed, a zoom, a meter.
3
4
  *
4
- * This module is the numeric foundation for the branded geometry helpers.
5
- * Plain `number` values enter through explicit constructors such as
6
- * {@link Finite} and {@link NonNegative}. Once validated, downstream helpers
7
- * operate only on branded values.
8
- *
9
- * In game code this is useful whenever "just a number" is too weak: viewport
10
- * sizes, speeds, distances, durations, and geometry values often need explicit
11
- * non-negative or finite guarantees before they should be stored in the world.
5
+ * Every function is total over finite numbers and returns a plain `number`.
6
+ * Clamping stays `Math.min(Math.max(value, min), max)`, which is exact on
7
+ * every engine. Angles have their own module, `Angle`, because they wrap;
8
+ * vectors use the matching `Vec2` helpers.
12
9
  *
13
10
  * @example
14
11
  * ```ts
15
- * // Validate raw numbers before geometry or gameplay code relies on them.
16
- * const speed = Scalar.Finite.result(12)
17
- * const min = Scalar.Finite.result(0)
18
- * const max = Scalar.Finite.result(8)
19
- * if (!speed.ok || !min.ok || !max.ok) return
20
- *
21
- * // Operate on the branded values after the boundary has been crossed.
22
- * const clamped = Scalar.clamp(speed.value, min.value, max.value)
12
+ * // Accelerate toward the top speed by at most `acceleration * dt` per step.
13
+ * const speed = Scalar.moveToward(current, topSpeed, acceleration * dt)
14
+ * // Ease a zoom toward its target the same way at any step rate.
15
+ * const zoom = Scalar.decayToward(view.zoom, target, 8, dt)
23
16
  * ```
24
17
  *
25
18
  * @module Scalar
26
- * @docGroup data-structures
27
- *
28
- * @categoryDescription Scalar Types
29
- * Branded numeric shapes and shared scalar validation errors.
30
- *
31
- * @categoryDescription Constructors
32
- * Non-throwing validation boundaries for finite and constrained scalar values.
19
+ * @docGroup helpers
33
20
  *
34
21
  * @categoryDescription Operations
35
- * Pure scalar combinators that preserve branding after validation.
36
- */
37
- import type * as Brand from "@typeonce/ritmo/Brand"
38
- import * as internal from "./internal/scalar.ts"
39
- import type { StandardSchema } from "@typeonce/ritmo/Descriptor"
40
- import type * as Result from "@typeonce/ritmo/Result"
41
-
42
- /**
43
- * Finite number brand.
44
- *
45
- * Values of this type are guaranteed not to be `NaN`, `Infinity`, or
46
- * `-Infinity` when they come from a constructor (`result`, `option`) or a `codec`. The
47
- * arithmetic helpers here and in `Vector2` keep the brand without
48
- * re-validating, so their results are finite only while they stay within
49
- * `±Number.MAX_VALUE`: game-sized values never come close, but validate
50
- * again with `result` when inputs may be that large.
51
- *
52
- * @category Scalar Types
53
- */
54
- export type Finite = Brand.Branded<number, "Scalar/Finite">
55
- /**
56
- * Finite number brand constrained to `>= 0`.
57
- *
58
- * @category Scalar Types
59
- */
60
- export type NonNegative = Brand.Branded<number, "Scalar/NonNegative">
61
- /**
62
- * Finite number brand constrained to `> 0`.
63
- *
64
- * @category Scalar Types
22
+ * Moving one number toward another.
65
23
  */
66
- export type Positive = Brand.Branded<number, "Scalar/Positive">
24
+ import * as Ops from "@typeonce/ritmo/Ops"
67
25
 
68
26
  /**
69
- * Structured scalar validation failures.
27
+ * Moves `current` toward `target` by at most `maxDelta`, never overshooting:
28
+ * `target` itself once the gap is `maxDelta` or less, and `current` unchanged
29
+ * when `maxDelta` is 0 or less. For a constant rate (acceleration, a meter
30
+ * filling); for an ease that slows near the target use {@link decayToward},
31
+ * for vectors `Vec2.moveToward`, for angles `Angle.turnToward`.
70
32
  *
71
- * @category Scalar Types
72
- */
73
- export type Error =
74
- | { readonly tag: "Scalar/NotFinite"; readonly value: number }
75
- | { readonly tag: "Scalar/Negative"; readonly value: number }
76
- | { readonly tag: "Scalar/NonPositive"; readonly value: number }
77
-
78
- /**
79
- * Structured interpolation failure.
80
- *
81
- * @category Scalar Types
82
- */
83
- export type InterpolationError = {
84
- readonly tag: "Scalar/ZeroRange"
85
- readonly start: number
86
- readonly end: number
87
- }
88
-
89
- /**
90
- * The constructors, guard, and schema of one scalar brand.
91
- *
92
- * @category Constructors
33
+ * @category Operations
93
34
  */
94
- export interface Constructor<Value> {
95
- /** Validates one number and returns an explicit success/failure result. */
96
- readonly result: (raw: number) => Result.Result<Value, Error>
97
- /** Validates one number and returns `null` on failure. */
98
- readonly option: (raw: number) => Value | null
99
- /** Whether any value is a number of this brand. */
100
- readonly is: (raw: unknown) => raw is Value
101
- /** The Standard Schema for `Descriptor.ConstructedComponent(...)`: the saved form is the number itself. */
102
- readonly codec: StandardSchema<number, Value>
35
+ export const moveToward = (current: number, target: number, maxDelta: number): number => {
36
+ if (!(maxDelta > 0)) return current
37
+ const gap = target - current
38
+ if (Math.abs(gap) <= maxDelta) return target
39
+ return gap > 0 ? current + maxDelta : current - maxDelta
103
40
  }
104
41
 
105
42
  /**
106
- * Constructor for {@link Finite}: `result` (explicit failure), `option`
107
- * (`null` on failure), `is` (a type guard), and `codec`, for
108
- * `Descriptor.ConstructedComponent(Scalar.Finite.codec)`.
109
- *
110
- * @category Constructors
111
- */
112
- export const Finite: Constructor<Finite> = internal.Finite
113
- /**
114
- * Constructor for {@link NonNegative}: `result` (explicit failure), `option`
115
- * (`null` on failure), `is` (a type guard), and `codec`, for
116
- * `Descriptor.ConstructedComponent(Scalar.NonNegative.codec)`.
117
- *
118
- * @category Constructors
119
- */
120
- export const NonNegative: Constructor<NonNegative> = internal.NonNegative
121
- /**
122
- * Constructor for {@link Positive}: `result` (explicit failure), `option`
123
- * (`null` on failure), `is` (a type guard), and `codec`, for
124
- * `Descriptor.ConstructedComponent(Scalar.Positive.codec)`.
125
- *
126
- * @category Constructors
127
- */
128
- export const Positive: Constructor<Positive> = internal.Positive
129
-
130
- /**
131
- * Clamps one finite value between two finite bounds.
132
- *
133
- * The result stays branded because valid branded scalar input cannot produce an
134
- * invalid scalar output.
43
+ * The number `amount` of the way from `start` (at 0) to `end` (at 1). The
44
+ * amount is not clamped, so 1.5 goes past `end`. For a fixed blend between
45
+ * two values; to approach a target over time use {@link moveToward} or
46
+ * {@link decayToward}, for vectors `Vec2.lerp`.
135
47
  *
136
48
  * @category Operations
137
49
  */
138
- export const clamp: (value: Finite, min: Finite, max: Finite) => Finite = internal.clamp
139
- /**
140
- * Interpolates between two finite values by one finite amount.
141
- *
142
- * The amount is not clamped. This stays a pure interpolation primitive rather
143
- * than mixing interpolation with policy. The result keeps the `Finite` brand
144
- * without re-validating (see `Finite`).
145
- *
146
- * @category Operations
147
- */
148
- export const lerp: (start: Finite, end: Finite, amount: Finite) => Finite = internal.lerp
149
- /**
150
- * Moves one finite value toward a target by at most one non-negative delta.
151
- *
152
- * Use this for explicit interpolation-style updates where overshooting the
153
- * target should be impossible.
154
- *
155
- * @category Operations
156
- */
157
- export const approach: (current: Finite, target: Finite, maxDelta: NonNegative) => Finite = internal.approach
158
- /**
159
- * Returns the interpolation amount of one value within a finite input range.
160
- *
161
- * Fails explicitly when the input range has zero size.
162
- *
163
- * @category Operations
164
- */
165
- export const inverseLerp: (
166
- start: Finite,
167
- end: Finite,
168
- value: Finite
169
- ) => Result.Result<Finite, InterpolationError> = internal.inverseLerp
170
- /**
171
- * Remaps one finite value from one input range to one output range.
172
- *
173
- * Fails explicitly when the input range has zero size.
174
- *
175
- * @category Operations
176
- */
177
- export const remap: (
178
- value: Finite,
179
- inputStart: Finite,
180
- inputEnd: Finite,
181
- outputStart: Finite,
182
- outputEnd: Finite
183
- ) => Result.Result<Finite, InterpolationError> = internal.remap
50
+ export const lerp = (start: number, end: number, amount: number): number => start + (end - start) * amount
184
51
 
185
52
  /**
186
- * Common result shape returned by the scalar constructors.
53
+ * Moves `current` toward `target` by the share `rate` closes per second,
54
+ * over `dt` seconds: `target + (current - target) * exp(-rate * dt)`. The
55
+ * same total time gives the same result at any step rate, which `lerp` by a
56
+ * fixed amount per step does not. `current` unchanged when `rate * dt` is 0
57
+ * or less, `target` itself when `rate` is `Infinity` (and `dt` positive).
58
+ * It nears `target` without reaching it; when it must arrive, use
59
+ * {@link moveToward}. For vectors use `Vec2.decayToward`.
187
60
  *
188
- * @category Scalar Types
61
+ * @category Operations
189
62
  */
190
- export type FiniteResult = Result.Result<Finite, Error>
63
+ export const decayToward = (current: number, target: number, rate: number, dt: number): number => {
64
+ const k = rate * dt
65
+ if (!(k > 0)) return current
66
+ if (k === Infinity) return target
67
+ return target + (current - target) * Ops.exp(-k)
68
+ }