@holtjs/stage 0.0.0-stage → 0.3.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.
package/dist/nav.d.ts ADDED
@@ -0,0 +1,119 @@
1
+ import { CircleObstacle, Point2 } from "./hit.js";
2
+ export type TileWalkable = (tx: number, ty: number) => boolean;
3
+ export interface CircleGridOptions {
4
+ width: number;
5
+ height: number;
6
+ cellSize: number;
7
+ wallMargin?: number;
8
+ obstacleMargin?: number;
9
+ originX?: number;
10
+ originY?: number;
11
+ }
12
+ /**
13
+ * Bake a binary walkability mask (`1` = walkable, `0` = blocked) from static circle obstacles
14
+ * and optional arena wall margin. Plain `Uint8Array` suitable for reuse across ticks and workers.
15
+ */
16
+ export declare function buildCircleObstacleGrid(obstacles: readonly CircleObstacle[], opts: CircleGridOptions): {
17
+ free: Uint8Array;
18
+ cols: number;
19
+ rows: number;
20
+ cellSize: number;
21
+ originX: number;
22
+ originY: number;
23
+ };
24
+ /** Bake a binary walkability mask (`1` = walkable, `0` = blocked) from any tile predicate. */
25
+ export declare function buildTileWalkGrid(cols: number, rows: number, tileOk: TileWalkable, originTileX?: number, originTileY?: number): Uint8Array;
26
+ /**
27
+ * Compute a 4-neighbor BFS distance field into `dist` (`-1` = unreachable) starting from `(targetX, targetY)`.
28
+ * Zero allocations — operates directly on caller-provided plain TypedArray buffers.
29
+ */
30
+ export declare function computeFlowField(free: Uint8Array, dist: Int16Array, queue: Int32Array, cols: number, rows: number, cellSize: number, targetX: number, targetY: number, originX?: number, originY?: number): void;
31
+ /** Sample straight-line clearance across a flow walkability grid. */
32
+ export declare function isFlowLineClear(free: Uint8Array, cols: number, rows: number, cellSize: number, x0: number, y0: number, x1: number, y1: number, distance?: number, sampleStep?: number, skipSteps?: number, originX?: number, originY?: number): boolean;
33
+ export interface FlowSampleOptions {
34
+ /** Skip flow sampling when already closer than this distance (returns `null` = go straight). */
35
+ minDist?: number;
36
+ /** Straight-line ray sample spacing in world units (default `cellSize * 0.5`). */
37
+ losStep?: number;
38
+ /** Initial ray samples to skip near entity origin (default 2). */
39
+ losSkip?: number;
40
+ /** Optional world-space grid origin X (default 0). */
41
+ originX?: number;
42
+ /** Optional world-space grid origin Y (default 0). */
43
+ originY?: number;
44
+ /** Optional pre-allocated tuple to avoid allocation on hot ticks. */
45
+ out?: [number, number];
46
+ }
47
+ /**
48
+ * Sample the next waypoint around obstacles from `(x, y)` toward `(targetX, targetY)` using `dist`.
49
+ * Returns `null` when the straight line is unobstructed (go directly at target) or no gradient exists;
50
+ * otherwise returns `[wx, wy]` at the center of the best neighboring cell (without diagonal corner clipping,
51
+ * and with 2-cell recovery if pressed inside an obstacle margin).
52
+ */
53
+ export declare function sampleFlowStep(free: Uint8Array, dist: Int16Array, cols: number, rows: number, cellSize: number, x: number, y: number, targetX: number, targetY: number, distance?: number, opts?: FlowSampleOptions): [number, number] | null;
54
+ /** Ergonomic multi-target FlowField manager for games, crowds, and Mods. */
55
+ export declare class FlowFieldGrid {
56
+ #private;
57
+ readonly cols: number;
58
+ readonly rows: number;
59
+ readonly cellSize: number;
60
+ readonly originX: number;
61
+ readonly originY: number;
62
+ readonly free: Uint8Array;
63
+ readonly fields: Int16Array[];
64
+ readonly ready: Uint8Array;
65
+ constructor(opts: {
66
+ cols: number;
67
+ rows: number;
68
+ cellSize: number;
69
+ free: Uint8Array;
70
+ maxTargets?: number;
71
+ originX?: number;
72
+ originY?: number;
73
+ });
74
+ static fromCircleObstacles(obstacles: readonly CircleObstacle[], opts: CircleGridOptions & {
75
+ maxTargets?: number;
76
+ }): FlowFieldGrid;
77
+ static fromTilePredicate(opts: {
78
+ cols: number;
79
+ rows: number;
80
+ tileOk: TileWalkable;
81
+ cellSize?: number;
82
+ originTileX?: number;
83
+ originTileY?: number;
84
+ maxTargets?: number;
85
+ }): FlowFieldGrid;
86
+ reset(): void;
87
+ compute(slot: number, targetX: number, targetY: number): void;
88
+ sample(slot: number, x: number, y: number, targetX: number, targetY: number, distance?: number, opts?: Omit<FlowSampleOptions, 'originX' | 'originY'>): [number, number] | null;
89
+ }
90
+ export interface GridLosOptions {
91
+ /** Coordinate offset added before `Math.floor` (default `0.5` for center-aligned entity tiles). */
92
+ offset?: number;
93
+ /** Sample spacing in tile units (default `0.35`). */
94
+ step?: number;
95
+ /** Distances shorter than this are trivially clear (default `0.05`). */
96
+ minDist?: number;
97
+ }
98
+ /** True when the straight segment from `(x0, y0)` to `(x1, y1)` stays on walkable tiles. */
99
+ export declare function hasGridLineOfSight(tileOk: TileWalkable, x0: number, y0: number, x1: number, y1: number, opts?: GridLosOptions): boolean;
100
+ export interface GridPathOptions {
101
+ /** Max Chebyshev radius from start tile (default 6). */
102
+ maxRadius?: number;
103
+ /** Max BFS queue expansions (default 96). */
104
+ maxExpand?: number;
105
+ /** Coordinate offset added before `Math.floor` (default `0.5`). */
106
+ offset?: number;
107
+ /** LOS sample step for fast-path and string-pulling (default `0.35`). */
108
+ losStep?: number;
109
+ /** Apply greedy LOS string-pulling to smooth jagged grid corners (default `true`). */
110
+ smooth?: boolean;
111
+ }
112
+ /**
113
+ * Deterministic bounded 8-neighbor BFS pathfinder (no diagonal corner clipping) with optional
114
+ * greedy line-of-sight string-pulling. Works with any tile walkability callback (NPCs,
115
+ * patrolling NPCs, Bosses, or Mod creatures). Returns `null` if unreachable within limits.
116
+ */
117
+ export declare function findGridPath(tileOk: TileWalkable, x0: number, y0: number, x1: number, y1: number, opts?: GridPathOptions): Array<[number, number]> | null;
118
+ /** Write normalized seek velocity from `(x, y)` toward `(targetX, targetY)` at `speed` into `out`. */
119
+ export declare function steerToward(x: number, y: number, targetX: number, targetY: number, speed: number, out: Point2): Point2;
package/dist/nav.js ADDED
@@ -0,0 +1,342 @@
1
+ // src/nav.ts
2
+ const FLOW_DIRS = [
3
+ [1, 0],
4
+ [-1, 0],
5
+ [0, 1],
6
+ [0, -1],
7
+ [1, 1],
8
+ [1, -1],
9
+ [-1, 1],
10
+ [-1, -1]
11
+ ];
12
+ const clamp = (v, lo, hi) => v < lo ? lo : v > hi ? hi : v;
13
+ /**
14
+ * Bake a binary walkability mask (`1` = walkable, `0` = blocked) from static circle obstacles
15
+ * and optional arena wall margin. Plain `Uint8Array` suitable for reuse across ticks and workers.
16
+ */
17
+ function buildCircleObstacleGrid(obstacles, opts) {
18
+ const cellSize = Math.max(1e-6, opts.cellSize);
19
+ const cols = Math.max(1, Math.ceil(opts.width / cellSize));
20
+ const rows = Math.max(1, Math.ceil(opts.height / cellSize));
21
+ const wall = opts.wallMargin ?? 0;
22
+ const pad = opts.obstacleMargin ?? 0;
23
+ const ox = opts.originX ?? 0;
24
+ const oy = opts.originY ?? 0;
25
+ const free = new Uint8Array(cols * rows);
26
+ const minX = ox + wall;
27
+ const minY = oy + wall;
28
+ const maxX = ox + opts.width - wall;
29
+ const maxY = oy + opts.height - wall;
30
+ for (let cy = 0; cy < rows; cy++) {
31
+ const y = oy + (cy + .5) * cellSize;
32
+ for (let cx = 0; cx < cols; cx++) {
33
+ const x = ox + (cx + .5) * cellSize;
34
+ if (!(x > minX && y > minY && x < maxX && y < maxY)) continue;
35
+ let ok = true;
36
+ for (let i = 0; i < obstacles.length; i++) {
37
+ const k = obstacles[i];
38
+ const dx = x - k.x;
39
+ const dy = y - k.y;
40
+ const r = k.r + pad;
41
+ if (dx * dx + dy * dy < r * r) {
42
+ ok = false;
43
+ break;
44
+ }
45
+ }
46
+ free[cy * cols + cx] = ok ? 1 : 0;
47
+ }
48
+ }
49
+ return {
50
+ free,
51
+ cols,
52
+ rows,
53
+ cellSize,
54
+ originX: ox,
55
+ originY: oy
56
+ };
57
+ }
58
+ /** Bake a binary walkability mask (`1` = walkable, `0` = blocked) from any tile predicate. */
59
+ function buildTileWalkGrid(cols, rows, tileOk, originTileX = 0, originTileY = 0) {
60
+ const free = new Uint8Array(cols * rows);
61
+ for (let cy = 0; cy < rows; cy++) {
62
+ const ty = originTileY + cy;
63
+ for (let cx = 0; cx < cols; cx++) free[cy * cols + cx] = tileOk(originTileX + cx, ty) ? 1 : 0;
64
+ }
65
+ return free;
66
+ }
67
+ /**
68
+ * Compute a 4-neighbor BFS distance field into `dist` (`-1` = unreachable) starting from `(targetX, targetY)`.
69
+ * Zero allocations — operates directly on caller-provided plain TypedArray buffers.
70
+ */
71
+ function computeFlowField(free, dist, queue, cols, rows, cellSize, targetX, targetY, originX = 0, originY = 0) {
72
+ dist.fill(-1);
73
+ const sx = clamp(Math.floor((targetX - originX) / cellSize), 0, cols - 1);
74
+ const sy = clamp(Math.floor((targetY - originY) / cellSize), 0, rows - 1);
75
+ let head = 0;
76
+ let tail = 0;
77
+ const start = sy * cols + sx;
78
+ dist[start] = 0;
79
+ queue[tail++] = start;
80
+ while (head < tail) {
81
+ const c = queue[head++];
82
+ const cx = c % cols;
83
+ const cy = c / cols | 0;
84
+ const nextDist = dist[c] + 1;
85
+ for (let k = 0; k < 4; k++) {
86
+ const nx = cx + FLOW_DIRS[k][0];
87
+ const ny = cy + FLOW_DIRS[k][1];
88
+ if (nx < 0 || ny < 0 || nx >= cols || ny >= rows) continue;
89
+ const j = ny * cols + nx;
90
+ if (dist[j] >= 0 || !free[j]) continue;
91
+ dist[j] = nextDist;
92
+ queue[tail++] = j;
93
+ }
94
+ }
95
+ }
96
+ /** Sample straight-line clearance across a flow walkability grid. */
97
+ function isFlowLineClear(free, cols, rows, cellSize, x0, y0, x1, y1, distance, sampleStep = 8, skipSteps = 2, originX = 0, originY = 0) {
98
+ const dd = distance ?? Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
99
+ const n = Math.ceil(dd / Math.max(1e-6, sampleStep));
100
+ for (let i = skipSteps; i < n; i++) {
101
+ const k = i / n;
102
+ const lx = clamp(Math.floor((x0 + (x1 - x0) * k - originX) / cellSize), 0, cols - 1);
103
+ if (!free[clamp(Math.floor((y0 + (y1 - y0) * k - originY) / cellSize), 0, rows - 1) * cols + lx]) return false;
104
+ }
105
+ return true;
106
+ }
107
+ /**
108
+ * Sample the next waypoint around obstacles from `(x, y)` toward `(targetX, targetY)` using `dist`.
109
+ * Returns `null` when the straight line is unobstructed (go directly at target) or no gradient exists;
110
+ * otherwise returns `[wx, wy]` at the center of the best neighboring cell (without diagonal corner clipping,
111
+ * and with 2-cell recovery if pressed inside an obstacle margin).
112
+ */
113
+ function sampleFlowStep(free, dist, cols, rows, cellSize, x, y, targetX, targetY, distance, opts) {
114
+ const dd = distance ?? Math.sqrt((targetX - x) * (targetX - x) + (targetY - y) * (targetY - y));
115
+ if (dd < (opts?.minDist ?? 0)) return null;
116
+ const ox = opts?.originX ?? 0;
117
+ const oy = opts?.originY ?? 0;
118
+ if (isFlowLineClear(free, cols, rows, cellSize, x, y, targetX, targetY, dd, opts?.losStep ?? cellSize * .5, opts?.losSkip ?? 2, ox, oy)) return null;
119
+ const cx = clamp(Math.floor((x - ox) / cellSize), 0, cols - 1);
120
+ const cy = clamp(Math.floor((y - oy) / cellSize), 0, rows - 1);
121
+ const here = dist[cy * cols + cx];
122
+ const onObstacle = here < 0;
123
+ let best = onObstacle ? 32767 : here;
124
+ let bx = -1;
125
+ let by = -1;
126
+ for (let k = 0; k < 8; k++) {
127
+ const nx = cx + FLOW_DIRS[k][0];
128
+ const ny = cy + FLOW_DIRS[k][1];
129
+ if (nx < 0 || ny < 0 || nx >= cols || ny >= rows) continue;
130
+ if (k >= 4 && !onObstacle && (!free[cy * cols + nx] || !free[ny * cols + cx])) continue;
131
+ const v = dist[ny * cols + nx];
132
+ if (v >= 0 && v < best) {
133
+ best = v;
134
+ bx = nx;
135
+ by = ny;
136
+ }
137
+ }
138
+ if (onObstacle && bx < 0) {
139
+ const ny0 = Math.max(0, cy - 2);
140
+ const ny1 = Math.min(rows - 1, cy + 2);
141
+ const nx0 = Math.max(0, cx - 2);
142
+ const nx1 = Math.min(cols - 1, cx + 2);
143
+ for (let ny = ny0; ny <= ny1; ny++) for (let nx = nx0; nx <= nx1; nx++) {
144
+ const v = dist[ny * cols + nx];
145
+ if (v >= 0 && v < best) {
146
+ best = v;
147
+ bx = nx;
148
+ by = ny;
149
+ }
150
+ }
151
+ }
152
+ if (bx < 0) return null;
153
+ const wx = ox + (bx + .5) * cellSize;
154
+ const wy = oy + (by + .5) * cellSize;
155
+ if (opts?.out) {
156
+ opts.out[0] = wx;
157
+ opts.out[1] = wy;
158
+ return opts.out;
159
+ }
160
+ return [wx, wy];
161
+ }
162
+ /** Ergonomic multi-target FlowField manager for games, crowds, and Mods. */
163
+ var FlowFieldGrid = class FlowFieldGrid {
164
+ cols;
165
+ rows;
166
+ cellSize;
167
+ originX;
168
+ originY;
169
+ free;
170
+ fields;
171
+ ready;
172
+ #queue;
173
+ constructor(opts) {
174
+ this.cols = opts.cols;
175
+ this.rows = opts.rows;
176
+ this.cellSize = opts.cellSize;
177
+ this.originX = opts.originX ?? 0;
178
+ this.originY = opts.originY ?? 0;
179
+ this.free = opts.free;
180
+ const slots = Math.max(1, opts.maxTargets ?? 1);
181
+ const nCells = this.cols * this.rows;
182
+ this.fields = Array.from({ length: slots }, () => new Int16Array(nCells).fill(-1));
183
+ this.ready = new Uint8Array(slots);
184
+ this.#queue = new Int32Array(nCells);
185
+ }
186
+ static fromCircleObstacles(obstacles, opts) {
187
+ const baked = buildCircleObstacleGrid(obstacles, opts);
188
+ return new FlowFieldGrid({
189
+ ...baked,
190
+ ...opts.maxTargets !== void 0 ? { maxTargets: opts.maxTargets } : {}
191
+ });
192
+ }
193
+ static fromTilePredicate(opts) {
194
+ const cellSize = opts.cellSize ?? 1;
195
+ const ox = (opts.originTileX ?? 0) * cellSize;
196
+ const oy = (opts.originTileY ?? 0) * cellSize;
197
+ const free = buildTileWalkGrid(opts.cols, opts.rows, opts.tileOk, opts.originTileX ?? 0, opts.originTileY ?? 0);
198
+ return new FlowFieldGrid({
199
+ cols: opts.cols,
200
+ rows: opts.rows,
201
+ cellSize,
202
+ free,
203
+ originX: ox,
204
+ originY: oy,
205
+ ...opts.maxTargets !== void 0 ? { maxTargets: opts.maxTargets } : {}
206
+ });
207
+ }
208
+ reset() {
209
+ this.ready.fill(0);
210
+ for (const f of this.fields) f.fill(-1);
211
+ }
212
+ compute(slot, targetX, targetY) {
213
+ if (slot < 0 || slot >= this.fields.length) return;
214
+ computeFlowField(this.free, this.fields[slot], this.#queue, this.cols, this.rows, this.cellSize, targetX, targetY, this.originX, this.originY);
215
+ this.ready[slot] = 1;
216
+ }
217
+ sample(slot, x, y, targetX, targetY, distance, opts) {
218
+ if (slot < 0 || slot >= this.fields.length || !this.ready[slot]) return null;
219
+ return sampleFlowStep(this.free, this.fields[slot], this.cols, this.rows, this.cellSize, x, y, targetX, targetY, distance, {
220
+ ...opts,
221
+ originX: this.originX,
222
+ originY: this.originY
223
+ });
224
+ }
225
+ };
226
+ const tileKey = (tx, ty) => (ty + 32768) * 65536 + (tx + 32768);
227
+ /** True when the straight segment from `(x0, y0)` to `(x1, y1)` stays on walkable tiles. */
228
+ function hasGridLineOfSight(tileOk, x0, y0, x1, y1, opts) {
229
+ const offset = opts?.offset ?? .5;
230
+ const step = Math.max(.05, Math.min(.5, opts?.step ?? .35));
231
+ const minDist = opts?.minDist ?? .05;
232
+ const dist = Math.hypot(x1 - x0, y1 - y0);
233
+ if (dist < minDist) return true;
234
+ const steps = Math.max(2, Math.ceil(dist / step));
235
+ let prevTx = Math.floor(x0 + offset);
236
+ let prevTy = Math.floor(y0 + offset);
237
+ for (let i = 1; i <= steps; i++) {
238
+ const k = i / steps;
239
+ const tx = Math.floor(x0 + (x1 - x0) * k + offset);
240
+ const ty = Math.floor(y0 + (y1 - y0) * k + offset);
241
+ if (!tileOk(tx, ty)) return false;
242
+ if (tx !== prevTx && ty !== prevTy && (!tileOk(tx, prevTy) || !tileOk(prevTx, ty))) return false;
243
+ prevTx = tx;
244
+ prevTy = ty;
245
+ }
246
+ return true;
247
+ }
248
+ /**
249
+ * Deterministic bounded 8-neighbor BFS pathfinder (no diagonal corner clipping) with optional
250
+ * greedy line-of-sight string-pulling. Works with any tile walkability callback (NPCs,
251
+ * patrolling NPCs, Bosses, or Mod creatures). Returns `null` if unreachable within limits.
252
+ */
253
+ function findGridPath(tileOk, x0, y0, x1, y1, opts) {
254
+ const maxRadius = opts?.maxRadius ?? 6;
255
+ const maxExpand = opts?.maxExpand ?? 96;
256
+ const offset = opts?.offset ?? .5;
257
+ const losOpts = {
258
+ offset,
259
+ step: opts?.losStep ?? .35
260
+ };
261
+ if (Math.hypot(x1 - x0, y1 - y0) < .04) return [[x1, y1]];
262
+ if (hasGridLineOfSight(tileOk, x0, y0, x1, y1, losOpts)) return [[x0, y0], [x1, y1]];
263
+ const sx = Math.floor(x0 + offset);
264
+ const sy = Math.floor(y0 + offset);
265
+ const gx = Math.floor(x1 + offset);
266
+ const gy = Math.floor(y1 + offset);
267
+ if (!tileOk(sx, sy) || !tileOk(gx, gy)) return null;
268
+ if (Math.max(Math.abs(gx - sx), Math.abs(gy - sy)) > maxRadius) return null;
269
+ const startKey = tileKey(sx, sy);
270
+ const goalKey = tileKey(gx, gy);
271
+ if (startKey === goalKey) return [[x0, y0], [x1, y1]];
272
+ const qx = [sx];
273
+ const qy = [sy];
274
+ const parent = /* @__PURE__ */ new Map([[startKey, -1]]);
275
+ let head = 0;
276
+ let found = false;
277
+ while (head < qx.length && head < maxExpand) {
278
+ const cx = qx[head];
279
+ const cy = qy[head];
280
+ head++;
281
+ const ck = tileKey(cx, cy);
282
+ for (let i = 0; i < FLOW_DIRS.length; i++) {
283
+ const dx = FLOW_DIRS[i][0];
284
+ const dy = FLOW_DIRS[i][1];
285
+ const nx = cx + dx;
286
+ const ny = cy + dy;
287
+ if (Math.abs(nx - sx) > maxRadius || Math.abs(ny - sy) > maxRadius) continue;
288
+ if (!tileOk(nx, ny)) continue;
289
+ if (dx !== 0 && dy !== 0 && (!tileOk(cx + dx, cy) || !tileOk(cx, cy + dy))) continue;
290
+ const nk = tileKey(nx, ny);
291
+ if (parent.has(nk)) continue;
292
+ parent.set(nk, ck);
293
+ if (nk === goalKey) {
294
+ found = true;
295
+ break;
296
+ }
297
+ qx.push(nx);
298
+ qy.push(ny);
299
+ }
300
+ if (found) break;
301
+ }
302
+ if (!found) return null;
303
+ const raw = [[x1, y1]];
304
+ let cur = parent.get(goalKey) ?? -1;
305
+ while (cur >= 0 && cur !== startKey) {
306
+ const tx = cur % 65536 - 32768;
307
+ const ty = Math.floor(cur / 65536) - 32768;
308
+ raw.push([tx, ty]);
309
+ cur = parent.get(cur) ?? -1;
310
+ }
311
+ raw.push([x0, y0]);
312
+ raw.reverse();
313
+ if (opts?.smooth === false) return raw;
314
+ const smooth = [raw[0]];
315
+ let i = 0;
316
+ while (i < raw.length - 1) {
317
+ let next = i + 1;
318
+ for (let j = raw.length - 1; j > i + 1; j--) if (hasGridLineOfSight(tileOk, raw[i][0], raw[i][1], raw[j][0], raw[j][1], losOpts)) {
319
+ next = j;
320
+ break;
321
+ }
322
+ smooth.push(raw[next]);
323
+ i = next;
324
+ }
325
+ return smooth;
326
+ }
327
+ /** Write normalized seek velocity from `(x, y)` toward `(targetX, targetY)` at `speed` into `out`. */
328
+ function steerToward(x, y, targetX, targetY, speed, out) {
329
+ const dx = targetX - x;
330
+ const dy = targetY - y;
331
+ const d = Math.sqrt(dx * dx + dy * dy);
332
+ if (d < 1e-9) {
333
+ out.x = 0;
334
+ out.y = 0;
335
+ return out;
336
+ }
337
+ const s = speed / d;
338
+ out.x = dx * s;
339
+ out.y = dy * s;
340
+ return out;
341
+ }
342
+ export { FlowFieldGrid, buildCircleObstacleGrid, buildTileWalkGrid, computeFlowField, findGridPath, hasGridLineOfSight, isFlowLineClear, sampleFlowStep, steerToward };
@@ -0,0 +1,219 @@
1
+ export declare const PARTICLE_RECT = 0;
2
+ export declare const PARTICLE_SPARKLE = 1;
3
+ export declare const PARTICLE_BUBBLE = 2;
4
+ export declare const PARTICLE_WAKE = 3;
5
+ export type ParticleShape = typeof PARTICLE_RECT | typeof PARTICLE_SPARKLE | typeof PARTICLE_BUBBLE | typeof PARTICLE_WAKE;
6
+ /** Fade & envelope curves for pooled particles. */
7
+ export declare const FADE_PUNCH = 0;
8
+ export declare const FADE_SINE = 1;
9
+ export declare const FADE_FLICKER = 2;
10
+ export declare const FADE_EASE = 3;
11
+ export declare const FADE_LINEAR = 4;
12
+ export type ParticleFade = typeof FADE_PUNCH | typeof FADE_SINE | typeof FADE_FLICKER | typeof FADE_EASE | typeof FADE_LINEAR;
13
+ export declare const FLAG_SIZE_POP = 1;
14
+ export interface ParticleWorldView {
15
+ readonly x: number;
16
+ readonly y: number;
17
+ readonly right: number;
18
+ readonly bottom: number;
19
+ }
20
+ /** Duck-typed 2D vector drawing surface (matches Stage2D `Graphics` without importing renderer internals). */
21
+ export interface ParticleGraphics {
22
+ fillStyle(color: number, alpha?: number): unknown;
23
+ fillRect(x: number, y: number, width: number, height: number): unknown;
24
+ lineStyle(lineWidth: number, color: number, alpha?: number): unknown;
25
+ strokeCircle(x: number, y: number, radius: number): unknown;
26
+ strokeEllipse(x: number, y: number, width: number, height: number): unknown;
27
+ }
28
+ export interface SpawnParticleOptions {
29
+ x: number;
30
+ y: number;
31
+ z?: number;
32
+ vx?: number;
33
+ vy?: number;
34
+ vz?: number;
35
+ /** Velocity drag factor per second (`v *= max(0, 1 - drag * dt)`). */
36
+ drag?: number;
37
+ /** 2.5D vertical Z gravity (`vz -= gravityZ * dt`, `z = max(0, z + vz * dt)`). */
38
+ gravityZ?: number;
39
+ /** 2D screen-space Y acceleration (`vy += gravityY * dt`, e.g. falling dust). */
40
+ gravityY?: number;
41
+ /** Horizontal sinusoidal wobble amplitude (world px/s). */
42
+ wobbleAmp?: number;
43
+ /** Sinusoidal wobble frequency in rad/s (default ~3.85). */
44
+ wobbleFreq?: number;
45
+ phase?: number;
46
+ /** Lifetime in seconds (> 0). */
47
+ life: number;
48
+ color: number;
49
+ /** Base size / radius in world px. */
50
+ size?: number;
51
+ /** Peak opacity 0..1 (default 1). */
52
+ alpha?: number;
53
+ shape?: ParticleShape;
54
+ fade?: ParticleFade;
55
+ flags?: number;
56
+ /** Optional wake expansion distance (world px added to radius over life, default 6.5). */
57
+ grow?: number;
58
+ }
59
+ export interface BurstOptions {
60
+ count: number;
61
+ speed: number;
62
+ color: number;
63
+ color2?: number;
64
+ size?: number;
65
+ life?: number;
66
+ gravityZ?: number;
67
+ drag?: number;
68
+ zMin?: number;
69
+ zSpan?: number;
70
+ yScale?: number;
71
+ /** Scale count for power-saving / lite modes (default 1). */
72
+ liteScale?: number;
73
+ shape?: ParticleShape;
74
+ }
75
+ export interface SparksOptions {
76
+ count: number;
77
+ color: number;
78
+ speed?: number;
79
+ spread?: number;
80
+ size?: number;
81
+ life?: number;
82
+ gravityZ?: number;
83
+ drag?: number;
84
+ liteScale?: number;
85
+ }
86
+ export interface PuffOptions {
87
+ shape?: typeof PARTICLE_RECT | typeof PARTICLE_WAKE;
88
+ vx?: number;
89
+ vy?: number;
90
+ gravityY?: number;
91
+ life: number;
92
+ color: number;
93
+ size: number;
94
+ alpha?: number;
95
+ grow?: number;
96
+ }
97
+ export interface ParticlePoolOptions {
98
+ capacity: number;
99
+ /** Overwrite oldest slot in ring order when full (default `true`; `false` drops when full). */
100
+ overwriteOldest?: boolean;
101
+ }
102
+ /**
103
+ * Fixed-capacity SoA TypedArray particle pool (zero GC allocations per spawn or frame).
104
+ * Supports 2.5D z-gravity bursts/sparks, 2D ground puffs/wakes, and ambient floating particles.
105
+ */
106
+ export declare class ParticlePool {
107
+ #private;
108
+ readonly capacity: number;
109
+ readonly overwriteOldest: boolean;
110
+ readonly px: Float32Array;
111
+ readonly py: Float32Array;
112
+ readonly pz: Float32Array;
113
+ readonly vx: Float32Array;
114
+ readonly vy: Float32Array;
115
+ readonly vz: Float32Array;
116
+ readonly drag: Float32Array;
117
+ readonly gravZ: Float32Array;
118
+ readonly gravY: Float32Array;
119
+ readonly wobbleAmp: Float32Array;
120
+ readonly wobbleFreq: Float32Array;
121
+ readonly phase: Float32Array;
122
+ readonly life: Float32Array;
123
+ readonly maxLife: Float32Array;
124
+ readonly col: Uint32Array;
125
+ readonly size: Float32Array;
126
+ readonly alpha: Float32Array;
127
+ readonly grow: Float32Array;
128
+ readonly shape: Uint8Array;
129
+ readonly fade: Uint8Array;
130
+ readonly flags: Uint8Array;
131
+ constructor(opts: ParticlePoolOptions | number);
132
+ get activeCount(): number;
133
+ isAnimating(): boolean;
134
+ clear(): void;
135
+ private allocSlot;
136
+ private writeSlot;
137
+ /** Spawn a single particle with full physics/rendering options. Returns slot index or -1 if dropped. */
138
+ spawn(opts: SpawnParticleOptions, activeCap?: number): number;
139
+ /** Radial 2.5D burst of pixel bits (enemy death pop, confetti, crate break, fireworks). */
140
+ burst(x: number, y: number, opts: BurstOptions, activeCap?: number): void;
141
+ /** Directional narrow-fan hit sparks flying away along `angle` (radians). */
142
+ sparks(x: number, y: number, angle: number, opts: SparksOptions, activeCap?: number): void;
143
+ /** Ground footstep dust puff or expanding elliptical water wake. */
144
+ puff(x: number, y: number, opts: PuffOptions, activeCap?: number): number;
145
+ /** Advance all active particles by `dt` seconds. Returns current active count. */
146
+ step(dt: number, nowMs?: number): number;
147
+ }
148
+ /**
149
+ * Render all active particles in `pool` onto any Stage2D-compatible `Graphics` surface.
150
+ * Zero renderer import so lightweight headless or UI bundles never pull WebGL/Canvas2D renderers.
151
+ */
152
+ export declare function drawParticlePool(g: ParticleGraphics, pool: ParticlePool, nowMs?: number, worldView?: ParticleWorldView | null, cullPad?: number): void;
153
+ export type AmbientParticleKind = 'firefly' | 'petal' | 'sparkle' | 'ember' | 'bubble';
154
+ export interface AmbientEmitterSpec {
155
+ readonly x: number;
156
+ readonly y: number;
157
+ readonly radius: number;
158
+ readonly rate: number;
159
+ readonly kind: AmbientParticleKind;
160
+ readonly color?: number;
161
+ readonly duskOnly?: boolean;
162
+ }
163
+ export interface AmbientStepOptions {
164
+ dusk?: number;
165
+ reducedMotion?: boolean;
166
+ /** Scale emitter coordinates & radius into world px (default `16` for 16px tile coordinates; pass `1` for pixel coordinates). */
167
+ coordScale?: number;
168
+ }
169
+ /**
170
+ * Viewport-culled continuous ambient emitter scheduler writing directly into a shared `ParticlePool`.
171
+ * Tracks `visibleActive` so DemandPacer sleeps when no emitter is in view.
172
+ */
173
+ export declare class AmbientEmitterPool {
174
+ #private;
175
+ readonly pool: ParticlePool;
176
+ constructor(pool: ParticlePool);
177
+ get emitters(): readonly AmbientEmitterSpec[];
178
+ get visibleActive(): boolean;
179
+ setEmitters(next: readonly AmbientEmitterSpec[]): void;
180
+ isAnimating(): boolean;
181
+ private emitOne;
182
+ /** Advance emitters (spawning visible particles into `this.pool`) and step `this.pool`. */
183
+ step(dt: number, nowMs: number, worldView?: ParticleWorldView | null, opts?: AmbientStepOptions): boolean;
184
+ }
185
+ export interface JuiceControllerOptions {
186
+ /** Initial shake level: `0` off, `1` low, `2` high. */
187
+ level?: number;
188
+ hitstopMaxMs?: number;
189
+ hitstopGapMs?: number;
190
+ lagMaxMs?: number;
191
+ shakePx?: readonly [number, number, number];
192
+ traumaDecayPerSec?: number;
193
+ }
194
+ /**
195
+ * Pure hit-feel controller combining capped hitstop (with smooth render lag catch-up) and
196
+ * squared-trauma screen shake. Zero DOM, zero renderer coupling — composable by any 2D action game or Mod.
197
+ */
198
+ export declare class JuiceController {
199
+ #private;
200
+ level: number;
201
+ stop: number;
202
+ lag: number;
203
+ lastStop: number;
204
+ trauma: number;
205
+ ox: number;
206
+ oy: number;
207
+ constructor(opts?: JuiceControllerOptions);
208
+ reset(): void;
209
+ setLevel(v: number): void;
210
+ /** Hold world simulation rendering still for `ms` on a heavy blow (rate-limited by `gapMs`). */
211
+ hitstop(ms: number, nowMs: number, gapMs?: number): void;
212
+ /** Add camera trauma in `[0, 1]` (squared to compute pixel shake offset). */
213
+ shake(weight: number): void;
214
+ /**
215
+ * Advance hitstop & screen shake for one frame.
216
+ * Returns effective world particle delta in seconds (`0` while held in hitstop).
217
+ */
218
+ tick(frameMs: number, nowMs: number, paused?: boolean): number;
219
+ }