@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/CHANGELOG.md +62 -0
- package/LICENSE +201 -0
- package/NOTICE +13 -0
- package/README.md +172 -2
- package/dist/audio.d.ts +159 -0
- package/dist/audio.js +781 -0
- package/dist/ecs.d.ts +172 -0
- package/dist/ecs.js +537 -0
- package/dist/flex.d.ts +113 -0
- package/dist/flex.js +426 -0
- package/dist/game-BHDcwovp.js +6725 -0
- package/dist/game-NqFMKsuL.d.ts +1205 -0
- package/dist/headless.d.ts +147 -0
- package/dist/headless.js +327 -0
- package/dist/hit.d.ts +63 -0
- package/dist/hit.js +185 -0
- package/dist/index.d.ts +307 -0
- package/dist/index.js +45 -0
- package/dist/joystick.d.ts +233 -0
- package/dist/joystick.js +763 -0
- package/dist/nav.d.ts +119 -0
- package/dist/nav.js +342 -0
- package/dist/particles.d.ts +219 -0
- package/dist/particles.js +478 -0
- package/dist/spatial.d.ts +70 -0
- package/dist/spatial.js +165 -0
- package/dist/surfaces.d.ts +209 -0
- package/dist/surfaces.js +567 -0
- package/dist/trace.d.ts +143 -0
- package/dist/trace.js +246 -0
- package/dist/tween-D3Ztppuz.d.ts +91 -0
- package/dist/tween.d.ts +2 -0
- package/dist/tween.js +312 -0
- package/dist/types-ChGhuDNX.js +165 -0
- package/llms.txt +40 -0
- package/package.json +107 -4
- package/skills/stage/SKILL.md +282 -0
- package/skills/stage/references/subpaths.md +74 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
// src/particles.ts
|
|
2
|
+
const PARTICLE_RECT = 0;
|
|
3
|
+
const PARTICLE_SPARKLE = 1;
|
|
4
|
+
const PARTICLE_BUBBLE = 2;
|
|
5
|
+
const PARTICLE_WAKE = 3;
|
|
6
|
+
/** Fade & envelope curves for pooled particles. */
|
|
7
|
+
const FADE_PUNCH = 0;
|
|
8
|
+
const FADE_SINE = 1;
|
|
9
|
+
const FADE_FLICKER = 2;
|
|
10
|
+
const FADE_EASE = 3;
|
|
11
|
+
const FADE_LINEAR = 4;
|
|
12
|
+
const FLAG_SIZE_POP = 1;
|
|
13
|
+
/**
|
|
14
|
+
* Fixed-capacity SoA TypedArray particle pool (zero GC allocations per spawn or frame).
|
|
15
|
+
* Supports 2.5D z-gravity bursts/sparks, 2D ground puffs/wakes, and ambient floating particles.
|
|
16
|
+
*/
|
|
17
|
+
var ParticlePool = class {
|
|
18
|
+
capacity;
|
|
19
|
+
overwriteOldest;
|
|
20
|
+
px;
|
|
21
|
+
py;
|
|
22
|
+
pz;
|
|
23
|
+
vx;
|
|
24
|
+
vy;
|
|
25
|
+
vz;
|
|
26
|
+
drag;
|
|
27
|
+
gravZ;
|
|
28
|
+
gravY;
|
|
29
|
+
wobbleAmp;
|
|
30
|
+
wobbleFreq;
|
|
31
|
+
phase;
|
|
32
|
+
life;
|
|
33
|
+
maxLife;
|
|
34
|
+
col;
|
|
35
|
+
size;
|
|
36
|
+
alpha;
|
|
37
|
+
grow;
|
|
38
|
+
shape;
|
|
39
|
+
fade;
|
|
40
|
+
flags;
|
|
41
|
+
#cursor = 0;
|
|
42
|
+
#activeCount = 0;
|
|
43
|
+
constructor(opts) {
|
|
44
|
+
const cfg = typeof opts === "number" ? { capacity: opts } : opts;
|
|
45
|
+
const n = Math.max(1, Math.floor(cfg.capacity));
|
|
46
|
+
this.capacity = n;
|
|
47
|
+
this.overwriteOldest = cfg.overwriteOldest !== false;
|
|
48
|
+
this.px = new Float32Array(n);
|
|
49
|
+
this.py = new Float32Array(n);
|
|
50
|
+
this.pz = new Float32Array(n);
|
|
51
|
+
this.vx = new Float32Array(n);
|
|
52
|
+
this.vy = new Float32Array(n);
|
|
53
|
+
this.vz = new Float32Array(n);
|
|
54
|
+
this.drag = new Float32Array(n);
|
|
55
|
+
this.gravZ = new Float32Array(n);
|
|
56
|
+
this.gravY = new Float32Array(n);
|
|
57
|
+
this.wobbleAmp = new Float32Array(n);
|
|
58
|
+
this.wobbleFreq = new Float32Array(n);
|
|
59
|
+
this.phase = new Float32Array(n);
|
|
60
|
+
this.life = new Float32Array(n);
|
|
61
|
+
this.maxLife = new Float32Array(n);
|
|
62
|
+
this.col = new Uint32Array(n);
|
|
63
|
+
this.size = new Float32Array(n);
|
|
64
|
+
this.alpha = new Float32Array(n);
|
|
65
|
+
this.grow = new Float32Array(n);
|
|
66
|
+
this.shape = new Uint8Array(n);
|
|
67
|
+
this.fade = new Uint8Array(n);
|
|
68
|
+
this.flags = new Uint8Array(n);
|
|
69
|
+
}
|
|
70
|
+
get activeCount() {
|
|
71
|
+
return this.#activeCount;
|
|
72
|
+
}
|
|
73
|
+
isAnimating() {
|
|
74
|
+
return this.#activeCount > 0;
|
|
75
|
+
}
|
|
76
|
+
clear() {
|
|
77
|
+
this.life.fill(0);
|
|
78
|
+
this.#activeCount = 0;
|
|
79
|
+
this.#cursor = 0;
|
|
80
|
+
}
|
|
81
|
+
allocSlot(cap) {
|
|
82
|
+
const limit = Math.max(1, Math.min(this.capacity, Math.floor(cap)));
|
|
83
|
+
if (this.overwriteOldest) {
|
|
84
|
+
const idx = this.#cursor % limit;
|
|
85
|
+
this.#cursor = (idx + 1) % limit;
|
|
86
|
+
return idx;
|
|
87
|
+
}
|
|
88
|
+
for (let i = 0; i < limit; i++) {
|
|
89
|
+
const idx = (this.#cursor + i) % limit;
|
|
90
|
+
if (this.life[idx] <= 0) {
|
|
91
|
+
this.#cursor = (idx + 1) % limit;
|
|
92
|
+
return idx;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return -1;
|
|
96
|
+
}
|
|
97
|
+
writeSlot(activeCap, x, y, z, vx, vy, vz, drag, gravityZ, gravityY, wobbleAmp, wobbleFreq, phase, life, color, size, alpha, grow, shape, fade, flags) {
|
|
98
|
+
if (!(life > 0)) return -1;
|
|
99
|
+
const i = this.allocSlot(activeCap);
|
|
100
|
+
if (i < 0) return -1;
|
|
101
|
+
const wasDead = this.life[i] <= 0;
|
|
102
|
+
this.px[i] = x;
|
|
103
|
+
this.py[i] = y;
|
|
104
|
+
this.pz[i] = z;
|
|
105
|
+
this.vx[i] = vx;
|
|
106
|
+
this.vy[i] = vy;
|
|
107
|
+
this.vz[i] = vz;
|
|
108
|
+
this.drag[i] = drag;
|
|
109
|
+
this.gravZ[i] = gravityZ;
|
|
110
|
+
this.gravY[i] = gravityY;
|
|
111
|
+
this.wobbleAmp[i] = wobbleAmp;
|
|
112
|
+
this.wobbleFreq[i] = wobbleFreq;
|
|
113
|
+
this.phase[i] = phase;
|
|
114
|
+
this.life[i] = life;
|
|
115
|
+
this.maxLife[i] = life;
|
|
116
|
+
this.col[i] = color >>> 0;
|
|
117
|
+
this.size[i] = size;
|
|
118
|
+
this.alpha[i] = alpha;
|
|
119
|
+
this.grow[i] = grow;
|
|
120
|
+
this.shape[i] = shape;
|
|
121
|
+
this.fade[i] = fade;
|
|
122
|
+
this.flags[i] = flags;
|
|
123
|
+
if (wasDead) this.#activeCount++;
|
|
124
|
+
return i;
|
|
125
|
+
}
|
|
126
|
+
/** Spawn a single particle with full physics/rendering options. Returns slot index or -1 if dropped. */
|
|
127
|
+
spawn(opts, activeCap = this.capacity) {
|
|
128
|
+
return this.writeSlot(activeCap, opts.x, opts.y, opts.z ?? 0, opts.vx ?? 0, opts.vy ?? 0, opts.vz ?? 0, opts.drag ?? 0, opts.gravityZ ?? 0, opts.gravityY ?? 0, opts.wobbleAmp ?? 0, opts.wobbleFreq ?? 0, opts.phase ?? 0, opts.life, opts.color, opts.size ?? 2, opts.alpha ?? 1, opts.grow ?? 6.5, opts.shape ?? 0, opts.fade ?? 0, opts.flags ?? 0);
|
|
129
|
+
}
|
|
130
|
+
/** Radial 2.5D burst of pixel bits (enemy death pop, confetti, crate break, fireworks). */
|
|
131
|
+
burst(x, y, opts, activeCap = this.capacity) {
|
|
132
|
+
const scale = opts.liteScale ?? 1;
|
|
133
|
+
const n = Math.max(1, Math.round(opts.count * scale));
|
|
134
|
+
const col2 = opts.color2 ?? opts.color;
|
|
135
|
+
const size = opts.size ?? 2;
|
|
136
|
+
const life = opts.life ?? .35;
|
|
137
|
+
const drag = opts.drag ?? 3;
|
|
138
|
+
const gravityZ = opts.gravityZ ?? 260;
|
|
139
|
+
const zMin = opts.zMin ?? 40;
|
|
140
|
+
const zSpan = opts.zSpan ?? 70;
|
|
141
|
+
const yScale = opts.yScale ?? .7;
|
|
142
|
+
const shape = opts.shape ?? 0;
|
|
143
|
+
for (let k = 0; k < n; k++) {
|
|
144
|
+
const a = k / n * Math.PI * 2 + Math.random() * .6;
|
|
145
|
+
const s = opts.speed * (.55 + Math.random() * .6);
|
|
146
|
+
this.writeSlot(activeCap, x, y, 0, Math.cos(a) * s, Math.sin(a) * s * yScale, zMin + Math.random() * zSpan, drag, gravityZ, 0, 0, 0, 0, life * (.75 + Math.random() * .5), k % 2 ? col2 : opts.color, size, 1, 6.5, shape, 0, 1);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** Directional narrow-fan hit sparks flying away along `angle` (radians). */
|
|
150
|
+
sparks(x, y, angle, opts, activeCap = this.capacity) {
|
|
151
|
+
const scale = opts.liteScale ?? 1;
|
|
152
|
+
const n = Math.max(1, Math.round(opts.count * scale));
|
|
153
|
+
const speed = opts.speed ?? 110;
|
|
154
|
+
const spread = opts.spread ?? 1.6;
|
|
155
|
+
const size = opts.size ?? 1;
|
|
156
|
+
const drag = opts.drag ?? 3;
|
|
157
|
+
const gravityZ = opts.gravityZ ?? 260;
|
|
158
|
+
const baseLife = opts.life ?? .18;
|
|
159
|
+
for (let k = 0; k < n; k++) {
|
|
160
|
+
const a = angle + (Math.random() - .5) * spread;
|
|
161
|
+
const s = speed * (.6 + Math.random() * .7);
|
|
162
|
+
this.writeSlot(activeCap, x, y, 0, Math.cos(a) * s, Math.sin(a) * s * .75, 20 + Math.random() * 50, drag, gravityZ, 0, 0, 0, 0, baseLife + Math.random() * .12, opts.color, size + (k === 0 ? 1 : 0), 1, 6.5, 0, 0, 1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Ground footstep dust puff or expanding elliptical water wake. */
|
|
166
|
+
puff(x, y, opts, activeCap = this.capacity) {
|
|
167
|
+
const shape = opts.shape ?? 0;
|
|
168
|
+
return this.writeSlot(activeCap, x, y, 0, opts.vx ?? 0, opts.vy ?? 0, 0, 0, 0, opts.gravityY ?? (shape === 3 ? 0 : 12), 0, 0, 0, opts.life, opts.color, opts.size, opts.alpha ?? .75, opts.grow ?? 6.5, shape, shape === 3 ? 4 : 3, 0);
|
|
169
|
+
}
|
|
170
|
+
/** Advance all active particles by `dt` seconds. Returns current active count. */
|
|
171
|
+
step(dt, nowMs = 0) {
|
|
172
|
+
if (this.#activeCount === 0 || !(dt > 0)) return this.#activeCount;
|
|
173
|
+
const clampedDt = Math.min(dt, .1);
|
|
174
|
+
const L = this.life;
|
|
175
|
+
const n = this.capacity;
|
|
176
|
+
let alive = 0;
|
|
177
|
+
const tSec = nowMs / 1e3;
|
|
178
|
+
for (let i = 0; i < n; i++) {
|
|
179
|
+
if (L[i] <= 0) continue;
|
|
180
|
+
const rem = L[i] - clampedDt;
|
|
181
|
+
L[i] = rem;
|
|
182
|
+
if (rem <= 0) continue;
|
|
183
|
+
alive++;
|
|
184
|
+
const wobble = this.wobbleAmp[i] ? Math.sin(tSec * this.wobbleFreq[i] + this.phase[i]) * this.wobbleAmp[i] : 0;
|
|
185
|
+
this.px[i] += (this.vx[i] + wobble) * clampedDt;
|
|
186
|
+
this.py[i] += this.vy[i] * clampedDt;
|
|
187
|
+
const gy = this.gravY[i];
|
|
188
|
+
if (gy) this.vy[i] += gy * clampedDt;
|
|
189
|
+
const d = this.drag[i];
|
|
190
|
+
if (d) {
|
|
191
|
+
const keep = Math.max(0, 1 - d * clampedDt);
|
|
192
|
+
this.vx[i] *= keep;
|
|
193
|
+
this.vy[i] *= keep;
|
|
194
|
+
}
|
|
195
|
+
const gz = this.gravZ[i];
|
|
196
|
+
if (gz || this.vz[i] || this.pz[i]) {
|
|
197
|
+
this.vz[i] -= gz * clampedDt;
|
|
198
|
+
const nextZ = this.pz[i] + this.vz[i] * clampedDt;
|
|
199
|
+
this.pz[i] = nextZ > 0 ? nextZ : 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
this.#activeCount = alive;
|
|
203
|
+
return alive;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Render all active particles in `pool` onto any Stage2D-compatible `Graphics` surface.
|
|
208
|
+
* Zero renderer import so lightweight headless or UI bundles never pull WebGL/Canvas2D renderers.
|
|
209
|
+
*/
|
|
210
|
+
function drawParticlePool(g, pool, nowMs = 0, worldView, cullPad = 16) {
|
|
211
|
+
if (pool.activeCount === 0) return;
|
|
212
|
+
const L = pool.life;
|
|
213
|
+
const M = pool.maxLife;
|
|
214
|
+
const n = pool.capacity;
|
|
215
|
+
const minX = worldView ? worldView.x - cullPad : -1e400;
|
|
216
|
+
const maxX = worldView ? worldView.right + cullPad : 1e400;
|
|
217
|
+
const minY = worldView ? worldView.y - cullPad : -1e400;
|
|
218
|
+
const maxY = worldView ? worldView.bottom + cullPad : 1e400;
|
|
219
|
+
for (let i = 0; i < n; i++) {
|
|
220
|
+
const rem = L[i];
|
|
221
|
+
if (rem <= 0) continue;
|
|
222
|
+
const x = pool.px[i];
|
|
223
|
+
const y = pool.py[i] - pool.pz[i];
|
|
224
|
+
if (x < minX || x > maxX || y < minY || y > maxY) continue;
|
|
225
|
+
const k = rem / M[i];
|
|
226
|
+
const age = 1 - k;
|
|
227
|
+
const baseAlpha = pool.alpha[i];
|
|
228
|
+
const fade = pool.fade[i];
|
|
229
|
+
let a;
|
|
230
|
+
if (fade === 0) a = baseAlpha * (k * 1.6 > 1 ? 1 : k * 1.6);
|
|
231
|
+
else if (fade === 1) a = baseAlpha * Math.sin(age * Math.PI);
|
|
232
|
+
else if (fade === 2) a = baseAlpha * Math.sin(age * Math.PI) * (.55 + .45 * Math.sin(nowMs / 180 + pool.phase[i]));
|
|
233
|
+
else if (fade === 3) a = baseAlpha * k * (1 - age * .35);
|
|
234
|
+
else a = baseAlpha * k;
|
|
235
|
+
if (a <= .005) continue;
|
|
236
|
+
const col = pool.col[i];
|
|
237
|
+
const shape = pool.shape[i];
|
|
238
|
+
const baseSize = pool.size[i];
|
|
239
|
+
if (shape === 3) {
|
|
240
|
+
const rx = baseSize + (1 - k * k) * pool.grow[i];
|
|
241
|
+
const ry = rx * .45;
|
|
242
|
+
g.lineStyle(1, col, a);
|
|
243
|
+
g.strokeEllipse(x, y, rx * 2, ry * 2);
|
|
244
|
+
} else if (shape === 2) {
|
|
245
|
+
g.lineStyle(1, col, a);
|
|
246
|
+
g.strokeCircle(x, y, baseSize);
|
|
247
|
+
} else if (shape === 1) {
|
|
248
|
+
const s = baseSize * (.6 + .4 * Math.sin(age * Math.PI));
|
|
249
|
+
g.fillStyle(col, a);
|
|
250
|
+
g.fillRect(x - s, y - .5, s * 2, 1);
|
|
251
|
+
g.fillRect(x - .5, y - s, 1, s * 2);
|
|
252
|
+
} else {
|
|
253
|
+
const s = baseSize + (pool.flags[i] & 1 && k > .6 ? 1 : 0);
|
|
254
|
+
g.fillStyle(col, a);
|
|
255
|
+
g.fillRect(Math.round(x - s / 2), Math.round(y - s / 2), s, s);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const AMBIENT_DEFAULT_COLOR = {
|
|
260
|
+
firefly: 0xffe484,
|
|
261
|
+
petal: 0xffb7ce,
|
|
262
|
+
sparkle: 0xfff6d6,
|
|
263
|
+
ember: 0xff9347,
|
|
264
|
+
bubble: 0xc2ecff
|
|
265
|
+
};
|
|
266
|
+
/**
|
|
267
|
+
* Viewport-culled continuous ambient emitter scheduler writing directly into a shared `ParticlePool`.
|
|
268
|
+
* Tracks `visibleActive` so DemandPacer sleeps when no emitter is in view.
|
|
269
|
+
*/
|
|
270
|
+
var AmbientEmitterPool = class {
|
|
271
|
+
pool;
|
|
272
|
+
#emitters = [];
|
|
273
|
+
#carry = [];
|
|
274
|
+
#visibleActive = false;
|
|
275
|
+
#spawnOpts = {
|
|
276
|
+
x: 0,
|
|
277
|
+
y: 0,
|
|
278
|
+
vx: 0,
|
|
279
|
+
vy: 0,
|
|
280
|
+
wobbleAmp: 0,
|
|
281
|
+
wobbleFreq: 0,
|
|
282
|
+
phase: 0,
|
|
283
|
+
life: 1,
|
|
284
|
+
color: 0,
|
|
285
|
+
size: 2,
|
|
286
|
+
alpha: 1,
|
|
287
|
+
shape: 0,
|
|
288
|
+
fade: 1
|
|
289
|
+
};
|
|
290
|
+
constructor(pool) {
|
|
291
|
+
this.pool = pool;
|
|
292
|
+
}
|
|
293
|
+
get emitters() {
|
|
294
|
+
return this.#emitters;
|
|
295
|
+
}
|
|
296
|
+
get visibleActive() {
|
|
297
|
+
return this.#visibleActive;
|
|
298
|
+
}
|
|
299
|
+
setEmitters(next) {
|
|
300
|
+
this.#emitters = [...next];
|
|
301
|
+
this.#carry = this.#emitters.map(() => 0);
|
|
302
|
+
if (!this.#emitters.length) this.#visibleActive = false;
|
|
303
|
+
}
|
|
304
|
+
isAnimating() {
|
|
305
|
+
return this.#visibleActive || this.pool.isAnimating();
|
|
306
|
+
}
|
|
307
|
+
emitOne(em, scale) {
|
|
308
|
+
const ang = Math.random() * Math.PI * 2;
|
|
309
|
+
const rad = Math.sqrt(Math.random()) * em.radius * scale;
|
|
310
|
+
const s = this.#spawnOpts;
|
|
311
|
+
s.x = em.x * scale + Math.cos(ang) * rad;
|
|
312
|
+
s.y = em.y * scale + Math.sin(ang) * rad * .72;
|
|
313
|
+
s.phase = Math.random() * Math.PI * 2;
|
|
314
|
+
s.color = em.color ?? AMBIENT_DEFAULT_COLOR[em.kind];
|
|
315
|
+
if (em.kind === "firefly") {
|
|
316
|
+
s.vx = (Math.random() - .5) * 7;
|
|
317
|
+
s.vy = -2 - Math.random() * 4;
|
|
318
|
+
s.wobbleAmp = 5;
|
|
319
|
+
s.wobbleFreq = 1e3 / 260;
|
|
320
|
+
s.life = 1.9 + Math.random() * 1.2;
|
|
321
|
+
s.size = 2;
|
|
322
|
+
s.alpha = 1;
|
|
323
|
+
s.shape = 0;
|
|
324
|
+
s.fade = 2;
|
|
325
|
+
} else if (em.kind === "petal") {
|
|
326
|
+
s.vx = 5 + Math.random() * 7;
|
|
327
|
+
s.vy = 4 + Math.random() * 5;
|
|
328
|
+
s.wobbleAmp = 5;
|
|
329
|
+
s.wobbleFreq = 1e3 / 260;
|
|
330
|
+
s.life = 2 + Math.random() * 1;
|
|
331
|
+
s.size = 2;
|
|
332
|
+
s.alpha = .85;
|
|
333
|
+
s.shape = 0;
|
|
334
|
+
s.fade = 1;
|
|
335
|
+
} else if (em.kind === "ember") {
|
|
336
|
+
s.vx = (Math.random() - .5) * 6;
|
|
337
|
+
s.vy = -12 - Math.random() * 10;
|
|
338
|
+
s.wobbleAmp = 0;
|
|
339
|
+
s.wobbleFreq = 0;
|
|
340
|
+
s.life = .95 + Math.random() * .65;
|
|
341
|
+
s.size = 1.5;
|
|
342
|
+
s.alpha = .85;
|
|
343
|
+
s.shape = 0;
|
|
344
|
+
s.fade = 1;
|
|
345
|
+
} else if (em.kind === "bubble") {
|
|
346
|
+
s.vx = (Math.random() - .5) * 3;
|
|
347
|
+
s.vy = -7 - Math.random() * 6;
|
|
348
|
+
s.wobbleAmp = 0;
|
|
349
|
+
s.wobbleFreq = 0;
|
|
350
|
+
s.life = 1.3 + Math.random() * .8;
|
|
351
|
+
s.size = 2.5;
|
|
352
|
+
s.alpha = .85;
|
|
353
|
+
s.shape = 2;
|
|
354
|
+
s.fade = 1;
|
|
355
|
+
} else {
|
|
356
|
+
s.vx = (Math.random() - .5) * 3;
|
|
357
|
+
s.vy = -3 - Math.random() * 4;
|
|
358
|
+
s.wobbleAmp = 0;
|
|
359
|
+
s.wobbleFreq = 0;
|
|
360
|
+
s.life = .75 + Math.random() * .45;
|
|
361
|
+
s.size = 2;
|
|
362
|
+
s.alpha = .85;
|
|
363
|
+
s.shape = 1;
|
|
364
|
+
s.fade = 1;
|
|
365
|
+
}
|
|
366
|
+
this.pool.spawn(s);
|
|
367
|
+
}
|
|
368
|
+
/** Advance emitters (spawning visible particles into `this.pool`) and step `this.pool`. */
|
|
369
|
+
step(dt, nowMs, worldView, opts) {
|
|
370
|
+
this.#visibleActive = false;
|
|
371
|
+
const clampedDt = dt > 0 ? Math.min(dt, .1) : 0;
|
|
372
|
+
const scale = opts?.coordScale ?? 16;
|
|
373
|
+
const dusk = opts?.dusk ?? 0;
|
|
374
|
+
if (!!!opts?.reducedMotion && worldView && this.#emitters.length) for (let i = 0; i < this.#emitters.length; i++) {
|
|
375
|
+
const em = this.#emitters[i];
|
|
376
|
+
if (em.duskOnly && dusk <= .04) continue;
|
|
377
|
+
const rate = Number.isFinite(em.rate) && em.rate > 0 ? em.rate : 0;
|
|
378
|
+
if (rate <= 0) continue;
|
|
379
|
+
const ex = em.x * scale;
|
|
380
|
+
const ey = em.y * scale;
|
|
381
|
+
const pad = em.radius * scale + 32;
|
|
382
|
+
if (ex + pad < worldView.x || ex - pad > worldView.right || ey + pad < worldView.y || ey - pad > worldView.bottom) continue;
|
|
383
|
+
this.#visibleActive = true;
|
|
384
|
+
if (clampedDt > 0) {
|
|
385
|
+
this.#carry[i] = Math.min(8, (this.#carry[i] ?? 0) + rate * clampedDt);
|
|
386
|
+
while (this.#carry[i] >= 1) {
|
|
387
|
+
this.#carry[i] -= 1;
|
|
388
|
+
this.emitOne(em, scale);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
this.pool.step(clampedDt, nowMs);
|
|
393
|
+
return this.isAnimating();
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
/**
|
|
397
|
+
* Pure hit-feel controller combining capped hitstop (with smooth render lag catch-up) and
|
|
398
|
+
* squared-trauma screen shake. Zero DOM, zero renderer coupling — composable by any 2D action game or Mod.
|
|
399
|
+
*/
|
|
400
|
+
var JuiceController = class {
|
|
401
|
+
level;
|
|
402
|
+
stop = 0;
|
|
403
|
+
lag = 0;
|
|
404
|
+
lastStop = -1e9;
|
|
405
|
+
trauma = 0;
|
|
406
|
+
ox = 0;
|
|
407
|
+
oy = 0;
|
|
408
|
+
#hitstopMax;
|
|
409
|
+
#hitstopGap;
|
|
410
|
+
#lagMax;
|
|
411
|
+
#shakePx;
|
|
412
|
+
#traumaDecay;
|
|
413
|
+
constructor(opts) {
|
|
414
|
+
this.level = opts?.level ?? 2;
|
|
415
|
+
this.#hitstopMax = opts?.hitstopMaxMs ?? 60;
|
|
416
|
+
this.#hitstopGap = opts?.hitstopGapMs ?? 220;
|
|
417
|
+
this.#lagMax = opts?.lagMaxMs ?? 120;
|
|
418
|
+
this.#shakePx = opts?.shakePx ?? [
|
|
419
|
+
0,
|
|
420
|
+
2,
|
|
421
|
+
4
|
|
422
|
+
];
|
|
423
|
+
this.#traumaDecay = opts?.traumaDecayPerSec ?? 1.8;
|
|
424
|
+
}
|
|
425
|
+
reset() {
|
|
426
|
+
this.stop = 0;
|
|
427
|
+
this.lag = 0;
|
|
428
|
+
this.trauma = 0;
|
|
429
|
+
this.ox = 0;
|
|
430
|
+
this.oy = 0;
|
|
431
|
+
}
|
|
432
|
+
setLevel(v) {
|
|
433
|
+
this.level = v;
|
|
434
|
+
if (!v) {
|
|
435
|
+
this.trauma = 0;
|
|
436
|
+
this.ox = 0;
|
|
437
|
+
this.oy = 0;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/** Hold world simulation rendering still for `ms` on a heavy blow (rate-limited by `gapMs`). */
|
|
441
|
+
hitstop(ms, nowMs, gapMs = this.#hitstopGap) {
|
|
442
|
+
if (nowMs - this.lastStop < gapMs || this.stop > 0) return;
|
|
443
|
+
this.lastStop = nowMs;
|
|
444
|
+
this.stop = Math.min(this.#hitstopMax, ms);
|
|
445
|
+
}
|
|
446
|
+
/** Add camera trauma in `[0, 1]` (squared to compute pixel shake offset). */
|
|
447
|
+
shake(weight) {
|
|
448
|
+
if (this.level) this.trauma = Math.min(1, this.trauma + weight);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Advance hitstop & screen shake for one frame.
|
|
452
|
+
* Returns effective world particle delta in seconds (`0` while held in hitstop).
|
|
453
|
+
*/
|
|
454
|
+
tick(frameMs, nowMs, paused = false) {
|
|
455
|
+
const safeMs = frameMs > 0 ? frameMs : 0;
|
|
456
|
+
if (this.stop > 0) {
|
|
457
|
+
const d = Math.min(safeMs, this.stop);
|
|
458
|
+
this.stop -= d;
|
|
459
|
+
this.lag = Math.min(this.#lagMax, this.lag + d);
|
|
460
|
+
} else if (this.lag > 0) this.lag = Math.max(0, this.lag - safeMs * .5);
|
|
461
|
+
if (paused) {
|
|
462
|
+
this.trauma = 0;
|
|
463
|
+
this.ox = 0;
|
|
464
|
+
this.oy = 0;
|
|
465
|
+
} else if (this.trauma > 0 && this.level) {
|
|
466
|
+
this.trauma = Math.max(0, this.trauma - this.#traumaDecay * safeMs / 1e3);
|
|
467
|
+
const a = (this.#shakePx[this.level] ?? this.#shakePx[2]) * this.trauma * this.trauma;
|
|
468
|
+
const t = nowMs / 1e3;
|
|
469
|
+
this.ox = Math.round(a * (Math.sin(t * 71) * .6 + Math.sin(t * 113 + 1.3) * .4));
|
|
470
|
+
this.oy = Math.round(a * (Math.sin(t * 89 + 2.1) * .6 + Math.sin(t * 131 + .4) * .4));
|
|
471
|
+
} else {
|
|
472
|
+
this.ox = 0;
|
|
473
|
+
this.oy = 0;
|
|
474
|
+
}
|
|
475
|
+
return this.stop > 0 ? 0 : safeMs / 1e3;
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
export { AmbientEmitterPool, FADE_EASE, FADE_FLICKER, FADE_LINEAR, FADE_PUNCH, FADE_SINE, FLAG_SIZE_POP, JuiceController, PARTICLE_BUBBLE, PARTICLE_RECT, PARTICLE_SPARKLE, PARTICLE_WAKE, ParticlePool, drawParticlePool };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export interface Vec2 {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
export interface SpatialHashBuffers {
|
|
6
|
+
readonly cols: number;
|
|
7
|
+
readonly rows: number;
|
|
8
|
+
readonly cellSize: number;
|
|
9
|
+
readonly originX: number;
|
|
10
|
+
readonly originY: number;
|
|
11
|
+
readonly cellStart: Int32Array;
|
|
12
|
+
readonly cellItems: Int32Array;
|
|
13
|
+
readonly cellOf: Int32Array;
|
|
14
|
+
readonly cursor: Int32Array;
|
|
15
|
+
}
|
|
16
|
+
export interface SpatialHashOptions {
|
|
17
|
+
/** World width covered by the grid (in world units, e.g. px or tiles). */
|
|
18
|
+
width: number;
|
|
19
|
+
/** World height covered by the grid. */
|
|
20
|
+
height: number;
|
|
21
|
+
/** Side length of one grid cell (choose >= typical query / separation diameter). */
|
|
22
|
+
cellSize: number;
|
|
23
|
+
/** Maximum number of indexed entities. */
|
|
24
|
+
maxItems: number;
|
|
25
|
+
/** Optional world-space minimum X (default 0). */
|
|
26
|
+
originX?: number;
|
|
27
|
+
/** Optional world-space minimum Y (default 0). */
|
|
28
|
+
originY?: number;
|
|
29
|
+
}
|
|
30
|
+
/** Allocate plain, structuredClone-friendly TypedArray buffers for a uniform spatial hash. */
|
|
31
|
+
export declare function createSpatialHashBuffers(opts: SpatialHashOptions): SpatialHashBuffers;
|
|
32
|
+
/**
|
|
33
|
+
* Rebuild a CSR uniform spatial hash in O(count + cols * rows) with zero heap allocations.
|
|
34
|
+
* Preserves deterministic ascending-index order inside each cell.
|
|
35
|
+
*/
|
|
36
|
+
export declare function rebuildSpatialHash(cellStart: Int32Array, cellItems: Int32Array, cellOf: Int32Array, cursor: Int32Array, xs: ArrayLike<number>, ys: ArrayLike<number>, count: number, cellSize: number, cols: number, rows: number, originX?: number, originY?: number): void;
|
|
37
|
+
/**
|
|
38
|
+
* Visit every entity index in the `(2 * cellRadius + 1)^2` cells around `(x, y)`.
|
|
39
|
+
* Stops early as soon as `visitor(index)` returns `true`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function querySpatialNeighborhood(cellStart: Int32Array, cellItems: Int32Array, x: number, y: number, cellSize: number, cols: number, rows: number, visitor: (index: number) => boolean | void, cellRadius?: number, originX?: number, originY?: number): void;
|
|
42
|
+
/**
|
|
43
|
+
* Visit every entity index within Euclidean `radius` of `(x, y)` using the spatial hash.
|
|
44
|
+
* Stops early when `visitor(index, distSq)` returns `true`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function querySpatialRadius(cellStart: Int32Array, cellItems: Int32Array, xs: ArrayLike<number>, ys: ArrayLike<number>, x: number, y: number, radius: number, cellSize: number, cols: number, rows: number, visitor: (index: number, distSq: number) => boolean | void, originX?: number, originY?: number): void;
|
|
47
|
+
/**
|
|
48
|
+
* Visit every entity index whose cell overlaps the world-space rectangle `[minX, maxX] x [minY, maxY]`.
|
|
49
|
+
* Stops early when `visitor(index)` returns `true`.
|
|
50
|
+
*/
|
|
51
|
+
export declare function querySpatialRect(cellStart: Int32Array, cellItems: Int32Array, minX: number, minY: number, maxX: number, maxY: number, cellSize: number, cols: number, rows: number, visitor: (index: number) => boolean | void, originX?: number, originY?: number): void;
|
|
52
|
+
/**
|
|
53
|
+
* Accumulate deterministic pairwise neighbor separation vector for entity `selfIndex` at `(x, y)`
|
|
54
|
+
* against all neighbors closer than `minDist` in the surrounding `(2 * cellRadius + 1)^2` cells.
|
|
55
|
+
* Writes result into `out` with zero heap allocation.
|
|
56
|
+
*/
|
|
57
|
+
export declare function accumulateSpatialSeparation(cellStart: Int32Array, cellItems: Int32Array, xs: ArrayLike<number>, ys: ArrayLike<number>, selfIndex: number, x: number, y: number, minDist: number, cellSize: number, cols: number, rows: number, out: Vec2, cellRadius?: number, originX?: number, originY?: number): Vec2;
|
|
58
|
+
/** Ergonomic stateful wrapper over `SpatialHashBuffers` for games, world plugins, and Mods. */
|
|
59
|
+
export declare class SpatialHashGrid {
|
|
60
|
+
readonly buffers: SpatialHashBuffers;
|
|
61
|
+
constructor(opts: SpatialHashOptions);
|
|
62
|
+
get cols(): number;
|
|
63
|
+
get rows(): number;
|
|
64
|
+
get cellSize(): number;
|
|
65
|
+
rebuild(count: number, xs: ArrayLike<number>, ys: ArrayLike<number>): void;
|
|
66
|
+
queryNeighborhood(x: number, y: number, visitor: (index: number) => boolean | void, cellRadius?: number): void;
|
|
67
|
+
queryRadius(x: number, y: number, radius: number, xs: ArrayLike<number>, ys: ArrayLike<number>, visitor: (index: number, distSq: number) => boolean | void): void;
|
|
68
|
+
queryRect(minX: number, minY: number, maxX: number, maxY: number, visitor: (index: number) => boolean | void): void;
|
|
69
|
+
accumulateSeparation(selfIndex: number, x: number, y: number, minDist: number, xs: ArrayLike<number>, ys: ArrayLike<number>, out: Vec2, cellRadius?: number): Vec2;
|
|
70
|
+
}
|