@typeonce/ritmo-math 0.14.0 → 0.16.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 (112) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/README.md +17 -12
  3. package/dist/Angle.d.ts +49 -0
  4. package/dist/Angle.d.ts.map +1 -0
  5. package/dist/Angle.js +67 -0
  6. package/dist/Angle.js.map +1 -0
  7. package/dist/Box2.d.ts +20 -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 +23 -1
  12. package/dist/Collide2.d.ts.map +1 -1
  13. package/dist/Collide2.js +23 -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 +22 -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 +54 -51
  22. package/dist/Scalar.js.map +1 -1
  23. package/dist/Segment2.d.ts +3 -0
  24. package/dist/Segment2.d.ts.map +1 -1
  25. package/dist/Segment2.js.map +1 -1
  26. package/dist/Vec2.d.ts +50 -7
  27. package/dist/Vec2.d.ts.map +1 -1
  28. package/dist/Vec2.js +59 -8
  29. package/dist/Vec2.js.map +1 -1
  30. package/dist/index.d.ts +9 -22
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +9 -22
  33. package/dist/index.js.map +1 -1
  34. package/package.json +3 -3
  35. package/src/Angle.ts +65 -0
  36. package/src/Box2.ts +24 -4
  37. package/src/Collide2.ts +30 -2
  38. package/src/FlowField.ts +38 -10
  39. package/src/Scalar.ts +46 -165
  40. package/src/Segment2.ts +3 -0
  41. package/src/Vec2.ts +61 -8
  42. package/src/index.ts +9 -22
  43. package/dist/Aabb.d.ts +0 -182
  44. package/dist/Aabb.d.ts.map +0 -1
  45. package/dist/Aabb.js +0 -109
  46. package/dist/Aabb.js.map +0 -1
  47. package/dist/InputAxis.d.ts +0 -44
  48. package/dist/InputAxis.d.ts.map +0 -1
  49. package/dist/InputAxis.js +0 -71
  50. package/dist/InputAxis.js.map +0 -1
  51. package/dist/Ops.d.ts +0 -209
  52. package/dist/Ops.d.ts.map +0 -1
  53. package/dist/Ops.js +0 -210
  54. package/dist/Ops.js.map +0 -1
  55. package/dist/Random.d.ts +0 -124
  56. package/dist/Random.d.ts.map +0 -1
  57. package/dist/Random.js +0 -126
  58. package/dist/Random.js.map +0 -1
  59. package/dist/Size2.d.ts +0 -118
  60. package/dist/Size2.d.ts.map +0 -1
  61. package/dist/Size2.js +0 -54
  62. package/dist/Size2.js.map +0 -1
  63. package/dist/SpatialGrid.d.ts +0 -68
  64. package/dist/SpatialGrid.d.ts.map +0 -1
  65. package/dist/SpatialGrid.js +0 -112
  66. package/dist/SpatialGrid.js.map +0 -1
  67. package/dist/Vector2.d.ts +0 -214
  68. package/dist/Vector2.d.ts.map +0 -1
  69. package/dist/Vector2.js +0 -134
  70. package/dist/Vector2.js.map +0 -1
  71. package/dist/internal/aabb.d.ts +0 -27
  72. package/dist/internal/aabb.d.ts.map +0 -1
  73. package/dist/internal/aabb.js +0 -52
  74. package/dist/internal/aabb.js.map +0 -1
  75. package/dist/internal/codec.d.ts +0 -9
  76. package/dist/internal/codec.d.ts.map +0 -1
  77. package/dist/internal/codec.js +0 -16
  78. package/dist/internal/codec.js.map +0 -1
  79. package/dist/internal/fdlibm.d.ts +0 -43
  80. package/dist/internal/fdlibm.d.ts.map +0 -1
  81. package/dist/internal/fdlibm.js +0 -1291
  82. package/dist/internal/fdlibm.js.map +0 -1
  83. package/dist/internal/inputAxis.d.ts +0 -11
  84. package/dist/internal/inputAxis.d.ts.map +0 -1
  85. package/dist/internal/inputAxis.js +0 -13
  86. package/dist/internal/inputAxis.js.map +0 -1
  87. package/dist/internal/scalar.d.ts +0 -11
  88. package/dist/internal/scalar.d.ts.map +0 -1
  89. package/dist/internal/scalar.js +0 -68
  90. package/dist/internal/scalar.js.map +0 -1
  91. package/dist/internal/size2.d.ts +0 -22
  92. package/dist/internal/size2.d.ts.map +0 -1
  93. package/dist/internal/size2.js +0 -38
  94. package/dist/internal/size2.js.map +0 -1
  95. package/dist/internal/vector2.d.ts +0 -39
  96. package/dist/internal/vector2.d.ts.map +0 -1
  97. package/dist/internal/vector2.js +0 -78
  98. package/dist/internal/vector2.js.map +0 -1
  99. package/src/Aabb.ts +0 -180
  100. package/src/InputAxis.ts +0 -81
  101. package/src/Ops.ts +0 -232
  102. package/src/Random.ts +0 -225
  103. package/src/Size2.ts +0 -122
  104. package/src/SpatialGrid.ts +0 -172
  105. package/src/Vector2.ts +0 -216
  106. package/src/internal/aabb.ts +0 -81
  107. package/src/internal/codec.ts +0 -21
  108. package/src/internal/fdlibm.ts +0 -1256
  109. package/src/internal/inputAxis.ts +0 -21
  110. package/src/internal/scalar.ts +0 -104
  111. package/src/internal/size2.ts +0 -54
  112. package/src/internal/vector2.ts +0 -121
