@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
package/dist/ecs.js
ADDED
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
import { SpatialHashGrid } from "./spatial.js";
|
|
2
|
+
// src/ecs.ts
|
|
3
|
+
const INDEX_BITS = 20;
|
|
4
|
+
const INDEX_MASK = (1 << INDEX_BITS) - 1;
|
|
5
|
+
const GEN_MASK = 2047;
|
|
6
|
+
const MAX_COMPONENTS = 32;
|
|
7
|
+
const CIURA_GAPS = [
|
|
8
|
+
701,
|
|
9
|
+
301,
|
|
10
|
+
132,
|
|
11
|
+
57,
|
|
12
|
+
23,
|
|
13
|
+
10,
|
|
14
|
+
4,
|
|
15
|
+
1
|
|
16
|
+
];
|
|
17
|
+
function nextPow2(n) {
|
|
18
|
+
let c = 64;
|
|
19
|
+
while (c < n) c <<= 1;
|
|
20
|
+
return c;
|
|
21
|
+
}
|
|
22
|
+
function allocColumn(kind, capacity) {
|
|
23
|
+
switch (kind) {
|
|
24
|
+
case "f32": return new Float32Array(capacity);
|
|
25
|
+
case "f64": return new Float64Array(capacity);
|
|
26
|
+
case "i32": return new Int32Array(capacity);
|
|
27
|
+
case "u32": return new Uint32Array(capacity);
|
|
28
|
+
case "i16": return new Int16Array(capacity);
|
|
29
|
+
case "u16": return new Uint16Array(capacity);
|
|
30
|
+
case "i8": return new Int8Array(capacity);
|
|
31
|
+
case "u8": return new Uint8Array(capacity);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function growTypedArray(prev, nextCap) {
|
|
35
|
+
const next = new prev.constructor(nextCap);
|
|
36
|
+
next.set(prev);
|
|
37
|
+
return next;
|
|
38
|
+
}
|
|
39
|
+
function growI32Filled(prev, nextCap, fill) {
|
|
40
|
+
const next = new Int32Array(nextCap);
|
|
41
|
+
next.set(prev);
|
|
42
|
+
next.fill(fill, prev.length);
|
|
43
|
+
return next;
|
|
44
|
+
}
|
|
45
|
+
/** Extract the stable internal handle index (0..1,048,575) from an `EntityId`. */
|
|
46
|
+
function entityIndex(entity) {
|
|
47
|
+
return entity & INDEX_MASK;
|
|
48
|
+
}
|
|
49
|
+
/** Extract the 11-bit generation counter from an `EntityId`. */
|
|
50
|
+
function entityGeneration(entity) {
|
|
51
|
+
return entity >>> INDEX_BITS & GEN_MASK;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Define a typed Structure-of-Arrays component schema.
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* const Position = defineComponent('Position', { x: 'f32', y: 'f32' });
|
|
58
|
+
* const Velocity = defineComponent('Velocity', { vx: 'f32', vy: 'f32' });
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
function defineComponent(name, schema) {
|
|
62
|
+
const fields = Object.freeze(Object.keys(schema));
|
|
63
|
+
return Object.freeze({
|
|
64
|
+
name,
|
|
65
|
+
schema: Object.freeze({ ...schema }),
|
|
66
|
+
fields
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Dense-packed, zero-GC Structure-of-Arrays (SoA) ECS world.
|
|
71
|
+
*
|
|
72
|
+
* Live entities occupy contiguous dense slots `0 .. world.count - 1` so simulation loops,
|
|
73
|
+
* frustum culling, spatial indexing, and GPU instanced buffer uploads iterate straight over
|
|
74
|
+
* flat `TypedArray`s with zero per-frame allocations and zero dead-slot branches.
|
|
75
|
+
*/
|
|
76
|
+
var SoaWorld = class {
|
|
77
|
+
/** Number of currently live entities (dense slots `0 .. count - 1`). */
|
|
78
|
+
count = 0;
|
|
79
|
+
/** Allocated dense slot capacity (always a power of two). */
|
|
80
|
+
capacity;
|
|
81
|
+
/** Per-dense-slot 32-bit component membership bitmask (`masks[slot]`). */
|
|
82
|
+
masks;
|
|
83
|
+
/** Dense slot -> live `EntityId` handle (`entities[slot]`). */
|
|
84
|
+
entities;
|
|
85
|
+
handleCapacity;
|
|
86
|
+
nextHandle = 0;
|
|
87
|
+
freeCount = 0;
|
|
88
|
+
freeHandles;
|
|
89
|
+
handleToSlot;
|
|
90
|
+
handleGen;
|
|
91
|
+
stores = [];
|
|
92
|
+
defToStore = /* @__PURE__ */ new Map();
|
|
93
|
+
/** Optional uniform CSR spatial hash grid synced over dense slots `0 .. count - 1`. */
|
|
94
|
+
grid = null;
|
|
95
|
+
gridOriginX = 0;
|
|
96
|
+
gridOriginY = 0;
|
|
97
|
+
gridWidth = 0;
|
|
98
|
+
gridHeight = 0;
|
|
99
|
+
gridCellSize = 4;
|
|
100
|
+
gridMaxItems = 0;
|
|
101
|
+
constructor(initialCapacity = 128) {
|
|
102
|
+
const cap = nextPow2(Math.max(16, initialCapacity | 0));
|
|
103
|
+
this.capacity = cap;
|
|
104
|
+
this.handleCapacity = cap;
|
|
105
|
+
this.masks = new Uint32Array(cap);
|
|
106
|
+
this.entities = new Uint32Array(cap);
|
|
107
|
+
this.freeHandles = new Int32Array(cap);
|
|
108
|
+
this.handleToSlot = new Int32Array(cap).fill(-1);
|
|
109
|
+
this.handleGen = new Uint16Array(cap);
|
|
110
|
+
}
|
|
111
|
+
/** Register a component definition and return its live SoA TypedArray column view. */
|
|
112
|
+
register(def) {
|
|
113
|
+
const existing = this.defToStore.get(def);
|
|
114
|
+
if (existing) return existing.columns;
|
|
115
|
+
if (this.stores.length >= MAX_COMPONENTS) throw new Error(`SoaWorld: maximum ${MAX_COMPONENTS} components per world exceeded`);
|
|
116
|
+
const bit = 1 << this.stores.length >>> 0;
|
|
117
|
+
const columns = {};
|
|
118
|
+
for (let i = 0; i < def.fields.length; i++) {
|
|
119
|
+
const f = def.fields[i];
|
|
120
|
+
columns[f] = allocColumn(def.schema[f], this.capacity);
|
|
121
|
+
}
|
|
122
|
+
const store = {
|
|
123
|
+
def,
|
|
124
|
+
bit,
|
|
125
|
+
columns
|
|
126
|
+
};
|
|
127
|
+
this.stores.push(store);
|
|
128
|
+
this.defToStore.set(def, store);
|
|
129
|
+
return columns;
|
|
130
|
+
}
|
|
131
|
+
/** Access the SoA TypedArray column view for `def` (auto-registers if not yet registered). */
|
|
132
|
+
store(def) {
|
|
133
|
+
const existing = this.defToStore.get(def);
|
|
134
|
+
if (existing) return existing.columns;
|
|
135
|
+
return this.register(def);
|
|
136
|
+
}
|
|
137
|
+
/** Bitmask bit for a single component definition (auto-registers if needed). */
|
|
138
|
+
bitOf(def) {
|
|
139
|
+
let s = this.defToStore.get(def);
|
|
140
|
+
if (!s) {
|
|
141
|
+
this.register(def);
|
|
142
|
+
s = this.defToStore.get(def);
|
|
143
|
+
}
|
|
144
|
+
return s.bit;
|
|
145
|
+
}
|
|
146
|
+
/** Combined 32-bit component query bitmask for one or more component definitions. */
|
|
147
|
+
maskOf(...defs) {
|
|
148
|
+
let mask = 0;
|
|
149
|
+
for (let i = 0; i < defs.length; i++) mask = (mask | this.bitOf(defs[i])) >>> 0;
|
|
150
|
+
return mask;
|
|
151
|
+
}
|
|
152
|
+
/** Ensure dense slot capacity can hold at least `needed` live entities. */
|
|
153
|
+
ensureCapacity(needed) {
|
|
154
|
+
if (needed <= this.capacity) return;
|
|
155
|
+
const cap = nextPow2(needed);
|
|
156
|
+
this.capacity = cap;
|
|
157
|
+
this.masks = growTypedArray(this.masks, cap);
|
|
158
|
+
this.entities = growTypedArray(this.entities, cap);
|
|
159
|
+
for (let i = 0; i < this.stores.length; i++) {
|
|
160
|
+
const st = this.stores[i];
|
|
161
|
+
const fields = st.def.fields;
|
|
162
|
+
for (let f = 0; f < fields.length; f++) {
|
|
163
|
+
const key = fields[f];
|
|
164
|
+
st.columns[key] = growTypedArray(st.columns[key], cap);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
ensureHandleCapacity(needed) {
|
|
169
|
+
if (needed <= this.handleCapacity) return;
|
|
170
|
+
const cap = nextPow2(needed);
|
|
171
|
+
if (cap > 1048576) throw new Error("SoaWorld: maximum entity handle capacity (1,048,576) exceeded");
|
|
172
|
+
this.handleCapacity = cap;
|
|
173
|
+
this.freeHandles = growTypedArray(this.freeHandles, cap);
|
|
174
|
+
this.handleToSlot = growI32Filled(this.handleToSlot, cap, -1);
|
|
175
|
+
this.handleGen = growTypedArray(this.handleGen, cap);
|
|
176
|
+
}
|
|
177
|
+
/** Spawn a new entity at dense slot `world.count` (O(1), zero GC). */
|
|
178
|
+
spawn() {
|
|
179
|
+
const slot = this.count;
|
|
180
|
+
this.ensureCapacity(slot + 1);
|
|
181
|
+
let idx;
|
|
182
|
+
if (this.freeCount > 0) idx = this.freeHandles[--this.freeCount];
|
|
183
|
+
else {
|
|
184
|
+
idx = this.nextHandle++;
|
|
185
|
+
this.ensureHandleCapacity(idx + 1);
|
|
186
|
+
}
|
|
187
|
+
const entity = ((this.handleGen[idx] & GEN_MASK) << INDEX_BITS | idx) >>> 0;
|
|
188
|
+
this.count = slot + 1;
|
|
189
|
+
this.masks[slot] = 0;
|
|
190
|
+
this.entities[slot] = entity;
|
|
191
|
+
this.handleToSlot[idx] = slot;
|
|
192
|
+
for (let i = 0; i < this.stores.length; i++) {
|
|
193
|
+
const st = this.stores[i];
|
|
194
|
+
const fields = st.def.fields;
|
|
195
|
+
for (let f = 0; f < fields.length; f++) st.columns[fields[f]][slot] = 0;
|
|
196
|
+
}
|
|
197
|
+
return entity;
|
|
198
|
+
}
|
|
199
|
+
/** Returns true iff `entity` is currently alive (matches generation and live slot). */
|
|
200
|
+
alive(entity) {
|
|
201
|
+
const idx = entity & INDEX_MASK;
|
|
202
|
+
if (idx >= this.nextHandle) return false;
|
|
203
|
+
return this.handleToSlot[idx] >= 0 && this.handleGen[idx] === (entity >>> INDEX_BITS & GEN_MASK);
|
|
204
|
+
}
|
|
205
|
+
/** Return the current dense slot index (`0 .. count - 1`) for `entity`, or `-1` if dead. */
|
|
206
|
+
slotOf(entity) {
|
|
207
|
+
const idx = entity & INDEX_MASK;
|
|
208
|
+
if (idx >= this.nextHandle) return -1;
|
|
209
|
+
if (this.handleGen[idx] !== (entity >>> INDEX_BITS & GEN_MASK)) return -1;
|
|
210
|
+
return this.handleToSlot[idx];
|
|
211
|
+
}
|
|
212
|
+
/** Return the live `EntityId` stored at dense `slot`. */
|
|
213
|
+
entityAt(slot) {
|
|
214
|
+
return this.entities[slot];
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Despawn `entity` in O(1) via dense swap-remove.
|
|
218
|
+
* Note: the entity previously at `count - 1` moves into `slotOf(entity)`, keeping
|
|
219
|
+
* dense arrays `0 .. count - 1` gap-free for hot iteration and GPU uploads.
|
|
220
|
+
*/
|
|
221
|
+
despawn(entity) {
|
|
222
|
+
const idx = entity & INDEX_MASK;
|
|
223
|
+
if (idx >= this.nextHandle) return false;
|
|
224
|
+
if (this.handleGen[idx] !== (entity >>> INDEX_BITS & GEN_MASK)) return false;
|
|
225
|
+
const slot = this.handleToSlot[idx];
|
|
226
|
+
if (slot < 0) return false;
|
|
227
|
+
const last = this.count - 1;
|
|
228
|
+
if (slot !== last) {
|
|
229
|
+
const movedEntity = this.entities[last];
|
|
230
|
+
this.entities[slot] = movedEntity;
|
|
231
|
+
this.masks[slot] = this.masks[last];
|
|
232
|
+
this.handleToSlot[movedEntity & INDEX_MASK] = slot;
|
|
233
|
+
for (let i = 0; i < this.stores.length; i++) {
|
|
234
|
+
const st = this.stores[i];
|
|
235
|
+
const fields = st.def.fields;
|
|
236
|
+
for (let f = 0; f < fields.length; f++) {
|
|
237
|
+
const col = st.columns[fields[f]];
|
|
238
|
+
col[slot] = col[last];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
this.count = last;
|
|
243
|
+
this.masks[last] = 0;
|
|
244
|
+
this.handleToSlot[idx] = -1;
|
|
245
|
+
this.handleGen[idx] = this.handleGen[idx] + 1 & GEN_MASK;
|
|
246
|
+
this.freeHandles[this.freeCount++] = idx;
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
/** Attach component `def` to `entity` (optionally setting initial field values). Returns dense slot or `-1`. */
|
|
250
|
+
add(entity, def, init) {
|
|
251
|
+
const slot = this.slotOf(entity);
|
|
252
|
+
if (slot < 0) return -1;
|
|
253
|
+
let st = this.defToStore.get(def);
|
|
254
|
+
if (!st) {
|
|
255
|
+
this.register(def);
|
|
256
|
+
st = this.defToStore.get(def);
|
|
257
|
+
}
|
|
258
|
+
this.masks[slot] = (this.masks[slot] | st.bit) >>> 0;
|
|
259
|
+
const fields = st.def.fields;
|
|
260
|
+
if (init) for (let f = 0; f < fields.length; f++) {
|
|
261
|
+
const key = fields[f];
|
|
262
|
+
const v = init[key];
|
|
263
|
+
st.columns[key][slot] = v !== void 0 ? v : 0;
|
|
264
|
+
}
|
|
265
|
+
else for (let f = 0; f < fields.length; f++) st.columns[fields[f]][slot] = 0;
|
|
266
|
+
return slot;
|
|
267
|
+
}
|
|
268
|
+
/** Detach component `def` from `entity`. */
|
|
269
|
+
remove(entity, def) {
|
|
270
|
+
const slot = this.slotOf(entity);
|
|
271
|
+
if (slot < 0) return false;
|
|
272
|
+
const st = this.defToStore.get(def);
|
|
273
|
+
if (!st) return false;
|
|
274
|
+
if ((this.masks[slot] & st.bit) === 0) return false;
|
|
275
|
+
this.masks[slot] = (this.masks[slot] & ~st.bit) >>> 0;
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
/** Returns true iff `entity` is alive and currently holds component `def`. */
|
|
279
|
+
has(entity, def) {
|
|
280
|
+
const slot = this.slotOf(entity);
|
|
281
|
+
if (slot < 0) return false;
|
|
282
|
+
const st = this.defToStore.get(def);
|
|
283
|
+
return Boolean(st && (this.masks[slot] & st.bit) !== 0);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Populate `outSlots` with dense slot indices matching `includeMask` (and excluding `excludeMask`).
|
|
287
|
+
* Returns the number of matching slots written (zero allocations).
|
|
288
|
+
*/
|
|
289
|
+
query(includeMask, outSlots, excludeMask = 0) {
|
|
290
|
+
const n = this.count;
|
|
291
|
+
const masks = this.masks;
|
|
292
|
+
const maxOut = outSlots.length;
|
|
293
|
+
let written = 0;
|
|
294
|
+
const inc = includeMask >>> 0;
|
|
295
|
+
const exc = excludeMask >>> 0;
|
|
296
|
+
for (let s = 0; s < n; s++) {
|
|
297
|
+
const m = masks[s];
|
|
298
|
+
if ((m & inc) === inc && (exc === 0 || (m & exc) === 0)) {
|
|
299
|
+
if (written >= maxOut) break;
|
|
300
|
+
outSlots[written++] = s;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return written;
|
|
304
|
+
}
|
|
305
|
+
/** Zero-GC callback iteration over dense slots matching `includeMask` (and excluding `excludeMask`). */
|
|
306
|
+
forEach(includeMask, fn, excludeMask = 0) {
|
|
307
|
+
const n = this.count;
|
|
308
|
+
const masks = this.masks;
|
|
309
|
+
const entities = this.entities;
|
|
310
|
+
const inc = includeMask >>> 0;
|
|
311
|
+
const exc = excludeMask >>> 0;
|
|
312
|
+
for (let s = 0; s < n; s++) {
|
|
313
|
+
const m = masks[s];
|
|
314
|
+
if ((m & inc) === inc && (exc === 0 || (m & exc) === 0)) fn(s, entities[s]);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Rebuild the uniform CSR `SpatialHashGrid` over dense slots `0 .. count - 1` using position columns.
|
|
319
|
+
* Reuses grid buffers across frames unless map bounds or capacity grow.
|
|
320
|
+
*/
|
|
321
|
+
syncSpatial(xCol, yCol, opts) {
|
|
322
|
+
const cs = Math.max(1, opts.cellSize);
|
|
323
|
+
const needed = Math.max(128, this.capacity);
|
|
324
|
+
if (!this.grid || this.gridOriginX !== opts.originX || this.gridOriginY !== opts.originY || this.gridWidth !== opts.width || this.gridHeight !== opts.height || this.gridCellSize !== cs || this.gridMaxItems < needed) {
|
|
325
|
+
this.gridOriginX = opts.originX;
|
|
326
|
+
this.gridOriginY = opts.originY;
|
|
327
|
+
this.gridWidth = opts.width;
|
|
328
|
+
this.gridHeight = opts.height;
|
|
329
|
+
this.gridCellSize = cs;
|
|
330
|
+
this.gridMaxItems = needed;
|
|
331
|
+
this.grid = new SpatialHashGrid({
|
|
332
|
+
originX: opts.originX,
|
|
333
|
+
originY: opts.originY,
|
|
334
|
+
width: opts.width,
|
|
335
|
+
height: opts.height,
|
|
336
|
+
cellSize: cs,
|
|
337
|
+
maxItems: needed
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
this.grid.rebuild(this.count, xCol, yCol);
|
|
341
|
+
return this.grid;
|
|
342
|
+
}
|
|
343
|
+
/** Query dense slots within Euclidean `radius` of `(cx, cy)` using the synced spatial grid. */
|
|
344
|
+
queryRadius(cx, cy, radius, xCol, yCol, outSlots, includeMask = 0) {
|
|
345
|
+
if (!this.grid || this.count === 0) return 0;
|
|
346
|
+
const inc = includeMask >>> 0;
|
|
347
|
+
const masks = this.masks;
|
|
348
|
+
const maxOut = outSlots.length;
|
|
349
|
+
let written = 0;
|
|
350
|
+
this.grid.queryRadius(cx, cy, radius, xCol, yCol, (slot) => {
|
|
351
|
+
if (inc !== 0 && (masks[slot] & inc) !== inc) return;
|
|
352
|
+
if (written >= maxOut) return true;
|
|
353
|
+
outSlots[written++] = slot;
|
|
354
|
+
});
|
|
355
|
+
return written;
|
|
356
|
+
}
|
|
357
|
+
/** Query dense slots inside `[minX, maxX] x [minY, maxY]` using the synced spatial grid. */
|
|
358
|
+
queryRect(minX, minY, maxX, maxY, xCol, yCol, outSlots, includeMask = 0) {
|
|
359
|
+
if (!this.grid || this.count === 0) return 0;
|
|
360
|
+
const inc = includeMask >>> 0;
|
|
361
|
+
const masks = this.masks;
|
|
362
|
+
const maxOut = outSlots.length;
|
|
363
|
+
let written = 0;
|
|
364
|
+
this.grid.queryRect(minX, minY, maxX, maxY, (slot) => {
|
|
365
|
+
if (inc !== 0 && (masks[slot] & inc) !== inc) return;
|
|
366
|
+
const x = xCol[slot];
|
|
367
|
+
const y = yCol[slot];
|
|
368
|
+
if (x < minX || x > maxX || y < minY || y > maxY) return;
|
|
369
|
+
if (written >= maxOut) return true;
|
|
370
|
+
outSlots[written++] = slot;
|
|
371
|
+
});
|
|
372
|
+
return written;
|
|
373
|
+
}
|
|
374
|
+
/** Despawn all live entities while keeping allocated TypedArray capacity and registered components. */
|
|
375
|
+
clear() {
|
|
376
|
+
for (let s = 0; s < this.count; s++) {
|
|
377
|
+
const idx = this.entities[s] & INDEX_MASK;
|
|
378
|
+
this.handleToSlot[idx] = -1;
|
|
379
|
+
this.handleGen[idx] = this.handleGen[idx] + 1 & GEN_MASK;
|
|
380
|
+
this.freeHandles[this.freeCount++] = idx;
|
|
381
|
+
this.masks[s] = 0;
|
|
382
|
+
}
|
|
383
|
+
this.count = 0;
|
|
384
|
+
}
|
|
385
|
+
/** PII-free runtime statistics snapshot. */
|
|
386
|
+
stats() {
|
|
387
|
+
return {
|
|
388
|
+
entities: this.count,
|
|
389
|
+
capacity: this.capacity,
|
|
390
|
+
components: this.stores.length,
|
|
391
|
+
spatialCells: this.grid ? this.grid.cols * this.grid.rows : 0
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
/**
|
|
396
|
+
* In-place zero-GC insertion/Ciura-gap Shell sort of `slots[0..count - 1]` ordered ascending by
|
|
397
|
+
* `depthCol[slot]`, with optional secondary tie-break ascending by `tieCol[slot]` (e.g. primary
|
|
398
|
+
* texture or material ID to minimize GPU batch switches on equal-depth rows).
|
|
399
|
+
*/
|
|
400
|
+
function sortSlotsByDepth(slots, count, depthCol, tieCol) {
|
|
401
|
+
if (count <= 1) return;
|
|
402
|
+
if (!tieCol) {
|
|
403
|
+
if (count <= 48) {
|
|
404
|
+
for (let i = 1; i < count; i++) {
|
|
405
|
+
const s = slots[i];
|
|
406
|
+
const ds = depthCol[s];
|
|
407
|
+
let j = i - 1;
|
|
408
|
+
while (j >= 0 && depthCol[slots[j]] > ds) {
|
|
409
|
+
slots[j + 1] = slots[j];
|
|
410
|
+
j--;
|
|
411
|
+
}
|
|
412
|
+
slots[j + 1] = s;
|
|
413
|
+
}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
for (let g = 0; g < CIURA_GAPS.length; g++) {
|
|
417
|
+
const gap = CIURA_GAPS[g];
|
|
418
|
+
if (gap >= count) continue;
|
|
419
|
+
for (let i = gap; i < count; i++) {
|
|
420
|
+
const s = slots[i];
|
|
421
|
+
const ds = depthCol[s];
|
|
422
|
+
let j = i;
|
|
423
|
+
while (j >= gap && depthCol[slots[j - gap]] > ds) {
|
|
424
|
+
slots[j] = slots[j - gap];
|
|
425
|
+
j -= gap;
|
|
426
|
+
}
|
|
427
|
+
slots[j] = s;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (count <= 48) {
|
|
433
|
+
for (let i = 1; i < count; i++) {
|
|
434
|
+
const s = slots[i];
|
|
435
|
+
const ds = depthCol[s];
|
|
436
|
+
const ts = tieCol[s];
|
|
437
|
+
let j = i - 1;
|
|
438
|
+
while (j >= 0) {
|
|
439
|
+
const sj = slots[j];
|
|
440
|
+
const dj = depthCol[sj];
|
|
441
|
+
if (dj < ds || dj === ds && tieCol[sj] <= ts) break;
|
|
442
|
+
slots[j + 1] = sj;
|
|
443
|
+
j--;
|
|
444
|
+
}
|
|
445
|
+
slots[j + 1] = s;
|
|
446
|
+
}
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
for (let g = 0; g < CIURA_GAPS.length; g++) {
|
|
450
|
+
const gap = CIURA_GAPS[g];
|
|
451
|
+
if (gap >= count) continue;
|
|
452
|
+
for (let i = gap; i < count; i++) {
|
|
453
|
+
const s = slots[i];
|
|
454
|
+
const ds = depthCol[s];
|
|
455
|
+
const ts = tieCol[s];
|
|
456
|
+
let j = i;
|
|
457
|
+
while (j >= gap) {
|
|
458
|
+
const sj = slots[j - gap];
|
|
459
|
+
const dj = depthCol[sj];
|
|
460
|
+
if (dj < ds || dj === ds && tieCol[sj] <= ts) break;
|
|
461
|
+
slots[j] = sj;
|
|
462
|
+
j -= gap;
|
|
463
|
+
}
|
|
464
|
+
slots[j] = s;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Cull entities within `[minX, maxX] x [minY, maxY]` (optionally filtered by `requiredMask`)
|
|
470
|
+
* into `outSlots` and sort them ascending by `depthCol[slot]` with zero allocations.
|
|
471
|
+
*/
|
|
472
|
+
function cullAndSortSlots(count, xCol, yCol, depthCol, minX, minY, maxX, maxY, outSlots, maskCol, requiredMask = 0, tieCol) {
|
|
473
|
+
const maxOut = outSlots.length;
|
|
474
|
+
const req = requiredMask >>> 0;
|
|
475
|
+
let visible = 0;
|
|
476
|
+
for (let s = 0; s < count; s++) {
|
|
477
|
+
if (maskCol && req !== 0 && (maskCol[s] & req) !== req) continue;
|
|
478
|
+
const x = xCol[s];
|
|
479
|
+
if (x < minX || x > maxX) continue;
|
|
480
|
+
const y = yCol[s];
|
|
481
|
+
if (y < minY || y > maxY) continue;
|
|
482
|
+
if (visible >= maxOut) break;
|
|
483
|
+
outSlots[visible++] = s;
|
|
484
|
+
}
|
|
485
|
+
sortSlotsByDepth(outSlots, visible, depthCol, tieCol);
|
|
486
|
+
return visible;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Partition depth-sorted entity slots (`sortedSlots[0..sortedCount - 1]`) into minimal
|
|
490
|
+
* painter's-order `DepthBand` spans strictly interleaved between static 2.5D blockers
|
|
491
|
+
* (`blockerDepths[0..blockerCount - 1]`, sorted ascending).
|
|
492
|
+
*
|
|
493
|
+
* Ideal for hardware-instanced 2.5D rendering: emits only as many instanced passes as there
|
|
494
|
+
* are distinct blocker intervals occupied by visible entities, while clamping open-edge bands
|
|
495
|
+
* to actual entity depths so un-blocked sprites preserve exact painter's order. Reuses objects
|
|
496
|
+
* in `outBands`.
|
|
497
|
+
*/
|
|
498
|
+
function partitionDepthBands(sortedSlots, sortedCount, depthCol, blockerDepths, blockerCount, outBands) {
|
|
499
|
+
if (sortedCount === 0) return 0;
|
|
500
|
+
let bands = 0;
|
|
501
|
+
let bIdx = 0;
|
|
502
|
+
let i = 0;
|
|
503
|
+
let prevBlocker = -1e400;
|
|
504
|
+
while (i < sortedCount) {
|
|
505
|
+
const firstDepth = depthCol[sortedSlots[i]];
|
|
506
|
+
while (bIdx < blockerCount && blockerDepths[bIdx] <= firstDepth) {
|
|
507
|
+
prevBlocker = blockerDepths[bIdx];
|
|
508
|
+
bIdx++;
|
|
509
|
+
}
|
|
510
|
+
const nextBlocker = bIdx < blockerCount ? blockerDepths[bIdx] : 1e400;
|
|
511
|
+
const start = i;
|
|
512
|
+
i++;
|
|
513
|
+
while (i < sortedCount && depthCol[sortedSlots[i]] < nextBlocker) i++;
|
|
514
|
+
const end = i;
|
|
515
|
+
let depth;
|
|
516
|
+
if (prevBlocker === -1e400 && nextBlocker === 1e400) depth = depthCol[sortedSlots[end - 1]];
|
|
517
|
+
else if (prevBlocker === -1e400) depth = Math.min(depthCol[sortedSlots[end - 1]], nextBlocker - 1e-6);
|
|
518
|
+
else if (nextBlocker === 1e400) depth = Math.max(depthCol[sortedSlots[start]], prevBlocker + 1e-6);
|
|
519
|
+
else depth = prevBlocker + (nextBlocker - prevBlocker) * .5;
|
|
520
|
+
let band = outBands[bands];
|
|
521
|
+
if (!band) {
|
|
522
|
+
band = {
|
|
523
|
+
start,
|
|
524
|
+
end,
|
|
525
|
+
depth
|
|
526
|
+
};
|
|
527
|
+
outBands[bands] = band;
|
|
528
|
+
} else {
|
|
529
|
+
band.start = start;
|
|
530
|
+
band.end = end;
|
|
531
|
+
band.depth = depth;
|
|
532
|
+
}
|
|
533
|
+
bands++;
|
|
534
|
+
}
|
|
535
|
+
return bands;
|
|
536
|
+
}
|
|
537
|
+
export { SoaWorld, cullAndSortSlots, defineComponent, entityGeneration, entityIndex, partitionDepthBands, sortSlotsByDepth };
|
package/dist/flex.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
export type FlexDir = 'row' | 'col';
|
|
2
|
+
export type FlexAlign = 'start' | 'center' | 'end' | 'stretch';
|
|
3
|
+
export type FlexJustify = 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly';
|
|
4
|
+
export type FlexPadding = number | readonly [number, number] | readonly [number, number, number, number];
|
|
5
|
+
export interface FlexContainerSpec {
|
|
6
|
+
dir?: FlexDir;
|
|
7
|
+
/** Fixed outer width; omit or pass `null` to auto-size to content + padding. */
|
|
8
|
+
w?: number | null;
|
|
9
|
+
/** Fixed outer height; omit or pass `null` to auto-size to content + padding. */
|
|
10
|
+
h?: number | null;
|
|
11
|
+
gap?: number;
|
|
12
|
+
padding?: FlexPadding;
|
|
13
|
+
align?: FlexAlign;
|
|
14
|
+
justify?: FlexJustify;
|
|
15
|
+
/** Snap resolved rects and outer dimensions to integer pixels (default `true`). */
|
|
16
|
+
round?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface FlexItemSpec {
|
|
19
|
+
w: number;
|
|
20
|
+
h: number;
|
|
21
|
+
basis?: number | null;
|
|
22
|
+
grow?: number;
|
|
23
|
+
shrink?: number;
|
|
24
|
+
minW?: number;
|
|
25
|
+
maxW?: number;
|
|
26
|
+
minH?: number;
|
|
27
|
+
maxH?: number;
|
|
28
|
+
alignSelf?: FlexAlign;
|
|
29
|
+
visible?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export interface FlexItemRect {
|
|
32
|
+
x: number;
|
|
33
|
+
y: number;
|
|
34
|
+
w: number;
|
|
35
|
+
h: number;
|
|
36
|
+
visible: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface FlexResult {
|
|
39
|
+
w: number;
|
|
40
|
+
h: number;
|
|
41
|
+
contentW: number;
|
|
42
|
+
contentH: number;
|
|
43
|
+
rects: FlexItemRect[];
|
|
44
|
+
}
|
|
45
|
+
/** Normalize uniform, [v, h], or [top, right, bottom, left] padding into `[top, right, bottom, left]`. */
|
|
46
|
+
export declare function resolvePadding(padding?: FlexPadding): [number, number, number, number];
|
|
47
|
+
/**
|
|
48
|
+
* Solve a single-axis flex container (`row` or `col`) with optional grow/shrink, min/max clamping,
|
|
49
|
+
* cross-axis alignment/stretch, and main-axis justification.
|
|
50
|
+
*/
|
|
51
|
+
export declare function computeFlexLayout(container: FlexContainerSpec, items: readonly FlexItemSpec[]): FlexResult;
|
|
52
|
+
export type WrapJustify = 'start' | 'center' | 'end' | 'between';
|
|
53
|
+
export interface WrapContainerSpec {
|
|
54
|
+
/** Outer maximum width (required for responsive line wrapping; optional when `columns` + natural/fixed `cellW` is set). */
|
|
55
|
+
w?: number | null;
|
|
56
|
+
/** Optional fixed outer height; omit or pass `null` to auto-size to wrapped rows + padding. */
|
|
57
|
+
h?: number | null;
|
|
58
|
+
/** Uniform gap between items on both axes (overridden by `colGap` / `rowGap`). */
|
|
59
|
+
gap?: number;
|
|
60
|
+
/** Horizontal gap between columns/items in a row. */
|
|
61
|
+
colGap?: number;
|
|
62
|
+
/** Vertical gap between wrapped rows. */
|
|
63
|
+
rowGap?: number;
|
|
64
|
+
padding?: FlexPadding;
|
|
65
|
+
/** Optional fixed number of columns (e.g. `7` for calendar grids, `8` for swatch rows). Omit for natural flex-wrap flow. */
|
|
66
|
+
columns?: number | null;
|
|
67
|
+
/** Optional uniform cell width override. */
|
|
68
|
+
cellW?: number | null;
|
|
69
|
+
/** Optional uniform cell height override. */
|
|
70
|
+
cellH?: number | null;
|
|
71
|
+
/** Vertical alignment of items within each wrapped row (default `'start'`). */
|
|
72
|
+
align?: FlexAlign;
|
|
73
|
+
/** Horizontal alignment of items within each wrapped row (default `'start'`). */
|
|
74
|
+
justify?: WrapJustify;
|
|
75
|
+
/** Horizontal alignment of an item inside its fixed-column slot when `columns` is set (default `'stretch'`). */
|
|
76
|
+
cellAlign?: FlexAlign;
|
|
77
|
+
/** Snap resolved rects and outer dimensions to integer pixels (default `true`). */
|
|
78
|
+
round?: boolean;
|
|
79
|
+
}
|
|
80
|
+
export interface WrapItemSpec {
|
|
81
|
+
w: number;
|
|
82
|
+
h: number;
|
|
83
|
+
visible?: boolean;
|
|
84
|
+
/** Column span when `columns` is set (clamped to `1..columns`). */
|
|
85
|
+
colSpan?: number;
|
|
86
|
+
/** Place item on its own full-width row (e.g. divider, section label, or "None" full-width chip). */
|
|
87
|
+
fullRow?: boolean;
|
|
88
|
+
}
|
|
89
|
+
export interface WrapItemRect {
|
|
90
|
+
x: number;
|
|
91
|
+
y: number;
|
|
92
|
+
w: number;
|
|
93
|
+
h: number;
|
|
94
|
+
/** Zero-based visual row index (`-1` when hidden). */
|
|
95
|
+
row: number;
|
|
96
|
+
/** Zero-based visual column index within its row (`-1` when hidden). */
|
|
97
|
+
col: number;
|
|
98
|
+
visible: boolean;
|
|
99
|
+
}
|
|
100
|
+
export interface WrapResult {
|
|
101
|
+
w: number;
|
|
102
|
+
h: number;
|
|
103
|
+
contentW: number;
|
|
104
|
+
contentH: number;
|
|
105
|
+
rows: number;
|
|
106
|
+
colsPerRow: number[];
|
|
107
|
+
rects: WrapItemRect[];
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Solve a multi-line wrapping flow (`columns` omitted) or fixed-column grid (`columns >= 1`)
|
|
111
|
+
* with crisp integer pixel snapping and `(row, col)` coordinates for 2D keyboard navigation.
|
|
112
|
+
*/
|
|
113
|
+
export declare function computeWrapLayout(container: WrapContainerSpec, items: readonly WrapItemSpec[]): WrapResult;
|