package/src/Scalar.ts CHANGED
@@ -1,190 +1,71 @@
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
+ * declare const current: number, topSpeed: number, acceleration: number, target: number, dt: number
13
+ * declare const view: { readonly zoom: number }
14
+ * // ---cut---
15
+ * // Accelerate toward the top speed by at most `acceleration * dt` per step.
16
+ * const speed = Scalar.moveToward(current, topSpeed, acceleration * dt)
17
+ * // Ease a zoom toward its target the same way at any step rate.
18
+ * const zoom = Scalar.decayToward(view.zoom, target, 8, dt)
23
19
  * ```
24
20
  *
25
21
  * @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.
22
+ * @docGroup helpers
33
23
  *
34
24
  * @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
25
+ * Moving one number toward another.
65
26
  */
66
- export type Positive = Brand.Branded<number, "Scalar/Positive">
27
+ import * as Ops from "@typeonce/ritmo/Ops"
67
28
 
68
29
  /**
69
- * Structured scalar validation failures.
30
+ * Moves `current` toward `target` by at most `maxDelta`, never overshooting:
31
+ * `target` itself once the gap is `maxDelta` or less, and `current` unchanged
32
+ * when `maxDelta` is 0 or less. For a constant rate (acceleration, a meter
33
+ * filling); for an ease that slows near the target use {@link decayToward},
34
+ * for vectors `Vec2.moveToward`, for angles `Angle.turnToward`.
70
35
  *
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
36
+ * @category Operations
93
37
  */
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>
38
+ export const moveToward = (current: number, target: number, maxDelta: number): number => {
39
+ if (!(maxDelta > 0)) return current
40
+ const gap = target - current
41
+ if (Math.abs(gap) <= maxDelta) return target
42
+ return gap > 0 ? current + maxDelta : current - maxDelta
103
43
  }
104
44
 
105
45
  /**
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.
46
+ * The number `amount` of the way from `start` (at 0) to `end` (at 1). The
47
+ * amount is not clamped, so 1.5 goes past `end`. For a fixed blend between
48
+ * two values; to approach a target over time use {@link moveToward} or
49
+ * {@link decayToward}, for vectors `Vec2.lerp`.
135
50
  *
136
51
  * @category Operations
137
52
  */
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
53
+ export const lerp = (start: number, end: number, amount: number): number => start + (end - start) * amount
184
54
 
185
55
  /**
186
- * Common result shape returned by the scalar constructors.
56
+ * Moves `current` toward `target` by the share `rate` closes per second,
57
+ * over `dt` seconds: `target + (current - target) * exp(-rate * dt)`. The
58
+ * same total time gives the same result at any step rate, which `lerp` by a
59
+ * fixed amount per step does not. `current` unchanged when `rate * dt` is 0
60
+ * or less, `target` itself when `rate` is `Infinity` (and `dt` positive).
61
+ * It nears `target` without reaching it; when it must arrive, use
62
+ * {@link moveToward}. For vectors use `Vec2.decayToward`.
187
63
  *
188
- * @category Scalar Types
64
+ * @category Operations
189
65
  */
190
- export type FiniteResult = Result.Result<Finite, Error>
66
+ export const decayToward = (current: number, target: number, rate: number, dt: number): number => {
67
+ const k = rate * dt
68
+ if (!(k > 0)) return current
69
+ if (k === Infinity) return target
70
+ return target + (current - target) * Ops.exp(-k)
71
+ }
package/src/Segment2.ts CHANGED
@@ -9,6 +9,9 @@
9
9
  *
10
10
  * @example
11
11
  * ```ts
12
+ * declare const position: Vec2.Vec2, delta: Vec2.Vec2
13
+ * declare const radius: number
14
+ * // ---cut---
12
15
  * const fence = Segment2.between({ x: 0, y: 0 }, { x: 40, y: 30 })
13
16
  * const moved = Collide2.moveCircle(position, delta, radius, { segments: [fence] })
14
17
  * ```
package/src/Vec2.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  /**
2
2
  * Plain two-dimensional vector arithmetic for hot paths.
3
3
  *
4
- * `Vector2` validates values at a boundary (authored data, saves, components);
5
- * `Vec2` is the arithmetic that runs every frame on numbers already known to
6
- * be sane. Values are immutable `{ x, y }` records and every helper returns a
7
- * new one. A branded `Vector2` is a `Vec2`, so validated values flow into
8
- * these helpers without conversion.
4
+ * Values are immutable `{ x, y }` records and every helper returns a new
5
+ * one, so a component's value (validated once by the game's schema when it
6
+ * entered the world) passes straight in. Lengths and angles go through
7
+ * `Ops`, so the results replay the same on every engine.
9
8
  *
10
9
  * @example
11
10
  * ```ts
11
+ * declare const position: Vec2.Vec2, target: Vec2.Vec2, current: Vec2.Vec2
12
+ * declare const speed: number, maxSpeed: number
13
+ * // ---cut---
12
14
  * // Steer toward a target at a capped speed.
13
15
  * const wanted = Vec2.scale(Vec2.normalizeOr(Vec2.subtract(target, position), Vec2.zero), speed)
14
16
  * const velocity = Vec2.clampLength(Vec2.add(current, wanted), maxSpeed)
@@ -24,7 +26,7 @@
24
26
  * Immutable arithmetic, measurement, and steering helpers.
25
27
  */
26
28
 
27
- import * as Ops from "./Ops.ts"
29
+ import * as Ops from "@typeonce/ritmo/Ops"
28
30
 
29
31
  /**
30
32
  * A two-dimensional vector in world units.
@@ -65,7 +67,8 @@ export const subtract = (a: Vec2, b: Vec2): Vec2 => ({ x: a.x - b.x, y: a.y - b.
65
67
  export const scale = (a: Vec2, factor: number): Vec2 => ({ x: a.x * factor, y: a.y * factor })
66
68
 
67
69
  /**
68
- * `a` turned by `angle` radians (counter-clockwise for a y-up frame).
70
+ * `a` turned by `angle` radians (counter-clockwise for a y-up frame). For a
71
+ * quarter turn use {@link perpendicular}, which is exact.
69
72
  *
70
73
  * @category Operations
71
74
  */
@@ -76,12 +79,43 @@ export const rotate = (a: Vec2, angle: number): Vec2 => {
76
79
  }
77
80
 
78
81
  /**
79
- * Dot product.
82
+ * `a` turned a quarter turn counter-clockwise (for a y-up frame):
83
+ * `{ x: -a.y, y: a.x }`. Exact. For the normal of a wall or a direction, a
84
+ * sideways push, and the signed side of a point: `dot(perpendicular(forward),
85
+ * offset)` is positive on the left of `forward`, negative on its right.
86
+ *
87
+ * @category Operations
88
+ */
89
+ export const perpendicular = (a: Vec2): Vec2 => ({ x: -a.y, y: a.x })
90
+
91
+ /**
92
+ * Dot product. With {@link perpendicular} it is also the 2D cross product:
93
+ * `dot(perpendicular(a), b)`.
80
94
  *
81
95
  * @category Operations
82
96
  */
83
97
  export const dot = (a: Vec2, b: Vec2): number => a.x * b.x + a.y * b.y
84
98
 
99
+ /**
100
+ * The unit vector at `angle` radians from the x axis (counter-clockwise for a
101
+ * y-up frame), through `Ops.cos` and `Ops.sin`. Multiply by a length with
102
+ * {@link scale}. The inverse of {@link angle}.
103
+ *
104
+ * @category Operations
105
+ */
106
+ export const fromAngle = (angle: number): Vec2 => ({ x: Ops.cos(angle), y: Ops.sin(angle) })
107
+
108
+ /**
109
+ * The angle of `a` from the x axis in radians, in (-π, π], through
110
+ * `Ops.atan2` (so no `y, x` argument order to get wrong). The zero vector
111
+ * gives 0, and so does `-0` in either coordinate where `Ops.atan2` would give
112
+ * `-0` or flip to -π. To keep it in `Angle`'s range [-π, π), use
113
+ * `Angle.wrap`; to turn toward it, `Angle.turnToward`.
114
+ *
115
+ * @category Operations
116
+ */
117
+ export const angle = (a: Vec2): number => Ops.atan2(a.y + 0, a.x + 0)
118
+
85
119
  /**
86
120
  * Length of `a`.
87
121
  *
@@ -155,3 +189,22 @@ export const clampLength = (a: Vec2, max: number): Vec2 => {
155
189
  const len = Ops.hypot(a.x, a.y)
156
190
  return len <= max ? a : { x: (a.x / len) * max, y: (a.y / len) * max }
157
191
  }
192
+
193
+ /**
194
+ * Moves `current` toward `target` by the share `rate` closes per second, over
195
+ * `dt` seconds, with one shared factor `exp(-rate * dt)` so the point moves
196
+ * along the straight line: the same total time gives the same result at any
197
+ * step rate. `current` unchanged when `rate * dt` is 0 or less, `target`
198
+ * itself when `rate` is `Infinity` (and `dt` positive). It nears `target`
199
+ * without reaching it; when it must arrive, use {@link moveToward}. For
200
+ * plain numbers use `Scalar.decayToward`.
201
+ *
202
+ * @category Operations
203
+ */
204
+ export const decayToward = (current: Vec2, target: Vec2, rate: number, dt: number): Vec2 => {
205
+ const k = rate * dt
206
+ if (!(k > 0)) return current
207
+ if (k === Infinity) return target
208
+ const factor = Ops.exp(-k)
209
+ return { x: target.x + (current.x - target.x) * factor, y: target.y + (current.y - target.y) * factor }
210
+ }
package/src/index.ts CHANGED
@@ -1,31 +1,18 @@
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"
package/dist/Aabb.d.ts DELETED
@@ -1,182 +0,0 @@
1
- /**
2
- * Branded axis-aligned bounding boxes built from validated vectors and sizes.
3
- *
4
- * The public shape is centered: each AABB is described by a center position and
5
- * a non-negative size. Geometry helpers derive edges and overlap relationships
6
- * without mutating the original value.
7
- *
8
- * In game code this is the natural helper for collision volumes, trigger
9
- * regions, camera bounds, and spatial authored data that should stay validated
10
- * and immutable after construction. For the per-frame collision tests of
11
- * `Collide2` and `FlowField`, build a plain `Box2` (edge form) from it once.
12
- *
13
- * @example
14
- * ```ts
15
- * // Build a collision volume from raw authored data.
16
- * const player = Aabb.result({
17
- * position: { x: 0, y: 0 },
18
- * size: { width: 16, height: 16 }
19
- * })
20
- * ```
21
- *
22
- * @module Aabb
23
- * @docGroup data-structures
24
- *
25
- * @categoryDescription AABB Types
26
- * Raw, branded, and error shapes for centered axis-aligned bounding boxes.
27
- *
28
- * @categoryDescription Construction
29
- * Non-throwing constructor boundaries for converting raw position and size input into branded AABBs.
30
- *
31
- * @categoryDescription Accessors
32
- * Pure readers that expose center, size, and derived edge positions.
33
- *
34
- * @categoryDescription Operations
35
- * Immutable geometry helpers for translation and overlap checks.
36
- */
37
- import type * as Brand from "@typeonce/ritmo/Brand";
38
- import type { StandardSchema } from "@typeonce/ritmo/Descriptor";
39
- import type * as Result from "@typeonce/ritmo/Result";
40
- import type * as Scalar from "./Scalar.ts";
41
- import type * as Size2 from "./Size2.ts";
42
- import type * as Vector2 from "./Vector2.ts";
43
- /**
44
- * Raw AABB input accepted by the public constructors.
45
- *
46
- * @category AABB Types
47
- */
48
- export type Raw = {
49
- readonly position: Vector2.Raw;
50
- readonly size: Size2.Raw;
51
- };
52
- /**
53
- * Validated centered axis-aligned bounding box.
54
- *
55
- * @category AABB Types
56
- */
57
- export type Aabb = Brand.Branded<{
58
- readonly position: Vector2.Vector2;
59
- readonly size: Size2.Size2;
60
- }, "Aabb">;
61
- /**
62
- * Structured AABB-construction failure.
63
- *
64
- * @category AABB Types
65
- */
66
- export type Error = {
67
- readonly tag: "Aabb/InvalidPosition";
68
- readonly error: Vector2.Error;
69
- readonly input: Raw;
70
- } | {
71
- readonly tag: "Aabb/InvalidSize";
72
- readonly error: Size2.Error;
73
- readonly input: Raw;
74
- };
75
- /**
76
- * Validates one raw AABB and returns an explicit result.
77
- *
78
- * @example
79
- * ```ts
80
- * const playerBox = Aabb.result({
81
- * position: { x: 24, y: 24 },
82
- * size: { width: 16, height: 16 }
83
- * })
84
- * ```
85
- *
86
- * @category Construction
87
- */
88
- export declare const result: (raw: Raw) => Result.Result<Aabb, Error>;
89
- /**
90
- * The codec of `Aabb` (a Standard Schema, see `Descriptor.Codec`): give it to
91
- * `Descriptor.ConstructedComponent(...)` so raw input and saves are validated.
92
- * Its saved form is the raw shape, so it needs no `encode`. To put a `Aabb`
93
- * inside your own object schema, use {@link is} with your library's guard
94
- * schema (`Schema.declare`, `z.custom`).
95
- *
96
- * @example
97
- * ```ts
98
- * const Position = Descriptor.ConstructedComponent(Aabb.codec)("Game/Position")
99
- * ```
100
- *
101
- * @category Construction
102
- */
103
- export declare const codec: StandardSchema<Raw, Aabb>;
104
- /**
105
- * Validates one raw AABB and returns `null` on failure.
106
- *
107
- * @category Construction
108
- */
109
- export declare const option: (raw: Raw) => Aabb | null;
110
- /**
111
- * Whether any value is a valid `Aabb`: a guard, also for building a
112
- * `Aabb` field in your own schema (`Schema.declare(Aabb.is)`).
113
- *
114
- * @category Construction
115
- */
116
- export declare const is: (raw: unknown) => raw is Aabb;
117
- /**
118
- * Reads the center position of one AABB.
119
- *
120
- * @category Accessors
121
- */
122
- export declare const position: (aabb: Aabb) => Vector2.Vector2;
123
- /**
124
- * Reads the size of one AABB.
125
- *
126
- * @category Accessors
127
- */
128
- export declare const size: (aabb: Aabb) => Size2.Size2;
129
- /**
130
- * Returns the left edge of one centered AABB.
131
- *
132
- * @category Accessors
133
- */
134
- export declare const left: (aabb: Aabb) => Scalar.Finite;
135
- /**
136
- * Returns the right edge of one centered AABB.
137
- *
138
- * @category Accessors
139
- */
140
- export declare const right: (aabb: Aabb) => Scalar.Finite;
141
- /**
142
- * Returns the top edge of one centered AABB.
143
- *
144
- * @category Accessors
145
- */
146
- export declare const top: (aabb: Aabb) => Scalar.Finite;
147
- /**
148
- * Returns the bottom edge of one centered AABB.
149
- *
150
- * @category Accessors
151
- */
152
- export declare const bottom: (aabb: Aabb) => Scalar.Finite;
153
- /**
154
- * Checks whether two AABBs overlap on both axes.
155
- *
156
- * @example
157
- * ```ts
158
- * const overlaps = Aabb.intersects(playerBox, wallBox)
159
- * ```
160
- *
161
- * @category Operations
162
- */
163
- export declare const intersects: (first: Aabb, second: Aabb) => boolean;
164
- /**
165
- * Checks whether two AABBs overlap on the horizontal axis.
166
- *
167
- * @category Operations
168
- */
169
- export declare const overlapsHorizontally: (first: Aabb, second: Aabb) => boolean;
170
- /**
171
- * Checks whether two AABBs overlap on the vertical axis.
172
- *
173
- * @category Operations
174
- */
175
- export declare const overlapsVertically: (first: Aabb, second: Aabb) => boolean;
176
- /**
177
- * Returns a translated copy of one AABB.
178
- *
179
- * @category Operations
180
- */
181
- export declare const translate: (aabb: Aabb, delta: Vector2.Vector2) => Aabb;
182
- //# sourceMappingURL=Aabb.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Aabb.d.ts","sourceRoot":"","sources":["../src/Aabb.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;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