@esengine/ecs-framework 2.1.27 → 2.1.29

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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @esengine/ecs-framework v2.1.27
2
+ * @esengine/ecs-framework v2.1.29
3
3
  * TypeScript definitions
4
4
  */
5
5
  /**
@@ -34,8 +34,8 @@ declare class GlobalManager {
34
34
  update(): void;
35
35
  }
36
36
 
37
- interface ITimer {
38
- context: any;
37
+ interface ITimer<TContext = unknown> {
38
+ context: TContext;
39
39
  /**
40
40
  * 调用stop以停止此计时器再次运行。这对非重复计时器没有影响。
41
41
  */
@@ -53,11 +53,11 @@ interface ITimer {
53
53
  /**
54
54
  * 私有类隐藏ITimer的实现
55
55
  */
56
- declare class Timer implements ITimer {
57
- context: any;
56
+ declare class Timer<TContext = unknown> implements ITimer<TContext> {
57
+ context: TContext;
58
58
  _timeInSeconds: number;
59
59
  _repeats: boolean;
60
- _onTime: (timer: ITimer) => void;
60
+ _onTime: (timer: ITimer<TContext>) => void;
61
61
  _isDone: boolean;
62
62
  _elapsedTime: number;
63
63
  getContext<T>(): T;
@@ -72,7 +72,7 @@ declare class Timer implements ITimer {
72
72
  reset(): void;
73
73
  stop(): void;
74
74
  tick(): boolean;
75
- initialize(timeInsSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer) => void): void;
75
+ initialize(timeInsSeconds: number, repeats: boolean, context: TContext, onTime: (timer: ITimer<TContext>) => void): void;
76
76
  /**
77
77
  * 空出对象引用,以便在js需要时GC可以清理它们的引用
78
78
  */
@@ -83,7 +83,7 @@ declare class Timer implements ITimer {
83
83
  * 允许动作的延迟和重复执行
84
84
  */
85
85
  declare class TimerManager extends GlobalManager {
86
- _timers: Timer[];
86
+ _timers: Array<Timer<unknown>>;
87
87
  update(): void;
88
88
  /**
89
89
  * 调度一个一次性或重复的计时器,该计时器将调用已传递的动作
@@ -92,7 +92,7 @@ declare class TimerManager extends GlobalManager {
92
92
  * @param context
93
93
  * @param onTime
94
94
  */
95
- schedule(timeInSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer) => void): Timer;
95
+ schedule<TContext = unknown>(timeInSeconds: number, repeats: boolean, context: TContext, onTime: (timer: ITimer<TContext>) => void): Timer<TContext>;
96
96
  }
97
97
 
98
98
  /**
@@ -359,7 +359,7 @@ declare class Pool<T extends IPoolable> {
359
359
  * @param estimatedObjectSize 估算的单个对象大小
360
360
  * @returns 对象池实例
361
361
  */
362
- static getPool<T extends IPoolable>(type: new (...args: any[]) => T, maxSize?: number, estimatedObjectSize?: number): Pool<T>;
362
+ static getPool<T extends IPoolable>(type: new (...args: unknown[]) => T, maxSize?: number, estimatedObjectSize?: number): Pool<T>;
363
363
  /**
364
364
  * 从池中获取对象
365
365
  * @returns 对象实例
@@ -412,24 +412,24 @@ declare class Pool<T extends IPoolable> {
412
412
  * @param type 对象类型
413
413
  * @returns 对象实例
414
414
  */
415
- static obtain<T extends IPoolable>(type: new (...args: any[]) => T): T;
415
+ static obtain<T extends IPoolable>(type: new (...args: unknown[]) => T): T;
416
416
  /**
417
417
  * 静态方法:将对象归还到对应类型的池中
418
418
  * @param type 对象类型
419
419
  * @param obj 要归还的对象
420
420
  */
421
- static free<T extends IPoolable>(type: new (...args: any[]) => T, obj: T): void;
421
+ static free<T extends IPoolable>(type: new (...args: unknown[]) => T, obj: T): void;
422
422
  /**
423
423
  * 静态方法:预热指定类型的池
424
424
  * @param type 对象类型
425
425
  * @param count 要创建的对象数量
426
426
  */
427
- static warmUp<T extends IPoolable>(type: new (...args: any[]) => T, count: number): void;
427
+ static warmUp<T extends IPoolable>(type: new (...args: unknown[]) => T, count: number): void;
428
428
  /**
429
429
  * 静态方法:清空指定类型的池
430
430
  * @param type 对象类型
431
431
  */
432
- static clearPool<T extends IPoolable>(type: new (...args: any[]) => T): void;
432
+ static clearPool<T extends IPoolable>(type: new (...args: unknown[]) => T): void;
433
433
  /**
434
434
  * 静态方法:清空所有池
435
435
  */
@@ -581,8 +581,6 @@ interface IComponent {
581
581
  interface ISystemBase {
582
582
  /** 系统名称 */
583
583
  readonly systemName: string;
584
- /** 系统处理的实体列表 */
585
- readonly entities: readonly any[];
586
584
  /** 更新顺序/优先级 */
587
585
  updateOrder: number;
588
586
  /** 系统启用状态 */
@@ -599,7 +597,7 @@ interface ISystemBase {
599
597
  *
600
598
  * 用于类型安全的组件操作
601
599
  */
602
- type ComponentType$1<T extends IComponent = IComponent> = new (...args: any[]) => T;
600
+ type ComponentType$1<T extends IComponent = IComponent> = new (...args: unknown[]) => T;
603
601
  /**
604
602
  * 事件总线接口
605
603
  * 提供类型安全的事件发布订阅机制
@@ -678,7 +676,7 @@ interface IEventListenerConfig {
678
676
  /** 是否异步执行 */
679
677
  async?: boolean;
680
678
  /** 执行上下文 */
681
- context?: any;
679
+ context?: unknown;
682
680
  }
683
681
  /**
684
682
  * 事件统计信息接口
@@ -757,7 +755,7 @@ interface IPerformanceEventData extends IEventData {
757
755
  /** 内存使用量 */
758
756
  memoryUsage?: number;
759
757
  /** 额外数据 */
760
- metadata?: Record<string, any>;
758
+ metadata?: Record<string, unknown>;
761
759
  }
762
760
  /**
763
761
  * ECS调试配置接口
@@ -818,6 +816,23 @@ interface IECSDebugData {
818
816
  /** 场景数据 */
819
817
  scenes?: ISceneDebugData;
820
818
  }
819
+ /**
820
+ * 实体层次结构节点接口
821
+ */
822
+ interface IEntityHierarchyNode {
823
+ id: number;
824
+ name: string;
825
+ active: boolean;
826
+ enabled: boolean;
827
+ activeInHierarchy: boolean;
828
+ componentCount: number;
829
+ componentTypes: string[];
830
+ parentId: number | null;
831
+ children: IEntityHierarchyNode[];
832
+ depth: number;
833
+ tag: number;
834
+ updateOrder: number;
835
+ }
821
836
  /**
822
837
  * 实体调试数据接口
823
838
  */
@@ -862,7 +877,7 @@ interface IEntityDebugData {
862
877
  componentCount: number;
863
878
  componentTypes: string[];
864
879
  parentId: number | null;
865
- children: any[];
880
+ children: IEntityHierarchyNode[];
866
881
  depth: number;
867
882
  tag: number;
868
883
  updateOrder: number;
@@ -885,7 +900,7 @@ interface IEntityDebugData {
885
900
  depth: number;
886
901
  components: Array<{
887
902
  typeName: string;
888
- properties: Record<string, any>;
903
+ properties: Record<string, unknown>;
889
904
  }>;
890
905
  componentCount: number;
891
906
  componentTypes: string[];
@@ -1120,2390 +1135,2537 @@ declare abstract class Component implements IComponent {
1120
1135
  }
1121
1136
 
1122
1137
  /**
1123
- * 组件类型定义
1124
- */
1125
- type ComponentType<T extends Component = Component> = new (...args: any[]) => T;
1126
- /**
1127
- * 高性能组件存储器
1128
- * 使用SoA(Structure of Arrays)模式存储组件
1138
+ * 高性能实体列表管理器
1139
+ * 管理场景中的所有实体,支持快速查找和批量操作
1129
1140
  */
1130
- declare class ComponentStorage<T extends Component> {
1131
- private components;
1132
- private entityToIndex;
1133
- private indexToEntity;
1134
- private freeIndices;
1135
- private componentType;
1136
- private _size;
1137
- constructor(componentType: ComponentType<T>);
1141
+ declare class EntityList {
1142
+ buffer: Entity[];
1143
+ private _scene;
1144
+ private _idToEntity;
1145
+ private _nameToEntities;
1146
+ private _entitiesToAdd;
1147
+ private _entitiesToRemove;
1148
+ private _isUpdating;
1149
+ get count(): number;
1150
+ constructor(scene: any);
1138
1151
  /**
1139
- * 添加组件
1140
- * @param entityId 实体ID
1141
- * @param component 组件实例
1152
+ * 添加实体(立即添加或延迟添加)
1153
+ * @param entity 要添加的实体
1142
1154
  */
1143
- addComponent(entityId: number, component: T): void;
1155
+ add(entity: Entity): void;
1144
1156
  /**
1145
- * 获取组件
1146
- * @param entityId 实体ID
1147
- * @returns 组件实例或null
1157
+ * 立即添加实体
1158
+ * @param entity 要添加的实体
1148
1159
  */
1149
- getComponent(entityId: number): T | null;
1160
+ private addImmediate;
1150
1161
  /**
1151
- * 检查实体是否有此组件
1152
- * @param entityId 实体ID
1153
- * @returns 是否有组件
1162
+ * 移除实体(立即移除或延迟移除)
1163
+ * @param entity 要移除的实体
1154
1164
  */
1155
- hasComponent(entityId: number): boolean;
1165
+ remove(entity: Entity): void;
1156
1166
  /**
1157
- * 移除组件
1158
- * @param entityId 实体ID
1159
- * @returns 被移除的组件或null
1167
+ * 立即移除实体
1168
+ * @param entity 要移除的实体
1160
1169
  */
1161
- removeComponent(entityId: number): T | null;
1170
+ private removeImmediate;
1162
1171
  /**
1163
- * 高效遍历所有组件
1164
- * @param callback 回调函数
1172
+ * 移除所有实体
1165
1173
  */
1166
- forEach(callback: (component: T, entityId: number, index: number) => void): void;
1174
+ removeAllEntities(): void;
1167
1175
  /**
1168
- * 获取所有组件(密集数组)
1169
- * @returns 组件数组
1176
+ * 更新实体列表,处理延迟操作
1170
1177
  */
1171
- getDenseArray(): {
1172
- components: T[];
1173
- entityIds: number[];
1174
- };
1178
+ updateLists(): void;
1175
1179
  /**
1176
- * 清空所有组件
1180
+ * 更新所有实体
1177
1181
  */
1178
- clear(): void;
1182
+ update(): void;
1179
1183
  /**
1180
- * 获取组件数量
1184
+ * 根据名称查找实体(使用索引,O(1)复杂度)
1185
+ * @param name 实体名称
1186
+ * @returns 找到的第一个实体或null
1181
1187
  */
1182
- get size(): number;
1188
+ findEntity(name: string): Entity | null;
1183
1189
  /**
1184
- * 获取组件类型
1190
+ * 根据名称查找所有实体
1191
+ * @param name 实体名称
1192
+ * @returns 找到的所有实体数组
1185
1193
  */
1186
- get type(): ComponentType<T>;
1194
+ findEntitiesByName(name: string): Entity[];
1187
1195
  /**
1188
- * 压缩存储(移除空洞)
1196
+ * 根据ID查找实体(使用索引,O(1)复杂度)
1197
+ * @param id 实体ID
1198
+ * @returns 找到的实体或null
1189
1199
  */
1190
- compact(): void;
1200
+ findEntityById(id: number): Entity | null;
1191
1201
  /**
1192
- * 获取存储统计信息
1202
+ * 根据标签查找实体
1203
+ * @param tag 标签
1204
+ * @returns 找到的所有实体数组
1193
1205
  */
1194
- getStats(): {
1195
- totalSlots: number;
1196
- usedSlots: number;
1197
- freeSlots: number;
1198
- fragmentation: number;
1199
- };
1200
- }
1201
- /**
1202
- * 组件存储管理器
1203
- * 管理所有组件类型的存储器
1204
- */
1205
- declare class ComponentStorageManager {
1206
- private storages;
1206
+ findEntitiesByTag(tag: number): Entity[];
1207
1207
  /**
1208
- * 获取或创建组件存储器
1208
+ * 根据组件类型查找实体
1209
1209
  * @param componentType 组件类型
1210
- * @returns 组件存储器
1210
+ * @returns 找到的所有实体数组
1211
1211
  */
1212
- getStorage<T extends Component>(componentType: ComponentType<T>): ComponentStorage<T>;
1212
+ findEntitiesWithComponent<T extends Component>(componentType: new (...args: unknown[]) => T): Entity[];
1213
1213
  /**
1214
- * 添加组件
1215
- * @param entityId 实体ID
1216
- * @param component 组件实例
1214
+ * 批量操作:对所有实体执行指定操作
1215
+ * @param action 要执行的操作
1217
1216
  */
1218
- addComponent<T extends Component>(entityId: number, component: T): void;
1217
+ forEach(action: (entity: Entity) => void): void;
1219
1218
  /**
1220
- * 获取组件
1221
- * @param entityId 实体ID
1222
- * @param componentType 组件类型
1223
- * @returns 组件实例或null
1219
+ * 批量操作:对符合条件的实体执行指定操作
1220
+ * @param predicate 筛选条件
1221
+ * @param action 要执行的操作
1224
1222
  */
1225
- getComponent<T extends Component>(entityId: number, componentType: ComponentType<T>): T | null;
1223
+ forEachWhere(predicate: (entity: Entity) => boolean, action: (entity: Entity) => void): void;
1226
1224
  /**
1227
- * 检查实体是否有组件
1228
- * @param entityId 实体ID
1229
- * @param componentType 组件类型
1230
- * @returns 是否有组件
1225
+ * 更新名称索引
1226
+ * @param entity 实体
1227
+ * @param isAdd 是否为添加操作
1231
1228
  */
1232
- hasComponent<T extends Component>(entityId: number, componentType: ComponentType<T>): boolean;
1229
+ private updateNameIndex;
1233
1230
  /**
1234
- * 移除组件
1235
- * @param entityId 实体ID
1236
- * @param componentType 组件类型
1237
- * @returns 被移除的组件或null
1231
+ * 获取实体列表的统计信息
1232
+ * @returns 统计信息
1238
1233
  */
1239
- removeComponent<T extends Component>(entityId: number, componentType: ComponentType<T>): T | null;
1234
+ getStats(): {
1235
+ totalEntities: number;
1236
+ activeEntities: number;
1237
+ pendingAdd: number;
1238
+ pendingRemove: number;
1239
+ nameIndexSize: number;
1240
+ };
1241
+ }
1242
+
1243
+ /**
1244
+ * 事件处理器函数类型
1245
+ */
1246
+ type EventHandler$1<T = any> = (event: T) => void;
1247
+ /**
1248
+ * 异步事件处理器函数类型
1249
+ */
1250
+ type AsyncEventHandler$1<T = any> = (event: T) => Promise<void>;
1251
+ /**
1252
+ * 事件监听器配置
1253
+ */
1254
+ interface EventListenerConfig {
1255
+ /** 是否只执行一次 */
1256
+ once?: boolean;
1257
+ /** 优先级(数字越大优先级越高) */
1258
+ priority?: number;
1259
+ /** 是否异步执行 */
1260
+ async?: boolean;
1261
+ /** 执行上下文 */
1262
+ context?: any;
1263
+ }
1264
+ /**
1265
+ * 事件统计信息
1266
+ */
1267
+ interface EventStats {
1268
+ /** 事件类型 */
1269
+ eventType: string;
1270
+ /** 监听器数量 */
1271
+ listenerCount: number;
1272
+ /** 触发次数 */
1273
+ triggerCount: number;
1274
+ /** 总执行时间(毫秒) */
1275
+ totalExecutionTime: number;
1276
+ /** 平均执行时间(毫秒) */
1277
+ averageExecutionTime: number;
1278
+ /** 最后触发时间 */
1279
+ lastTriggerTime: number;
1280
+ }
1281
+ /**
1282
+ * 事件批处理配置
1283
+ */
1284
+ interface EventBatchConfig {
1285
+ /** 批处理大小 */
1286
+ batchSize: number;
1287
+ /** 批处理延迟(毫秒) */
1288
+ delay: number;
1289
+ /** 是否启用批处理 */
1290
+ enabled: boolean;
1291
+ }
1292
+ /**
1293
+ * 类型安全的高性能事件系统
1294
+ * 支持同步/异步事件、优先级、批处理等功能
1295
+ */
1296
+ declare class TypeSafeEventSystem {
1297
+ private listeners;
1298
+ private stats;
1299
+ private batchQueue;
1300
+ private batchTimers;
1301
+ private batchConfigs;
1302
+ private nextListenerId;
1303
+ private isEnabled;
1304
+ private maxListeners;
1240
1305
  /**
1241
- * 移除实体的所有组件
1242
- * @param entityId 实体ID
1306
+ * 添加事件监听器
1307
+ * @param eventType 事件类型
1308
+ * @param handler 事件处理器
1309
+ * @param config 监听器配置
1310
+ * @returns 监听器ID(用于移除)
1243
1311
  */
1244
- removeAllComponents(entityId: number): void;
1312
+ on<T>(eventType: string, handler: EventHandler$1<T>, config?: EventListenerConfig): string;
1245
1313
  /**
1246
- * 获取实体的组件位掩码
1247
- * @param entityId 实体ID
1248
- * @returns 组件位掩码
1314
+ * 添加一次性事件监听器
1315
+ * @param eventType 事件类型
1316
+ * @param handler 事件处理器
1317
+ * @param config 监听器配置
1318
+ * @returns 监听器ID
1249
1319
  */
1250
- getComponentMask(entityId: number): bigint;
1320
+ once<T>(eventType: string, handler: EventHandler$1<T>, config?: EventListenerConfig): string;
1251
1321
  /**
1252
- * 压缩所有存储器
1322
+ * 添加异步事件监听器
1323
+ * @param eventType 事件类型
1324
+ * @param handler 异步事件处理器
1325
+ * @param config 监听器配置
1326
+ * @returns 监听器ID
1253
1327
  */
1254
- compactAll(): void;
1328
+ onAsync<T>(eventType: string, handler: AsyncEventHandler$1<T>, config?: EventListenerConfig): string;
1255
1329
  /**
1256
- * 获取所有存储器的统计信息
1330
+ * 移除事件监听器
1331
+ * @param eventType 事件类型
1332
+ * @param listenerId 监听器ID
1333
+ * @returns 是否成功移除
1257
1334
  */
1258
- getAllStats(): Map<string, any>;
1335
+ off(eventType: string, listenerId: string): boolean;
1259
1336
  /**
1260
- * 清空所有存储器
1337
+ * 移除指定事件类型的所有监听器
1338
+ * @param eventType 事件类型
1261
1339
  */
1262
- clear(): void;
1263
- }
1264
-
1265
- /**
1266
- * 增强的事件总线实现
1267
- * 基于TypeSafeEventSystem,提供类型安全的事件发布订阅机制
1268
- */
1269
- declare class EventBus implements IEventBus {
1270
- private eventSystem;
1271
- private eventIdCounter;
1272
- private isDebugMode;
1273
- constructor(debugMode?: boolean);
1340
+ offAll(eventType: string): void;
1274
1341
  /**
1275
- * 发射事件
1342
+ * 触发事件
1276
1343
  * @param eventType 事件类型
1277
- * @param data 事件数据
1344
+ * @param event 事件数据
1345
+ * @returns Promise(如果有异步监听器)
1278
1346
  */
1279
- emit<T>(eventType: string, data: T): void;
1347
+ emit<T>(eventType: string, event: T): Promise<void>;
1280
1348
  /**
1281
- * 异步发射事件
1349
+ * 同步触发事件(忽略异步监听器)
1282
1350
  * @param eventType 事件类型
1283
- * @param data 事件数据
1351
+ * @param event 事件数据
1284
1352
  */
1285
- emitAsync<T>(eventType: string, data: T): Promise<void>;
1353
+ emitSync<T>(eventType: string, event: T): void;
1286
1354
  /**
1287
- * 监听事件
1355
+ * 设置事件批处理配置
1288
1356
  * @param eventType 事件类型
1289
- * @param handler 事件处理器
1290
- * @param config 监听器配置
1291
- * @returns 监听器ID
1357
+ * @param config 批处理配置
1292
1358
  */
1293
- on<T>(eventType: string, handler: (data: T) => void, config?: IEventListenerConfig): string;
1359
+ setBatchConfig(eventType: string, config: EventBatchConfig): void;
1294
1360
  /**
1295
- * 监听事件(一次性)
1361
+ * 立即处理指定事件类型的批处理队列
1296
1362
  * @param eventType 事件类型
1297
- * @param handler 事件处理器
1298
- * @param config 监听器配置
1299
- * @returns 监听器ID
1300
1363
  */
1301
- once<T>(eventType: string, handler: (data: T) => void, config?: IEventListenerConfig): string;
1364
+ flushBatch(eventType: string): void;
1302
1365
  /**
1303
- * 异步监听事件
1304
- * @param eventType 事件类型
1305
- * @param handler 异步事件处理器
1306
- * @param config 监听器配置
1307
- * @returns 监听器ID
1366
+ * 获取事件统计信息
1367
+ * @param eventType 事件类型(可选)
1368
+ * @returns 统计信息
1308
1369
  */
1309
- onAsync<T>(eventType: string, handler: (data: T) => Promise<void>, config?: IEventListenerConfig): string;
1370
+ getStats(eventType?: string): EventStats | Map<string, EventStats>;
1310
1371
  /**
1311
- * 移除事件监听器
1312
- * @param eventType 事件类型
1313
- * @param listenerId 监听器ID
1372
+ * 重置统计信息
1373
+ * @param eventType 事件类型(可选,不指定则重置所有)
1314
1374
  */
1315
- off(eventType: string, listenerId: string): boolean;
1375
+ resetStats(eventType?: string): void;
1316
1376
  /**
1317
- * 移除指定事件类型的所有监听器
1318
- * @param eventType 事件类型
1377
+ * 启用/禁用事件系统
1378
+ * @param enabled 是否启用
1319
1379
  */
1320
- offAll(eventType: string): void;
1380
+ setEnabled(enabled: boolean): void;
1321
1381
  /**
1322
- * 检查是否有指定事件的监听器
1382
+ * 检查是否有指定事件类型的监听器
1323
1383
  * @param eventType 事件类型
1384
+ * @returns 是否有监听器
1324
1385
  */
1325
1386
  hasListeners(eventType: string): boolean;
1326
1387
  /**
1327
- * 获取事件统计信息
1328
- * @param eventType 事件类型(可选)
1388
+ * 获取指定事件类型的监听器数量
1389
+ * @param eventType 事件类型
1390
+ * @returns 监听器数量
1329
1391
  */
1330
- getStats(eventType?: string): IEventStats | Map<string, IEventStats>;
1392
+ getListenerCount(eventType: string): number;
1331
1393
  /**
1332
- * 清空所有监听器
1394
+ * 清空所有事件监听器和数据
1333
1395
  */
1334
1396
  clear(): void;
1335
1397
  /**
1336
- * 启用或禁用事件系统
1337
- * @param enabled 是否启用
1398
+ * 设置每个事件类型的最大监听器数量
1399
+ * @param max 最大数量
1338
1400
  */
1339
- setEnabled(enabled: boolean): void;
1401
+ setMaxListeners(max: number): void;
1340
1402
  /**
1341
- * 设置调试模式
1342
- * @param debug 是否启用调试
1403
+ * 添加监听器的内部实现
1404
+ * @param eventType 事件类型
1405
+ * @param handler 事件处理器
1406
+ * @param config 配置
1407
+ * @returns 监听器ID
1343
1408
  */
1344
- setDebugMode(debug: boolean): void;
1409
+ private addListener;
1345
1410
  /**
1346
- * 设置最大监听器数量
1347
- * @param max 最大数量
1411
+ * 执行事件的内部实现
1412
+ * @param eventType 事件类型
1413
+ * @param event 事件数据
1348
1414
  */
1349
- setMaxListeners(max: number): void;
1415
+ private executeEvent;
1350
1416
  /**
1351
- * 获取监听器数量
1352
- * @param eventType 事件类型
1417
+ * 按优先级排序监听器
1418
+ * @param listeners 监听器数组
1419
+ * @returns 排序后的监听器数组
1353
1420
  */
1354
- getListenerCount(eventType: string): number;
1421
+ private sortListenersByPriority;
1355
1422
  /**
1356
- * 设置事件批处理配置
1423
+ * 移除指定的监听器
1357
1424
  * @param eventType 事件类型
1358
- * @param batchSize 批处理大小
1359
- * @param delay 延迟时间(毫秒)
1425
+ * @param listenerIds 要移除的监听器ID数组
1360
1426
  */
1361
- setBatchConfig(eventType: string, batchSize: number, delay: number): void;
1427
+ private removeListeners;
1362
1428
  /**
1363
- * 刷新指定事件的批处理队列
1429
+ * 添加事件到批处理队列
1364
1430
  * @param eventType 事件类型
1431
+ * @param event 事件数据
1365
1432
  */
1366
- flushBatch(eventType: string): void;
1433
+ private addToBatch;
1367
1434
  /**
1368
- * 重置事件统计
1369
- * @param eventType 事件类型(可选)
1435
+ * 处理批处理事件
1436
+ * @param eventType 事件类型
1437
+ * @param batch 批处理事件数组
1370
1438
  */
1371
- resetStats(eventType?: string): void;
1439
+ private processBatch;
1372
1440
  /**
1373
- * 发射实体创建事件
1374
- * @param entityData 实体事件数据
1441
+ * 清除指定事件类型的批处理
1442
+ * @param eventType 事件类型
1375
1443
  */
1376
- emitEntityCreated(entityData: IEntityEventData): void;
1444
+ private clearBatch;
1377
1445
  /**
1378
- * 发射实体销毁事件
1379
- * @param entityData 实体事件数据
1446
+ * 清除所有批处理
1380
1447
  */
1381
- emitEntityDestroyed(entityData: IEntityEventData): void;
1448
+ private clearAllBatches;
1382
1449
  /**
1383
- * 发射组件添加事件
1384
- * @param componentData 组件事件数据
1450
+ * 更新事件统计信息
1451
+ * @param eventType 事件类型
1452
+ * @param executionTime 执行时间
1385
1453
  */
1386
- emitComponentAdded(componentData: IComponentEventData): void;
1454
+ private updateStats;
1387
1455
  /**
1388
- * 发射组件移除事件
1389
- * @param componentData 组件事件数据
1456
+ * 创建空的统计信息
1457
+ * @param eventType 事件类型
1458
+ * @returns 空的统计信息
1390
1459
  */
1391
- emitComponentRemoved(componentData: IComponentEventData): void;
1460
+ private createEmptyStats;
1461
+ }
1462
+
1463
+ /**
1464
+ * BigInt兼容性抽象层
1465
+ *
1466
+ * 为不支持BigInt的环境提供兼容实现,确保ECS框架在所有平台上都能正常运行。
1467
+ * 自动检测运行时环境的BigInt支持情况,并提供统一的接口。
1468
+ *
1469
+ * @example
1470
+ * ```typescript
1471
+ * // 创建兼容的BigInt值
1472
+ * const value = BigIntFactory.create(123);
1473
+ *
1474
+ * // 位运算
1475
+ * const result = value.or(BigIntFactory.create(456));
1476
+ *
1477
+ * // 检查兼容性
1478
+ * console.log(BigIntFactory.isNativeSupported()); // true/false
1479
+ * ```
1480
+ */
1481
+ /**
1482
+ * BigInt兼容接口
1483
+ *
1484
+ * 定义了BigInt的基本操作接口,支持原生BigInt和兼容实现的统一调用。
1485
+ */
1486
+ interface IBigIntLike {
1392
1487
  /**
1393
- * 发射系统添加事件
1394
- * @param systemData 系统事件数据
1488
+ * 获取数值表示
1489
+ * @returns 数值
1395
1490
  */
1396
- emitSystemAdded(systemData: ISystemEventData): void;
1491
+ valueOf(): number;
1397
1492
  /**
1398
- * 发射系统移除事件
1399
- * @param systemData 系统事件数据
1493
+ * 转换为字符串
1494
+ * @param radix 进制,支持2、10、16
1495
+ * @returns 字符串表示
1400
1496
  */
1401
- emitSystemRemoved(systemData: ISystemEventData): void;
1497
+ toString(radix?: number): string;
1402
1498
  /**
1403
- * 发射场景变化事件
1404
- * @param sceneData 场景事件数据
1499
+ * 位运算:与
1500
+ * @param other 另一个BigInt值
1501
+ * @returns 运算结果
1405
1502
  */
1406
- emitSceneChanged(sceneData: ISceneEventData): void;
1503
+ and(other: IBigIntLike): IBigIntLike;
1407
1504
  /**
1408
- * 发射性能警告事件
1409
- * @param performanceData 性能事件数据
1505
+ * 位运算:或
1506
+ * @param other 另一个BigInt值
1507
+ * @returns 运算结果
1410
1508
  */
1411
- emitPerformanceWarning(performanceData: IPerformanceEventData): void;
1509
+ or(other: IBigIntLike): IBigIntLike;
1412
1510
  /**
1413
- * 监听实体创建事件
1414
- * @param handler 事件处理器
1415
- * @param config 监听器配置
1511
+ * 位运算:异或
1512
+ * @param other 另一个BigInt值
1513
+ * @returns 运算结果
1416
1514
  */
1417
- onEntityCreated(handler: (data: IEntityEventData) => void, config?: IEventListenerConfig): string;
1515
+ xor(other: IBigIntLike): IBigIntLike;
1418
1516
  /**
1419
- * 监听组件添加事件
1420
- * @param handler 事件处理器
1421
- * @param config 监听器配置
1517
+ * 位运算:非
1518
+ * @param maxBits 最大位数限制
1519
+ * @returns 运算结果
1422
1520
  */
1423
- onComponentAdded(handler: (data: IComponentEventData) => void, config?: IEventListenerConfig): string;
1521
+ not(maxBits?: number): IBigIntLike;
1424
1522
  /**
1425
- * 监听系统错误事件
1426
- * @param handler 事件处理器
1427
- * @param config 监听器配置
1523
+ * 左移位运算
1524
+ * @param bits 移位数
1525
+ * @returns 运算结果
1428
1526
  */
1429
- onSystemError(handler: (data: ISystemEventData) => void, config?: IEventListenerConfig): string;
1527
+ shiftLeft(bits: number): IBigIntLike;
1430
1528
  /**
1431
- * 监听性能警告事件
1432
- * @param handler 事件处理器
1433
- * @param config 监听器配置
1529
+ * 右移位运算
1530
+ * @param bits 移位数
1531
+ * @returns 运算结果
1434
1532
  */
1435
- onPerformanceWarning(handler: (data: IPerformanceEventData) => void, config?: IEventListenerConfig): string;
1533
+ shiftRight(bits: number): IBigIntLike;
1436
1534
  /**
1437
- * 验证事件类型
1438
- * @param eventType 事件类型
1535
+ * 相等比较
1536
+ * @param other 另一个BigInt值
1537
+ * @returns 是否相等
1439
1538
  */
1440
- private validateEventType;
1539
+ equals(other: IBigIntLike): boolean;
1441
1540
  /**
1442
- * 增强事件数据
1443
- * @param eventType 事件类型
1444
- * @param data 原始数据
1541
+ * 检查是否为零
1542
+ * @returns 是否为零
1445
1543
  */
1446
- private enhanceEventData;
1544
+ isZero(): boolean;
1447
1545
  /**
1448
- * 转换EventStats为IEventStats
1449
- * @param stats EventStats实例
1546
+ * 创建副本
1547
+ * @returns 新的实例
1450
1548
  */
1451
- private convertEventStats;
1549
+ clone(): IBigIntLike;
1452
1550
  }
1453
1551
  /**
1454
- * 全局事件总线实例
1455
- * 提供全局访问的事件总线
1552
+ * 环境信息接口
1456
1553
  */
1457
- declare class GlobalEventBus {
1458
- private static instance;
1554
+ interface EnvironmentInfo {
1555
+ /** 是否支持BigInt */
1556
+ supportsBigInt: boolean;
1557
+ /** 运行环境 */
1558
+ environment: string;
1559
+ /** JavaScript引擎 */
1560
+ jsEngine: string;
1561
+ }
1562
+
1563
+ /**
1564
+ * SoA存储器(需要装饰器启用)
1565
+ * 使用Structure of Arrays存储模式,在大规模批量操作时提供优异性能
1566
+ */
1567
+ declare class SoAStorage<T extends Component> {
1568
+ private fields;
1569
+ private stringFields;
1570
+ private serializedFields;
1571
+ private complexFields;
1572
+ private entityToIndex;
1573
+ private indexToEntity;
1574
+ private freeIndices;
1575
+ private _size;
1576
+ private _capacity;
1577
+ readonly type: ComponentType<T>;
1578
+ constructor(componentType: ComponentType<T>);
1579
+ private initializeFields;
1580
+ addComponent(entityId: number, component: T): void;
1581
+ private updateComponentAtIndex;
1459
1582
  /**
1460
- * 获取全局事件总线实例
1461
- * @param debugMode 是否启用调试模式
1583
+ * 序列化值为JSON字符串
1462
1584
  */
1463
- static getInstance(debugMode?: boolean): EventBus;
1585
+ private serializeValue;
1464
1586
  /**
1465
- * 重置全局事件总线实例
1466
- * @param debugMode 是否启用调试模式
1587
+ * 反序列化JSON字符串为值
1467
1588
  */
1468
- static reset(debugMode?: boolean): EventBus;
1589
+ private deserializeValue;
1590
+ /**
1591
+ * 深拷贝对象
1592
+ */
1593
+ private deepClone;
1594
+ getComponent(entityId: number): T | null;
1595
+ private getFieldType;
1596
+ hasComponent(entityId: number): boolean;
1597
+ removeComponent(entityId: number): T | null;
1598
+ private resize;
1599
+ getActiveIndices(): number[];
1600
+ getFieldArray(fieldName: string): Float32Array | Float64Array | Int32Array | null;
1601
+ size(): number;
1602
+ clear(): void;
1603
+ compact(): void;
1604
+ getStats(): any;
1605
+ /**
1606
+ * 执行向量化批量操作
1607
+ * @param operation 操作函数,接收字段数组和活跃索引
1608
+ */
1609
+ performVectorizedOperation(operation: (fieldArrays: Map<string, Float32Array | Float64Array | Int32Array>, activeIndices: number[]) => void): void;
1469
1610
  }
1611
+
1470
1612
  /**
1471
- * 事件装饰器工厂
1472
- * 用于自动注册事件监听器
1473
- */
1474
- declare function EventHandler$1(eventType: string, config?: IEventListenerConfig): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
1475
- /**
1476
- * 异步事件装饰器工厂
1477
- * 用于自动注册异步事件监听器
1613
+ * 组件类型定义
1478
1614
  */
1479
- declare function AsyncEventHandler$1(eventType: string, config?: IEventListenerConfig): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
1480
-
1615
+ type ComponentType<T extends Component = Component> = new (...args: unknown[]) => T;
1481
1616
  /**
1482
- * 实体比较器
1483
- *
1484
- * 用于比较两个实体的优先级,首先按更新顺序比较,然后按ID比较。
1617
+ * 高性能组件存储器
1618
+ * 使用SoA(Structure of Arrays)模式存储组件
1485
1619
  */
1486
- declare class EntityComparer {
1620
+ declare class ComponentStorage<T extends Component> {
1621
+ private components;
1622
+ private entityToIndex;
1623
+ private indexToEntity;
1624
+ private freeIndices;
1625
+ private componentType;
1626
+ private _size;
1627
+ constructor(componentType: ComponentType<T>);
1487
1628
  /**
1488
- * 比较两个实体
1489
- *
1490
- * @param self - 第一个实体
1491
- * @param other - 第二个实体
1492
- * @returns 比较结果,负数表示self优先级更高,正数表示other优先级更高,0表示相等
1629
+ * 添加组件
1630
+ * @param entityId 实体ID
1631
+ * @param component 组件实例
1493
1632
  */
1494
- compare(self: Entity, other: Entity): number;
1495
- }
1496
- /**
1497
- * 游戏实体类
1498
- *
1499
- * ECS架构中的实体(Entity),作为组件的容器。
1500
- * 实体本身不包含游戏逻辑,所有功能都通过组件来实现。
1501
- * 支持父子关系,可以构建实体层次结构。
1502
- *
1503
- * @example
1504
- * ```typescript
1505
- * // 创建实体
1506
- * const entity = new Entity("Player", 1);
1507
- *
1508
- * // 添加组件
1509
- * const healthComponent = entity.addComponent(new HealthComponent(100));
1510
- *
1511
- * // 获取组件
1512
- * const health = entity.getComponent(HealthComponent);
1513
- *
1514
- * // 添加位置组件
1515
- * entity.addComponent(new PositionComponent(100, 200));
1516
- *
1517
- * // 添加子实体
1518
- * const weapon = new Entity("Weapon", 2);
1519
- * entity.addChild(weapon);
1520
- * ```
1521
- */
1522
- declare class Entity {
1633
+ addComponent(entityId: number, component: T): void;
1523
1634
  /**
1524
- * 实体比较器实例
1635
+ * 获取组件
1636
+ * @param entityId 实体ID
1637
+ * @returns 组件实例或null
1525
1638
  */
1526
- static entityComparer: EntityComparer;
1639
+ getComponent(entityId: number): T | null;
1527
1640
  /**
1528
- * 全局事件总线实例
1529
- * 用于发射组件相关事件
1641
+ * 检查实体是否有此组件
1642
+ * @param entityId 实体ID
1643
+ * @returns 是否有组件
1530
1644
  */
1531
- static eventBus: EventBus | null;
1645
+ hasComponent(entityId: number): boolean;
1532
1646
  /**
1533
- * 实体名称
1534
- *
1535
- * 用于标识和调试的友好名称。
1647
+ * 移除组件
1648
+ * @param entityId 实体ID
1649
+ * @returns 被移除的组件或null
1536
1650
  */
1537
- name: string;
1651
+ removeComponent(entityId: number): T | null;
1538
1652
  /**
1539
- * 实体唯一标识符
1540
- *
1541
- * 在场景中唯一的数字标识符。
1653
+ * 高效遍历所有组件
1654
+ * @param callback 回调函数
1542
1655
  */
1543
- readonly id: number;
1656
+ forEach(callback: (component: T, entityId: number, index: number) => void): void;
1544
1657
  /**
1545
- * 组件集合
1546
- *
1547
- * 存储实体拥有的所有组件。
1658
+ * 获取所有组件(密集数组)
1659
+ * @returns 组件数组
1548
1660
  */
1549
- readonly components: Component[];
1661
+ getDenseArray(): {
1662
+ components: T[];
1663
+ entityIds: number[];
1664
+ };
1550
1665
  /**
1551
- * 所属场景引用
1552
- *
1553
- * 指向实体所在的场景实例。
1666
+ * 清空所有组件
1554
1667
  */
1555
- scene: any;
1668
+ clear(): void;
1556
1669
  /**
1557
- * 更新间隔
1558
- *
1559
- * 控制实体更新的频率,值越大更新越不频繁。
1670
+ * 获取组件数量
1560
1671
  */
1561
- updateInterval: number;
1672
+ get size(): number;
1562
1673
  /**
1563
- * 销毁状态标志
1564
- *
1565
- * 标记实体是否已被销毁。
1674
+ * 获取组件类型
1566
1675
  */
1567
- _isDestroyed: boolean;
1676
+ get type(): ComponentType<T>;
1568
1677
  /**
1569
- * 父实体引用
1570
- *
1571
- * 指向父级实体,用于构建实体层次结构。
1678
+ * 压缩存储(移除空洞)
1572
1679
  */
1573
- private _parent;
1680
+ compact(): void;
1574
1681
  /**
1575
- * 子实体集合
1576
- *
1577
- * 存储所有子级实体的数组。
1682
+ * 获取存储统计信息
1578
1683
  */
1579
- private _children;
1684
+ getStats(): {
1685
+ totalSlots: number;
1686
+ usedSlots: number;
1687
+ freeSlots: number;
1688
+ fragmentation: number;
1689
+ };
1690
+ }
1691
+ /**
1692
+ * 组件存储管理器
1693
+ * 管理所有组件类型的存储器
1694
+ */
1695
+ declare class ComponentStorageManager {
1696
+ private storages;
1580
1697
  /**
1581
- * 激活状态
1582
- *
1583
- * 控制实体是否处于激活状态。
1698
+ * 获取或创建组件存储器(默认原始存储)
1699
+ * @param componentType 组件类型
1700
+ * @returns 组件存储器
1584
1701
  */
1585
- private _active;
1702
+ getStorage<T extends Component>(componentType: ComponentType<T>): ComponentStorage<T> | SoAStorage<T>;
1586
1703
  /**
1587
- * 实体标签
1588
- *
1589
- * 用于分类和查询的数字标签。
1704
+ * 添加组件
1705
+ * @param entityId 实体ID
1706
+ * @param component 组件实例
1590
1707
  */
1591
- private _tag;
1708
+ addComponent<T extends Component>(entityId: number, component: T): void;
1592
1709
  /**
1593
- * 启用状态
1594
- *
1595
- * 控制实体是否启用更新和处理。
1710
+ * 获取组件
1711
+ * @param entityId 实体ID
1712
+ * @param componentType 组件类型
1713
+ * @returns 组件实例或null
1596
1714
  */
1597
- private _enabled;
1715
+ getComponent<T extends Component>(entityId: number, componentType: ComponentType<T>): T | null;
1598
1716
  /**
1599
- * 更新顺序
1600
- *
1601
- * 控制实体在系统中的更新优先级。
1717
+ * 检查实体是否有组件
1718
+ * @param entityId 实体ID
1719
+ * @param componentType 组件类型
1720
+ * @returns 是否有组件
1602
1721
  */
1603
- private _updateOrder;
1722
+ hasComponent<T extends Component>(entityId: number, componentType: ComponentType<T>): boolean;
1604
1723
  /**
1605
- * 组件位掩码
1606
- *
1607
- * 用于快速查询实体拥有的组件类型。
1724
+ * 移除组件
1725
+ * @param entityId 实体ID
1726
+ * @param componentType 组件类型
1727
+ * @returns 被移除的组件或null
1608
1728
  */
1609
- private _componentMask;
1729
+ removeComponent<T extends Component>(entityId: number, componentType: ComponentType<T>): T | null;
1610
1730
  /**
1611
- * 组件类型到索引的映射
1612
- *
1613
- * 用于快速定位组件在数组中的位置。
1731
+ * 移除实体的所有组件
1732
+ * @param entityId 实体ID
1614
1733
  */
1615
- private _componentTypeToIndex;
1734
+ removeAllComponents(entityId: number): void;
1616
1735
  /**
1617
- * 构造函数
1618
- *
1619
- * @param name - 实体名称
1620
- * @param id - 实体唯一标识符
1736
+ * 获取实体的组件位掩码
1737
+ * @param entityId 实体ID
1738
+ * @returns 组件位掩码
1739
+ */
1740
+ getComponentMask(entityId: number): IBigIntLike;
1741
+ /**
1742
+ * 压缩所有存储器
1743
+ */
1744
+ compactAll(): void;
1745
+ /**
1746
+ * 获取所有存储器的统计信息
1747
+ */
1748
+ getAllStats(): Map<string, any>;
1749
+ /**
1750
+ * 清空所有存储器
1751
+ */
1752
+ clear(): void;
1753
+ }
1754
+
1755
+ /**
1756
+ * 组件索引类型
1757
+ */
1758
+ declare enum IndexType {
1759
+ /** 哈希索引 - 最快查找 */
1760
+ HASH = "hash",
1761
+ /** 位图索引 - 内存高效 */
1762
+ BITMAP = "bitmap",
1763
+ /** 排序索引 - 支持范围查询 */
1764
+ SORTED = "sorted"
1765
+ }
1766
+ /**
1767
+ * 索引统计信息
1768
+ */
1769
+ interface IndexStats {
1770
+ /** 索引类型 */
1771
+ type: IndexType;
1772
+ /** 索引大小 */
1773
+ size: number;
1774
+ /** 内存使用量(字节) */
1775
+ memoryUsage: number;
1776
+ /** 查询次数 */
1777
+ queryCount: number;
1778
+ /** 平均查询时间(毫秒) */
1779
+ avgQueryTime: number;
1780
+ /** 最后更新时间 */
1781
+ lastUpdated: number;
1782
+ }
1783
+ /**
1784
+ * 组件索引接口
1785
+ */
1786
+ interface IComponentIndex {
1787
+ /** 索引类型 */
1788
+ readonly type: IndexType;
1789
+ /** 添加实体到索引 */
1790
+ addEntity(entity: Entity): void;
1791
+ /** 从索引中移除实体 */
1792
+ removeEntity(entity: Entity): void;
1793
+ /** 查询包含指定组件的实体 */
1794
+ query(componentType: ComponentType): Set<Entity>;
1795
+ /** 批量查询多个组件 */
1796
+ queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
1797
+ /** 清空索引 */
1798
+ clear(): void;
1799
+ /** 获取索引统计信息 */
1800
+ getStats(): IndexStats;
1801
+ }
1802
+ /**
1803
+ * 哈希索引实现
1804
+ *
1805
+ * 使用Map数据结构,提供O(1)的查找性能。
1806
+ * 适合大多数查询场景。
1807
+ */
1808
+ declare class HashComponentIndex implements IComponentIndex {
1809
+ readonly type = IndexType.HASH;
1810
+ private _componentToEntities;
1811
+ private _entityToComponents;
1812
+ private _queryCount;
1813
+ private _totalQueryTime;
1814
+ private _lastUpdated;
1815
+ private _setPool;
1816
+ private _componentTypeSetPool;
1817
+ addEntity(entity: Entity): void;
1818
+ removeEntity(entity: Entity): void;
1819
+ query(componentType: ComponentType): Set<Entity>;
1820
+ queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
1821
+ clear(): void;
1822
+ getStats(): IndexStats;
1823
+ }
1824
+ /**
1825
+ * 位图索引实现
1826
+ *
1827
+ * 使用位操作进行快速集合运算,内存效率高。
1828
+ * 适合有限组件类型和大量实体的场景。
1829
+ */
1830
+ declare class BitmapComponentIndex implements IComponentIndex {
1831
+ readonly type = IndexType.BITMAP;
1832
+ private _componentTypeToBit;
1833
+ private _entityToBitmap;
1834
+ private _bitToEntities;
1835
+ private _nextBit;
1836
+ private _queryCount;
1837
+ private _totalQueryTime;
1838
+ private _lastUpdated;
1839
+ addEntity(entity: Entity): void;
1840
+ removeEntity(entity: Entity): void;
1841
+ query(componentType: ComponentType): Set<Entity>;
1842
+ queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
1843
+ clear(): void;
1844
+ getStats(): IndexStats;
1845
+ }
1846
+ /**
1847
+ * 智能组件索引管理器
1848
+ *
1849
+ * 根据使用模式自动选择最优的索引策略。
1850
+ * 支持动态切换索引类型以获得最佳性能。
1851
+ */
1852
+ declare class ComponentIndexManager {
1853
+ private _activeIndex;
1854
+ private _indexHistory;
1855
+ private _autoOptimize;
1856
+ private _optimizationThreshold;
1857
+ constructor(initialType?: IndexType);
1858
+ /**
1859
+ * 添加实体到索引
1860
+ */
1861
+ addEntity(entity: Entity): void;
1862
+ /**
1863
+ * 从索引中移除实体
1864
+ */
1865
+ removeEntity(entity: Entity): void;
1866
+ /**
1867
+ * 查询包含指定组件的实体
1868
+ */
1869
+ query(componentType: ComponentType): Set<Entity>;
1870
+ /**
1871
+ * 批量查询多个组件
1621
1872
  */
1622
- constructor(name: string, id: number);
1873
+ queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
1623
1874
  /**
1624
- * 获取销毁状态
1625
- *
1626
- * @returns 如果实体已被销毁则返回true
1875
+ * 手动切换索引类型
1627
1876
  */
1628
- get isDestroyed(): boolean;
1877
+ switchIndexType(type: IndexType): void;
1629
1878
  /**
1630
- * 获取父实体
1631
- *
1632
- * @returns 父实体,如果没有父实体则返回null
1879
+ * 启用/禁用自动优化
1633
1880
  */
1634
- get parent(): Entity | null;
1881
+ setAutoOptimize(enabled: boolean): void;
1635
1882
  /**
1636
- * 获取子实体数组的只读副本
1637
- *
1638
- * @returns 子实体数组的副本
1883
+ * 获取当前索引统计信息
1639
1884
  */
1640
- get children(): readonly Entity[];
1885
+ getStats(): IndexStats;
1641
1886
  /**
1642
- * 获取子实体数量
1643
- *
1644
- * @returns 子实体的数量
1887
+ * 获取所有索引类型的历史统计信息
1645
1888
  */
1646
- get childCount(): number;
1889
+ getAllStats(): Map<IndexType, IndexStats>;
1647
1890
  /**
1648
- * 获取活跃状态
1649
- *
1650
- * @returns 如果实体处于活跃状态则返回true
1891
+ * 清空索引
1651
1892
  */
1652
- get active(): boolean;
1893
+ clear(): void;
1653
1894
  /**
1654
- * 设置活跃状态
1655
- *
1656
- * 设置实体的活跃状态,会影响子实体的有效活跃状态。
1657
- *
1658
- * @param value - 新的活跃状态
1895
+ * 创建指定类型的索引
1659
1896
  */
1660
- set active(value: boolean);
1897
+ private createIndex;
1661
1898
  /**
1662
- * 获取实体的有效活跃状态
1663
- *
1664
- * 考虑父实体的活跃状态,只有当实体本身和所有父实体都处于活跃状态时才返回true。
1665
- *
1666
- * @returns 有效的活跃状态
1899
+ * 检查是否需要优化索引
1667
1900
  */
1668
- get activeInHierarchy(): boolean;
1901
+ private checkOptimization;
1902
+ }
1903
+
1904
+ /**
1905
+ * 原型标识符
1906
+ */
1907
+ type ArchetypeId = string;
1908
+ /**
1909
+ * 原型数据结构
1910
+ */
1911
+ interface Archetype {
1912
+ /** 原型唯一标识符 */
1913
+ id: ArchetypeId;
1914
+ /** 包含的组件类型 */
1915
+ componentTypes: ComponentType[];
1916
+ /** 属于该原型的实体列表 */
1917
+ entities: Entity[];
1918
+ /** 原型创建时间 */
1919
+ createdAt: number;
1920
+ /** 最后更新时间 */
1921
+ updatedAt: number;
1922
+ }
1923
+ /**
1924
+ * 原型查询结果
1925
+ */
1926
+ interface ArchetypeQueryResult {
1927
+ /** 匹配的原型列表 */
1928
+ archetypes: Archetype[];
1929
+ /** 所有匹配实体的总数 */
1930
+ totalEntities: number;
1931
+ /** 查询执行时间(毫秒) */
1932
+ executionTime: number;
1933
+ /** 是否使用了缓存 */
1934
+ fromCache: boolean;
1935
+ }
1936
+ /**
1937
+ * Archetype系统
1938
+ *
1939
+ * 根据实体的组件组合将实体分组到不同的原型中,提供高效的查询性能。
1940
+ */
1941
+ declare class ArchetypeSystem {
1942
+ /** 所有原型的映射表 */
1943
+ private _archetypes;
1944
+ /** 实体到原型的映射 */
1945
+ private _entityToArchetype;
1946
+ /** 组件类型到原型的映射 */
1947
+ private _componentToArchetypes;
1948
+ /** 查询缓存 */
1949
+ private _queryCache;
1950
+ private _cacheTimeout;
1951
+ private _maxCacheSize;
1669
1952
  /**
1670
- * 获取实体标签
1671
- *
1672
- * @returns 实体的数字标签
1953
+ * 添加实体到原型系统
1673
1954
  */
1674
- get tag(): number;
1955
+ addEntity(entity: Entity): void;
1675
1956
  /**
1676
- * 设置实体标签
1677
- *
1678
- * @param value - 新的标签值
1957
+ * 从原型系统中移除实体
1679
1958
  */
1680
- set tag(value: number);
1959
+ removeEntity(entity: Entity): void;
1681
1960
  /**
1682
- * 获取启用状态
1683
- *
1684
- * @returns 如果实体已启用则返回true
1961
+ * 查询包含指定组件组合的原型
1685
1962
  */
1686
- get enabled(): boolean;
1963
+ queryArchetypes(componentTypes: ComponentType[], operation?: 'AND' | 'OR'): ArchetypeQueryResult;
1687
1964
  /**
1688
- * 设置启用状态
1689
- *
1690
- * @param value - 新的启用状态
1965
+ * 获取实体所属的原型
1691
1966
  */
1692
- set enabled(value: boolean);
1967
+ getEntityArchetype(entity: Entity): Archetype | undefined;
1693
1968
  /**
1694
- * 获取更新顺序
1695
- *
1696
- * @returns 实体的更新顺序值
1969
+ * 获取所有原型
1697
1970
  */
1698
- get updateOrder(): number;
1971
+ getAllArchetypes(): Archetype[];
1699
1972
  /**
1700
- * 设置更新顺序
1701
- *
1702
- * @param value - 新的更新顺序值
1973
+ * 清空所有数据
1703
1974
  */
1704
- set updateOrder(value: number);
1975
+ clear(): void;
1705
1976
  /**
1706
- * 获取组件位掩码
1707
- *
1708
- * @returns 实体的组件位掩码
1977
+ * 获取实体的组件类型列表
1709
1978
  */
1710
- get componentMask(): bigint;
1979
+ private getEntityComponentTypes;
1711
1980
  /**
1712
- * 创建并添加组件
1713
- *
1714
- * @param componentType - 组件类型
1715
- * @param args - 组件构造函数参数
1716
- * @returns 创建的组件实例
1981
+ * 生成原型ID
1717
1982
  */
1718
- createComponent<T extends Component>(componentType: ComponentType<T>, ...args: any[]): T;
1983
+ private generateArchetypeId;
1719
1984
  /**
1720
- * 内部添加组件方法(不进行重复检查,用于初始化)
1721
- *
1722
- * @param component - 要添加的组件实例
1723
- * @returns 添加的组件实例
1985
+ * 创建新原型
1724
1986
  */
1725
- private addComponentInternal;
1987
+ private createArchetype;
1726
1988
  /**
1727
- * 添加组件到实体
1728
- *
1729
- * @param component - 要添加的组件实例
1730
- * @returns 添加的组件实例
1731
- * @throws {Error} 如果组件类型已存在
1989
+ * 检查原型是否包含所有指定组件
1732
1990
  */
1733
- addComponent<T extends Component>(component: T): T;
1991
+ private archetypeContainsAllComponents;
1734
1992
  /**
1735
- * 获取指定类型的组件
1736
- *
1737
- * @param type - 组件类型
1738
- * @returns 组件实例或null
1993
+ * 更新组件索引
1739
1994
  */
1740
- getComponent<T extends Component>(type: ComponentType<T>): T | null;
1995
+ private updateComponentIndexes;
1741
1996
  /**
1742
- * 重建组件索引映射
1997
+ * 使查询缓存失效
1743
1998
  */
1744
- private rebuildComponentIndex;
1999
+ private invalidateQueryCache;
2000
+ }
2001
+
2002
+ /**
2003
+ * 实体查询结果接口
2004
+ */
2005
+ interface QueryResult {
2006
+ entities: Entity[];
2007
+ count: number;
2008
+ /** 查询执行时间(毫秒) */
2009
+ executionTime: number;
2010
+ /** 是否来自缓存 */
2011
+ fromCache: boolean;
2012
+ }
2013
+ /**
2014
+ * 高性能实体查询系统
2015
+ *
2016
+ * 提供快速的实体查询功能,支持按组件类型、标签、名称等多种方式查询实体。
2017
+ * 系统采用多级索引和智能缓存机制,确保在大量实体场景下的查询性能。
2018
+ *
2019
+ * 主要特性:
2020
+ * - 支持单组件和多组件查询
2021
+ * - 自动索引管理和缓存优化
2022
+ * - WebAssembly计算加速(如果可用)
2023
+ * - 详细的性能统计信息
2024
+ *
2025
+ * @example
2026
+ * ```typescript
2027
+ * // 查询所有包含Position和Velocity组件的实体
2028
+ * const movingEntities = querySystem.queryAll(PositionComponent, VelocityComponent);
2029
+ *
2030
+ * // 查询特定标签的实体
2031
+ * const playerEntities = querySystem.queryByTag(PLAYER_TAG);
2032
+ * ```
2033
+ */
2034
+ declare class QuerySystem {
2035
+ private entities;
2036
+ private entityIndex;
2037
+ private indexDirty;
2038
+ private _version;
2039
+ private queryCache;
2040
+ private cacheMaxSize;
2041
+ private cacheTimeout;
2042
+ private componentPoolManager;
2043
+ private bitMaskOptimizer;
2044
+ private indexUpdateBatcher;
2045
+ private componentIndexManager;
2046
+ private archetypeSystem;
2047
+ private dirtyTrackingSystem;
2048
+ private queryStats;
2049
+ constructor();
1745
2050
  /**
1746
- * 检查实体是否有指定类型的组件
2051
+ * 设置实体列表并重建索引
1747
2052
  *
1748
- * @param type - 组件类型
1749
- * @returns 如果有该组件则返回true
2053
+ * 当实体集合发生大规模变化时调用此方法。
2054
+ * 系统将重新构建所有索引以确保查询性能。
2055
+ *
2056
+ * @param entities 新的实体列表
1750
2057
  */
1751
- hasComponent<T extends Component>(type: ComponentType<T>): boolean;
2058
+ setEntities(entities: Entity[]): void;
1752
2059
  /**
1753
- * 获取或创建指定类型的组件
2060
+ * 添加单个实体到查询系统
1754
2061
  *
1755
- * @param type - 组件类型
1756
- * @param args - 组件构造函数参数(仅在创建时使用)
1757
- * @returns 组件实例
2062
+ * 将新实体添加到查询系统中,并自动更新相关索引。
2063
+ * 为了提高批量添加性能,可以延迟缓存清理。
2064
+ *
2065
+ * @param entity 要添加的实体
2066
+ * @param deferCacheClear 是否延迟缓存清理(用于批量操作)
1758
2067
  */
1759
- getOrCreateComponent<T extends Component>(type: ComponentType<T>, ...args: any[]): T;
2068
+ addEntity(entity: Entity, deferCacheClear?: boolean): void;
1760
2069
  /**
1761
- * 移除指定的组件
2070
+ * 批量添加实体
2071
+ *
2072
+ * 高效地批量添加多个实体,减少缓存清理次数。
2073
+ * 使用Set来避免O(n)的重复检查。
2074
+ *
2075
+ * @param entities 要添加的实体列表
2076
+ */
2077
+ addEntities(entities: Entity[]): void;
2078
+ /**
2079
+ * 批量添加实体(无重复检查版本)
2080
+ *
2081
+ * 假设所有实体都是新的,跳过重复检查以获得最大性能。
2082
+ * 仅在确保没有重复实体时使用。
1762
2083
  *
1763
- * @param component - 要移除的组件实例
2084
+ * @param entities 要添加的实体列表
1764
2085
  */
1765
- removeComponent(component: Component): void;
2086
+ addEntitiesUnchecked(entities: Entity[]): void;
1766
2087
  /**
1767
- * 移除指定类型的组件
2088
+ * 从查询系统移除实体
1768
2089
  *
1769
- * @param type - 组件类型
1770
- * @returns 被移除的组件实例或null
2090
+ * 从查询系统中移除指定实体,并清理相关索引。
2091
+ *
2092
+ * @param entity 要移除的实体
1771
2093
  */
1772
- removeComponentByType<T extends Component>(type: ComponentType<T>): T | null;
2094
+ removeEntity(entity: Entity): void;
1773
2095
  /**
1774
- * 移除所有组件
2096
+ * 将实体添加到各种索引中(优化版本)
1775
2097
  */
1776
- removeAllComponents(): void;
2098
+ private addEntityToIndexes;
1777
2099
  /**
1778
- * 批量添加组件
1779
- *
1780
- * @param components - 要添加的组件数组
1781
- * @returns 添加的组件数组
2100
+ * 从各种索引中移除实体
1782
2101
  */
1783
- addComponents<T extends Component>(components: T[]): T[];
2102
+ private removeEntityFromIndexes;
1784
2103
  /**
1785
- * 批量移除组件类型
2104
+ * 重建所有索引
1786
2105
  *
1787
- * @param componentTypes - 要移除的组件类型数组
1788
- * @returns 被移除的组件数组
2106
+ * 清空并重新构建所有查询索引。
2107
+ * 通常在大量实体变更后调用以确保索引一致性。
1789
2108
  */
1790
- removeComponentsByTypes<T extends Component>(componentTypes: ComponentType<T>[]): (T | null)[];
2109
+ private rebuildIndexes;
1791
2110
  /**
1792
- * 获取所有指定类型的组件
2111
+ * 查询包含所有指定组件的实体
1793
2112
  *
1794
- * @param type - 组件类型
1795
- * @returns 组件实例数组
1796
- */
1797
- getComponents<T extends Component>(type: ComponentType<T>): T[];
1798
- /**
1799
- * 添加子实体
2113
+ * 返回同时包含所有指定组件类型的实体列表。
2114
+ * 系统会自动选择最高效的查询策略,包括索引查找和缓存机制。
1800
2115
  *
1801
- * @param child - 要添加的子实体
1802
- * @returns 添加的子实体
1803
- */
1804
- addChild(child: Entity): Entity;
1805
- /**
1806
- * 移除子实体
2116
+ * @param componentTypes 要查询的组件类型列表
2117
+ * @returns 查询结果,包含匹配的实体和性能信息
1807
2118
  *
1808
- * @param child - 要移除的子实体
1809
- * @returns 是否成功移除
1810
- */
1811
- removeChild(child: Entity): boolean;
1812
- /**
1813
- * 移除所有子实体
2119
+ * @example
2120
+ * ```typescript
2121
+ * // 查询同时具有位置和速度组件的实体
2122
+ * const result = querySystem.queryAll(PositionComponent, VelocityComponent);
2123
+ * console.log(`找到 ${result.count} 个移动实体`);
2124
+ * ```
1814
2125
  */
1815
- removeAllChildren(): void;
2126
+ queryAll(...componentTypes: ComponentType[]): QueryResult;
1816
2127
  /**
1817
- * 根据名称查找子实体
2128
+ * 多组件查询算法
1818
2129
  *
1819
- * @param name - 子实体名称
1820
- * @param recursive - 是否递归查找
1821
- * @returns 找到的子实体或null
2130
+ * 针对多组件查询场景的高效算法实现。
2131
+ * 通过选择最小的组件集合作为起点,减少需要检查的实体数量。
2132
+ *
2133
+ * @param componentTypes 组件类型列表
2134
+ * @returns 匹配的实体列表
1822
2135
  */
1823
- findChild(name: string, recursive?: boolean): Entity | null;
2136
+ private queryMultipleComponents;
1824
2137
  /**
1825
- * 根据标签查找子实体
2138
+ * 查询包含任意指定组件的实体
1826
2139
  *
1827
- * @param tag - 标签
1828
- * @param recursive - 是否递归查找
1829
- * @returns 找到的子实体数组
2140
+ * 返回包含任意一个指定组件类型的实体列表。
2141
+ * 使用集合合并算法确保高效的查询性能。
2142
+ *
2143
+ * @param componentTypes 要查询的组件类型列表
2144
+ * @returns 查询结果,包含匹配的实体和性能信息
2145
+ *
2146
+ * @example
2147
+ * ```typescript
2148
+ * // 查询具有武器或护甲组件的实体
2149
+ * const result = querySystem.queryAny(WeaponComponent, ArmorComponent);
2150
+ * console.log(`找到 ${result.count} 个装备实体`);
2151
+ * ```
1830
2152
  */
1831
- findChildrenByTag(tag: number, recursive?: boolean): Entity[];
2153
+ queryAny(...componentTypes: ComponentType[]): QueryResult;
1832
2154
  /**
1833
- * 获取根实体
2155
+ * 查询不包含任何指定组件的实体
1834
2156
  *
1835
- * @returns 层次结构的根实体
2157
+ * 返回不包含任何指定组件类型的实体列表。
2158
+ * 适用于排除特定类型实体的查询场景。
2159
+ *
2160
+ * @param componentTypes 要排除的组件类型列表
2161
+ * @returns 查询结果,包含匹配的实体和性能信息
2162
+ *
2163
+ * @example
2164
+ * ```typescript
2165
+ * // 查询不具有AI和玩家控制组件的实体(如静态物体)
2166
+ * const result = querySystem.queryNone(AIComponent, PlayerControlComponent);
2167
+ * console.log(`找到 ${result.count} 个静态实体`);
2168
+ * ```
1836
2169
  */
1837
- getRoot(): Entity;
2170
+ queryNone(...componentTypes: ComponentType[]): QueryResult;
1838
2171
  /**
1839
- * 检查是否是指定实体的祖先
2172
+ * 按标签查询实体
1840
2173
  *
1841
- * @param entity - 要检查的实体
1842
- * @returns 如果是祖先则返回true
2174
+ * 返回具有指定标签的所有实体。
2175
+ * 标签查询使用专用索引,具有很高的查询性能。
2176
+ *
2177
+ * @param tag 要查询的标签值
2178
+ * @returns 查询结果,包含匹配的实体和性能信息
2179
+ *
2180
+ * @example
2181
+ * ```typescript
2182
+ * // 查询所有玩家实体
2183
+ * const players = querySystem.queryByTag(PLAYER_TAG);
2184
+ * ```
1843
2185
  */
1844
- isAncestorOf(entity: Entity): boolean;
2186
+ queryByTag(tag: number): QueryResult;
1845
2187
  /**
1846
- * 检查是否是指定实体的后代
2188
+ * 按名称查询实体
1847
2189
  *
1848
- * @param entity - 要检查的实体
1849
- * @returns 如果是后代则返回true
2190
+ * 返回具有指定名称的所有实体。
2191
+ * 名称查询使用专用索引,适用于查找特定的命名实体。
2192
+ *
2193
+ * @param name 要查询的实体名称
2194
+ * @returns 查询结果,包含匹配的实体和性能信息
2195
+ *
2196
+ * @example
2197
+ * ```typescript
2198
+ * // 查找名为"Player"的实体
2199
+ * const player = querySystem.queryByName("Player");
2200
+ * ```
1850
2201
  */
1851
- isDescendantOf(entity: Entity): boolean;
2202
+ queryByName(name: string): QueryResult;
1852
2203
  /**
1853
- * 获取层次深度
2204
+ * 按单个组件类型查询实体
1854
2205
  *
1855
- * @returns 在层次结构中的深度(根实体为0)
2206
+ * 返回包含指定组件类型的所有实体。
2207
+ * 这是最基础的查询方法,具有最高的查询性能。
2208
+ *
2209
+ * @param componentType 要查询的组件类型
2210
+ * @returns 查询结果,包含匹配的实体和性能信息
2211
+ *
2212
+ * @example
2213
+ * ```typescript
2214
+ * // 查询所有具有位置组件的实体
2215
+ * const entitiesWithPosition = querySystem.queryByComponent(PositionComponent);
2216
+ * ```
1856
2217
  */
1857
- getDepth(): number;
2218
+ queryByComponent<T extends Component>(componentType: ComponentType<T>): QueryResult;
1858
2219
  /**
1859
- * 遍历所有子实体(深度优先)
1860
- *
1861
- * @param callback - 对每个子实体执行的回调函数
1862
- * @param recursive - 是否递归遍历
2220
+ * 从缓存获取查询结果
1863
2221
  */
1864
- forEachChild(callback: (child: Entity, index: number) => void, recursive?: boolean): void;
2222
+ private getFromCache;
1865
2223
  /**
1866
- * 活跃状态改变时的回调
2224
+ * 添加查询结果到缓存
1867
2225
  */
1868
- private onActiveChanged;
2226
+ private addToCache;
1869
2227
  /**
1870
- * 更新实体
1871
- *
1872
- * 调用所有组件的更新方法,并更新子实体。
2228
+ * 清理缓存
1873
2229
  */
1874
- update(): void;
2230
+ private cleanupCache;
1875
2231
  /**
1876
- * 销毁实体
1877
- *
1878
- * 移除所有组件、子实体并标记为已销毁。
2232
+ * 清除所有查询缓存
1879
2233
  */
1880
- destroy(): void;
2234
+ private clearQueryCache;
1881
2235
  /**
1882
- * 比较实体
2236
+ * 公共方法:清理查询缓存
1883
2237
  *
1884
- * @param other - 另一个实体
1885
- * @returns 比较结果
2238
+ * 用于外部调用清理缓存,通常在批量操作后使用。
1886
2239
  */
1887
- compareTo(other: Entity): number;
2240
+ clearCache(): void;
1888
2241
  /**
1889
- * 获取实体的字符串表示
2242
+ * 批量更新实体组件
1890
2243
  *
1891
- * @returns 实体的字符串描述
2244
+ * 对大量实体进行批量组件更新操作。
2245
+ *
2246
+ * @param updates 更新操作列表,包含实体ID和新的组件掩码
2247
+ *
2248
+ * @example
2249
+ * ```typescript
2250
+ * // 批量更新实体的组件配置
2251
+ * const updates = [
2252
+ * { entityId: 1, componentMask: BigInt(0b1011) },
2253
+ * { entityId: 2, componentMask: BigInt(0b1101) }
2254
+ * ];
2255
+ * querySystem.batchUpdateComponents(updates);
2256
+ * ```
1892
2257
  */
1893
- toString(): string;
2258
+ batchUpdateComponents(updates: Array<{
2259
+ entityId: number;
2260
+ componentMask: bigint;
2261
+ }>): void;
1894
2262
  /**
1895
- * 获取实体的调试信息(包含组件缓存信息)
2263
+ * 创建组件掩码
1896
2264
  *
1897
- * @returns 包含实体详细信息的对象
2265
+ * 根据组件类型列表生成对应的位掩码。
2266
+ * 使用位掩码优化器进行缓存和预计算。
2267
+ *
2268
+ * @param componentTypes 组件类型列表
2269
+ * @returns 生成的位掩码
1898
2270
  */
1899
- getDebugInfo(): {
1900
- name: string;
1901
- id: number;
1902
- enabled: boolean;
1903
- active: boolean;
1904
- activeInHierarchy: boolean;
1905
- destroyed: boolean;
1906
- componentCount: number;
1907
- componentTypes: string[];
1908
- componentMask: string;
1909
- parentId: number | null;
1910
- childCount: number;
1911
- childIds: number[];
1912
- depth: number;
1913
- indexMappingSize: number;
1914
- };
1915
- }
1916
-
1917
- /**
1918
- * 高性能实体列表管理器
1919
- * 管理场景中的所有实体,支持快速查找和批量操作
1920
- */
1921
- declare class EntityList {
1922
- buffer: Entity[];
1923
- private _scene;
1924
- private _idToEntity;
1925
- private _nameToEntities;
1926
- private _entitiesToAdd;
1927
- private _entitiesToRemove;
1928
- private _isUpdating;
1929
- get count(): number;
1930
- constructor(scene: any);
2271
+ private createComponentMask;
1931
2272
  /**
1932
- * 添加实体(立即添加或延迟添加)
1933
- * @param entity 要添加的实体
2273
+ * 获取当前版本号(用于缓存失效)
1934
2274
  */
1935
- add(entity: Entity): void;
2275
+ get version(): number;
1936
2276
  /**
1937
- * 立即添加实体
1938
- * @param entity 要添加的实体
2277
+ * 获取所有实体
1939
2278
  */
1940
- private addImmediate;
2279
+ getAllEntities(): Entity[];
1941
2280
  /**
1942
- * 移除实体(立即移除或延迟移除)
1943
- * @param entity 要移除的实体
2281
+ * 获取系统统计信息
2282
+ *
2283
+ * 返回查询系统的详细统计信息,包括实体数量、索引状态、
2284
+ * 查询性能统计等,用于性能监控和调试。
2285
+ *
2286
+ * @returns 系统统计信息对象
1944
2287
  */
1945
- remove(entity: Entity): void;
2288
+ getStats(): {
2289
+ entityCount: number;
2290
+ indexStats: {
2291
+ maskIndexSize: number;
2292
+ componentIndexSize: number;
2293
+ tagIndexSize: number;
2294
+ nameIndexSize: number;
2295
+ };
2296
+ queryStats: {
2297
+ totalQueries: number;
2298
+ cacheHits: number;
2299
+ indexHits: number;
2300
+ linearScans: number;
2301
+ archetypeHits: number;
2302
+ dirtyChecks: number;
2303
+ cacheHitRate: string;
2304
+ };
2305
+ optimizationStats: {
2306
+ componentIndex: any;
2307
+ archetypeSystem: any;
2308
+ dirtyTracking: any;
2309
+ };
2310
+ cacheStats: {
2311
+ size: number;
2312
+ hitRate: string;
2313
+ };
2314
+ };
1946
2315
  /**
1947
- * 立即移除实体
1948
- * @param entity 要移除的实体
2316
+ * 切换组件索引类型
2317
+ *
2318
+ * @param indexType 新的索引类型
1949
2319
  */
1950
- private removeImmediate;
2320
+ switchComponentIndexType(indexType: IndexType): void;
1951
2321
  /**
1952
- * 移除所有实体
2322
+ * 配置脏标记系统
2323
+ *
2324
+ * @param batchSize 批处理大小
2325
+ * @param maxProcessingTime 最大处理时间
1953
2326
  */
1954
- removeAllEntities(): void;
2327
+ configureDirtyTracking(batchSize: number, maxProcessingTime: number): void;
1955
2328
  /**
1956
- * 更新实体列表,处理延迟操作
2329
+ * 手动触发性能优化
1957
2330
  */
1958
- updateLists(): void;
2331
+ optimizePerformance(): void;
1959
2332
  /**
1960
- * 更新所有实体
2333
+ * 开始新的帧
1961
2334
  */
1962
- update(): void;
2335
+ beginFrame(): void;
1963
2336
  /**
1964
- * 根据名称查找实体(使用索引,O(1)复杂度)
1965
- * @param name 实体名称
1966
- * @returns 找到的第一个实体或null
2337
+ * 结束当前帧
1967
2338
  */
1968
- findEntity(name: string): Entity | null;
2339
+ endFrame(): void;
1969
2340
  /**
1970
- * 根据名称查找所有实体
1971
- * @param name 实体名称
1972
- * @returns 找到的所有实体数组
2341
+ * 标记实体组件已修改(用于脏标记追踪)
2342
+ *
2343
+ * @param entity 修改的实体
2344
+ * @param componentTypes 修改的组件类型
1973
2345
  */
1974
- findEntitiesByName(name: string): Entity[];
2346
+ markEntityDirty(entity: Entity, componentTypes: ComponentType[]): void;
1975
2347
  /**
1976
- * 根据ID查找实体(使用索引,O(1)复杂度)
1977
- * @param id 实体ID
1978
- * @returns 找到的实体或null
2348
+ * 获取实体所属的原型信息
2349
+ *
2350
+ * @param entity 要查询的实体
1979
2351
  */
1980
- findEntityById(id: number): Entity | null;
2352
+ getEntityArchetype(entity: Entity): Archetype | undefined;
2353
+ }
2354
+ /**
2355
+ * 查询构建器
2356
+ *
2357
+ * 提供链式API来构建复杂的实体查询条件。
2358
+ * 支持组合多种查询条件,创建灵活的查询表达式。
2359
+ *
2360
+ * @example
2361
+ * ```typescript
2362
+ * const result = new QueryBuilder(querySystem)
2363
+ * .withAll(PositionComponent, VelocityComponent)
2364
+ * .without(DeadComponent)
2365
+ * .execute();
2366
+ * ```
2367
+ */
2368
+ declare class QueryBuilder {
2369
+ private conditions;
2370
+ private querySystem;
2371
+ constructor(querySystem: QuerySystem);
1981
2372
  /**
1982
- * 根据标签查找实体
1983
- * @param tag 标签
1984
- * @returns 找到的所有实体数组
2373
+ * 添加"必须包含所有组件"条件
2374
+ *
2375
+ * @param componentTypes 必须包含的组件类型
2376
+ * @returns 查询构建器实例,支持链式调用
1985
2377
  */
1986
- findEntitiesByTag(tag: number): Entity[];
2378
+ withAll(...componentTypes: ComponentType[]): QueryBuilder;
1987
2379
  /**
1988
- * 根据组件类型查找实体
1989
- * @param componentType 组件类型
1990
- * @returns 找到的所有实体数组
2380
+ * 添加"必须包含任意组件"条件
2381
+ *
2382
+ * @param componentTypes 必须包含其中任意一个的组件类型
2383
+ * @returns 查询构建器实例,支持链式调用
1991
2384
  */
1992
- findEntitiesWithComponent<T extends Component>(componentType: new (...args: any[]) => T): Entity[];
2385
+ withAny(...componentTypes: ComponentType[]): QueryBuilder;
1993
2386
  /**
1994
- * 批量操作:对所有实体执行指定操作
1995
- * @param action 要执行的操作
2387
+ * 添加"不能包含任何组件"条件
2388
+ *
2389
+ * @param componentTypes 不能包含的组件类型
2390
+ * @returns 查询构建器实例,支持链式调用
1996
2391
  */
1997
- forEach(action: (entity: Entity) => void): void;
2392
+ without(...componentTypes: ComponentType[]): QueryBuilder;
1998
2393
  /**
1999
- * 批量操作:对符合条件的实体执行指定操作
2000
- * @param predicate 筛选条件
2001
- * @param action 要执行的操作
2394
+ * 执行查询并返回结果
2395
+ *
2396
+ * 根据已添加的查询条件执行实体查询。
2397
+ *
2398
+ * @returns 查询结果,包含匹配的实体和性能信息
2002
2399
  */
2003
- forEachWhere(predicate: (entity: Entity) => boolean, action: (entity: Entity) => void): void;
2400
+ execute(): QueryResult;
2004
2401
  /**
2005
- * 更新名称索引
2006
- * @param entity 实体
2007
- * @param isAdd 是否为添加操作
2402
+ * 创建组件掩码
2008
2403
  */
2009
- private updateNameIndex;
2404
+ private createComponentMask;
2010
2405
  /**
2011
- * 获取实体列表的统计信息
2012
- * @returns 统计信息
2406
+ * 重置查询构建器
2407
+ *
2408
+ * 清除所有已添加的查询条件,重新开始构建查询。
2409
+ *
2410
+ * @returns 查询构建器实例,支持链式调用
2013
2411
  */
2014
- getStats(): {
2015
- totalEntities: number;
2016
- activeEntities: number;
2017
- pendingAdd: number;
2018
- pendingRemove: number;
2019
- nameIndexSize: number;
2020
- };
2412
+ reset(): QueryBuilder;
2021
2413
  }
2022
2414
 
2023
2415
  /**
2024
- * 高性能位操作类
2025
- * 基于BigInt实现,支持任意数量的位操作
2416
+ * 增强的事件总线实现
2417
+ * 基于TypeSafeEventSystem,提供类型安全的事件发布订阅机制
2026
2418
  */
2027
- declare class Bits {
2028
- private _value;
2029
- constructor(initialValue?: bigint);
2419
+ declare class EventBus implements IEventBus {
2420
+ private eventSystem;
2421
+ private eventIdCounter;
2422
+ private isDebugMode;
2423
+ constructor(debugMode?: boolean);
2030
2424
  /**
2031
- * 设置指定位置的位为1
2425
+ * 发射事件
2426
+ * @param eventType 事件类型
2427
+ * @param data 事件数据
2032
2428
  */
2033
- set(index: number): void;
2429
+ emit<T>(eventType: string, data: T): void;
2034
2430
  /**
2035
- * 清除指定位置的位(设为0)
2431
+ * 异步发射事件
2432
+ * @param eventType 事件类型
2433
+ * @param data 事件数据
2036
2434
  */
2037
- clear(index: number): void;
2435
+ emitAsync<T>(eventType: string, data: T): Promise<void>;
2038
2436
  /**
2039
- * 获取指定位置的位值
2437
+ * 监听事件
2438
+ * @param eventType 事件类型
2439
+ * @param handler 事件处理器
2440
+ * @param config 监听器配置
2441
+ * @returns 监听器ID
2040
2442
  */
2041
- get(index: number): boolean;
2443
+ on<T>(eventType: string, handler: (data: T) => void, config?: IEventListenerConfig): string;
2042
2444
  /**
2043
- * 检查是否包含所有指定的位
2445
+ * 监听事件(一次性)
2446
+ * @param eventType 事件类型
2447
+ * @param handler 事件处理器
2448
+ * @param config 监听器配置
2449
+ * @returns 监听器ID
2044
2450
  */
2045
- containsAll(other: Bits): boolean;
2451
+ once<T>(eventType: string, handler: (data: T) => void, config?: IEventListenerConfig): string;
2046
2452
  /**
2047
- * 检查是否包含任意一个指定的位
2453
+ * 异步监听事件
2454
+ * @param eventType 事件类型
2455
+ * @param handler 异步事件处理器
2456
+ * @param config 监听器配置
2457
+ * @returns 监听器ID
2048
2458
  */
2049
- intersects(other: Bits): boolean;
2459
+ onAsync<T>(eventType: string, handler: (data: T) => Promise<void>, config?: IEventListenerConfig): string;
2050
2460
  /**
2051
- * 检查是否不包含任何指定的位
2461
+ * 移除事件监听器
2462
+ * @param eventType 事件类型
2463
+ * @param listenerId 监听器ID
2052
2464
  */
2053
- excludes(other: Bits): boolean;
2465
+ off(eventType: string, listenerId: string): boolean;
2054
2466
  /**
2055
- * 清空所有位
2467
+ * 移除指定事件类型的所有监听器
2468
+ * @param eventType 事件类型
2056
2469
  */
2057
- clearAll(): void;
2470
+ offAll(eventType: string): void;
2058
2471
  /**
2059
- * 检查是否为空(没有设置任何位)
2472
+ * 检查是否有指定事件的监听器
2473
+ * @param eventType 事件类型
2060
2474
  */
2061
- isEmpty(): boolean;
2475
+ hasListeners(eventType: string): boolean;
2062
2476
  /**
2063
- * 获取设置的位数量
2477
+ * 获取事件统计信息
2478
+ * @param eventType 事件类型(可选)
2064
2479
  */
2065
- cardinality(): number;
2480
+ getStats(eventType?: string): IEventStats | Map<string, IEventStats>;
2066
2481
  /**
2067
- * 位运算:与
2482
+ * 清空所有监听器
2068
2483
  */
2069
- and(other: Bits): Bits;
2484
+ clear(): void;
2070
2485
  /**
2071
- * 位运算:或
2486
+ * 启用或禁用事件系统
2487
+ * @param enabled 是否启用
2072
2488
  */
2073
- or(other: Bits): Bits;
2489
+ setEnabled(enabled: boolean): void;
2074
2490
  /**
2075
- * 位运算:异或
2491
+ * 设置调试模式
2492
+ * @param debug 是否启用调试
2076
2493
  */
2077
- xor(other: Bits): Bits;
2494
+ setDebugMode(debug: boolean): void;
2078
2495
  /**
2079
- * 位运算:非
2496
+ * 设置最大监听器数量
2497
+ * @param max 最大数量
2498
+ */
2499
+ setMaxListeners(max: number): void;
2500
+ /**
2501
+ * 获取监听器数量
2502
+ * @param eventType 事件类型
2503
+ */
2504
+ getListenerCount(eventType: string): number;
2505
+ /**
2506
+ * 设置事件批处理配置
2507
+ * @param eventType 事件类型
2508
+ * @param batchSize 批处理大小
2509
+ * @param delay 延迟时间(毫秒)
2080
2510
  */
2081
- not(maxBits?: number): Bits;
2511
+ setBatchConfig(eventType: string, batchSize: number, delay: number): void;
2082
2512
  /**
2083
- * 复制另一个Bits对象
2513
+ * 刷新指定事件的批处理队列
2514
+ * @param eventType 事件类型
2084
2515
  */
2085
- copyFrom(other: Bits): void;
2516
+ flushBatch(eventType: string): void;
2086
2517
  /**
2087
- * 创建当前Bits的副本
2518
+ * 重置事件统计
2519
+ * @param eventType 事件类型(可选)
2088
2520
  */
2089
- clone(): Bits;
2521
+ resetStats(eventType?: string): void;
2090
2522
  /**
2091
- * 获取原始BigInt值
2523
+ * 发射实体创建事件
2524
+ * @param entityData 实体事件数据
2092
2525
  */
2093
- getValue(): bigint;
2526
+ emitEntityCreated(entityData: IEntityEventData): void;
2094
2527
  /**
2095
- * 设置原始BigInt值
2528
+ * 发射实体销毁事件
2529
+ * @param entityData 实体事件数据
2096
2530
  */
2097
- setValue(value: bigint): void;
2531
+ emitEntityDestroyed(entityData: IEntityEventData): void;
2098
2532
  /**
2099
- * 获取调试信息
2533
+ * 发射组件添加事件
2534
+ * @param componentData 组件事件数据
2100
2535
  */
2101
- toString(): string;
2536
+ emitComponentAdded(componentData: IComponentEventData): void;
2102
2537
  /**
2103
- * 获取二进制表示
2538
+ * 发射组件移除事件
2539
+ * @param componentData 组件事件数据
2104
2540
  */
2105
- toBinaryString(maxBits?: number): string;
2541
+ emitComponentRemoved(componentData: IComponentEventData): void;
2106
2542
  /**
2107
- * 获取十六进制表示
2543
+ * 发射系统添加事件
2544
+ * @param systemData 系统事件数据
2108
2545
  */
2109
- toHexString(): string;
2546
+ emitSystemAdded(systemData: ISystemEventData): void;
2110
2547
  /**
2111
- * 从二进制字符串创建Bits
2548
+ * 发射系统移除事件
2549
+ * @param systemData 系统事件数据
2112
2550
  */
2113
- static fromBinaryString(binaryString: string): Bits;
2551
+ emitSystemRemoved(systemData: ISystemEventData): void;
2114
2552
  /**
2115
- * 从十六进制字符串创建Bits
2553
+ * 发射场景变化事件
2554
+ * @param sceneData 场景事件数据
2116
2555
  */
2117
- static fromHexString(hexString: string): Bits;
2556
+ emitSceneChanged(sceneData: ISceneEventData): void;
2118
2557
  /**
2119
- * 比较两个Bits对象是否相等
2558
+ * 发射性能警告事件
2559
+ * @param performanceData 性能事件数据
2120
2560
  */
2121
- equals(other: Bits): boolean;
2561
+ emitPerformanceWarning(performanceData: IPerformanceEventData): void;
2122
2562
  /**
2123
- * 获取最高位的索引
2563
+ * 监听实体创建事件
2564
+ * @param handler 事件处理器
2565
+ * @param config 监听器配置
2124
2566
  */
2125
- getHighestBitIndex(): number;
2567
+ onEntityCreated(handler: (data: IEntityEventData) => void, config?: IEventListenerConfig): string;
2126
2568
  /**
2127
- * 获取最低位的索引
2569
+ * 监听组件添加事件
2570
+ * @param handler 事件处理器
2571
+ * @param config 监听器配置
2128
2572
  */
2129
- getLowestBitIndex(): number;
2130
- }
2131
-
2132
- /**
2133
- * 高性能实体匹配器
2134
- * 用于快速匹配符合条件的实体
2135
- */
2136
- declare class Matcher {
2137
- protected allSet: (new (...args: any[]) => Component)[];
2138
- protected exclusionSet: (new (...args: any[]) => Component)[];
2139
- protected oneSet: (new (...args: any[]) => Component)[];
2140
- private _allBits?;
2141
- private _exclusionBits?;
2142
- private _oneBits?;
2143
- private _isDirty;
2144
- static empty(): Matcher;
2145
- getAllSet(): (new (...args: any[]) => Component)[];
2146
- getExclusionSet(): (new (...args: any[]) => Component)[];
2147
- getOneSet(): (new (...args: any[]) => Component)[];
2573
+ onComponentAdded(handler: (data: IComponentEventData) => void, config?: IEventListenerConfig): string;
2148
2574
  /**
2149
- * 检查实体是否匹配条件
2150
- * @param entity 要检查的实体
2151
- * @returns 是否匹配
2575
+ * 监听系统错误事件
2576
+ * @param handler 事件处理器
2577
+ * @param config 监听器配置
2152
2578
  */
2153
- isInterestedEntity(entity: Entity): boolean;
2579
+ onSystemError(handler: (data: ISystemEventData) => void, config?: IEventListenerConfig): string;
2154
2580
  /**
2155
- * 检查组件位掩码是否匹配条件
2156
- * @param componentBits 组件位掩码
2157
- * @returns 是否匹配
2581
+ * 监听性能警告事件
2582
+ * @param handler 事件处理器
2583
+ * @param config 监听器配置
2158
2584
  */
2159
- isInterested(componentBits: Bits): boolean;
2585
+ onPerformanceWarning(handler: (data: IPerformanceEventData) => void, config?: IEventListenerConfig): string;
2160
2586
  /**
2161
- * 添加所有包含的组件类型
2162
- * @param types 所有包含的组件类型列表
2587
+ * 验证事件类型
2588
+ * @param eventType 事件类型
2163
2589
  */
2164
- all(...types: (new (...args: any[]) => Component)[]): Matcher;
2590
+ private validateEventType;
2165
2591
  /**
2166
- * 添加排除包含的组件类型
2167
- * @param types 排除包含的组件类型列表
2592
+ * 增强事件数据
2593
+ * @param eventType 事件类型
2594
+ * @param data 原始数据
2168
2595
  */
2169
- exclude(...types: (new (...args: any[]) => Component)[]): Matcher;
2596
+ private enhanceEventData;
2170
2597
  /**
2171
- * 添加至少包含其中之一的组件类型
2172
- * @param types 至少包含其中之一的组件类型列表
2598
+ * 转换EventStats为IEventStats
2599
+ * @param stats EventStats实例
2173
2600
  */
2174
- one(...types: (new (...args: any[]) => Component)[]): Matcher;
2601
+ private convertEventStats;
2602
+ }
2603
+ /**
2604
+ * 全局事件总线实例
2605
+ * 提供全局访问的事件总线
2606
+ */
2607
+ declare class GlobalEventBus {
2608
+ private static instance;
2175
2609
  /**
2176
- * 获取实体的组件位掩码
2177
- * @param entity 实体
2178
- * @returns 组件位掩码
2610
+ * 获取全局事件总线实例
2611
+ * @param debugMode 是否启用调试模式
2179
2612
  */
2180
- private getEntityBits;
2613
+ static getInstance(debugMode?: boolean): EventBus;
2181
2614
  /**
2182
- * 如果位掩码已过期,则更新它们
2615
+ * 重置全局事件总线实例
2616
+ * @param debugMode 是否启用调试模式
2183
2617
  */
2184
- private updateBitsIfDirty;
2618
+ static reset(debugMode?: boolean): EventBus;
2619
+ }
2620
+ /**
2621
+ * 事件装饰器工厂
2622
+ * 用于自动注册事件监听器
2623
+ */
2624
+ declare function EventHandler(eventType: string, config?: IEventListenerConfig): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
2625
+ /**
2626
+ * 异步事件装饰器工厂
2627
+ * 用于自动注册异步事件监听器
2628
+ */
2629
+ declare function AsyncEventHandler(eventType: string, config?: IEventListenerConfig): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
2630
+
2631
+ interface IScene {
2632
+ readonly name: string;
2633
+ readonly componentStorageManager: ComponentStorageManager;
2634
+ readonly querySystem: QuerySystem;
2635
+ readonly eventSystem: TypeSafeEventSystem;
2636
+ readonly entities: EntityList;
2637
+ addEntity(entity: Entity): Entity;
2638
+ initialize(): void;
2639
+ update(deltaTime: number): void;
2640
+ }
2641
+ /**
2642
+ * 实体比较器
2643
+ *
2644
+ * 用于比较两个实体的优先级,首先按更新顺序比较,然后按ID比较。
2645
+ */
2646
+ declare class EntityComparer {
2185
2647
  /**
2186
- * 创建匹配器的字符串表示(用于调试)
2187
- * @returns 字符串表示
2648
+ * 比较两个实体
2649
+ *
2650
+ * @param self - 第一个实体
2651
+ * @param other - 第二个实体
2652
+ * @returns 比较结果,负数表示self优先级更高,正数表示other优先级更高,0表示相等
2188
2653
  */
2189
- toString(): string;
2654
+ compare(self: Entity, other: Entity): number;
2190
2655
  }
2191
-
2192
2656
  /**
2193
- * 实体系统的基类
2657
+ * 游戏实体类
2194
2658
  *
2195
- * 用于处理一组符合特定条件的实体。系统是ECS架构中的逻辑处理单元,
2196
- * 负责对拥有特定组件组合的实体执行业务逻辑。
2659
+ * ECS架构中的实体(Entity),作为组件的容器。
2660
+ * 实体本身不包含游戏逻辑,所有功能都通过组件来实现。
2661
+ * 支持父子关系,可以构建实体层次结构。
2197
2662
  *
2198
2663
  * @example
2199
2664
  * ```typescript
2200
- * class MovementSystem extends EntitySystem {
2201
- * constructor() {
2202
- * super(Matcher.empty().all(Transform, Velocity));
2203
- * }
2665
+ * // 创建实体
2666
+ * const entity = new Entity("Player", 1);
2204
2667
  *
2205
- * protected process(entities: Entity[]): void {
2206
- * for (const entity of entities) {
2207
- * const transform = entity.getComponent(Transform);
2208
- * const velocity = entity.getComponent(Velocity);
2209
- * transform.position.add(velocity.value);
2210
- * }
2211
- * }
2212
- * }
2668
+ * // 添加组件
2669
+ * const healthComponent = entity.addComponent(new HealthComponent(100));
2670
+ *
2671
+ * // 获取组件
2672
+ * const health = entity.getComponent(HealthComponent);
2673
+ *
2674
+ * // 添加位置组件
2675
+ * entity.addComponent(new PositionComponent(100, 200));
2676
+ *
2677
+ * // 添加子实体
2678
+ * const weapon = new Entity("Weapon", 2);
2679
+ * entity.addChild(weapon);
2213
2680
  * ```
2214
2681
  */
2215
- declare abstract class EntitySystem implements ISystemBase {
2216
- private _entities;
2217
- private _updateOrder;
2218
- private _enabled;
2219
- private _performanceMonitor;
2220
- private _systemName;
2682
+ declare class Entity {
2221
2683
  /**
2222
- * 获取系统处理的实体列表
2684
+ * 实体比较器实例
2223
2685
  */
2224
- get entities(): readonly Entity[];
2686
+ static entityComparer: EntityComparer;
2225
2687
  /**
2226
- * 获取系统的更新时序
2688
+ * 全局事件总线实例
2689
+ * 用于发射组件相关事件
2227
2690
  */
2228
- get updateOrder(): number;
2229
- set updateOrder(value: number);
2691
+ static eventBus: EventBus | null;
2230
2692
  /**
2231
- * 获取系统的启用状态
2693
+ * 实体名称
2694
+ *
2695
+ * 用于标识和调试的友好名称。
2232
2696
  */
2233
- get enabled(): boolean;
2697
+ name: string;
2234
2698
  /**
2235
- * 设置系统的启用状态
2699
+ * 实体唯一标识符
2700
+ *
2701
+ * 在场景中唯一的数字标识符。
2236
2702
  */
2237
- set enabled(value: boolean);
2703
+ readonly id: number;
2238
2704
  /**
2239
- * 获取系统名称
2705
+ * 组件集合
2706
+ *
2707
+ * 存储实体拥有的所有组件。
2240
2708
  */
2241
- get systemName(): string;
2242
- constructor(matcher?: Matcher);
2243
- private _scene;
2709
+ readonly components: Component[];
2244
2710
  /**
2245
- * 这个系统所属的场景
2711
+ * 所属场景引用
2712
+ *
2713
+ * 指向实体所在的场景实例。
2246
2714
  */
2247
- get scene(): Scene | null;
2248
- set scene(value: Scene | null);
2249
- private _matcher;
2715
+ scene: IScene | null;
2250
2716
  /**
2251
- * 获取实体匹配器
2717
+ * 更新间隔
2718
+ *
2719
+ * 控制实体更新的频率,值越大更新越不频繁。
2252
2720
  */
2253
- get matcher(): Matcher;
2721
+ updateInterval: number;
2254
2722
  /**
2255
- * 设置更新时序
2256
- * @param order 更新时序
2723
+ * 销毁状态标志
2724
+ *
2725
+ * 标记实体是否已被销毁。
2257
2726
  */
2258
- setUpdateOrder(order: number): void;
2727
+ _isDestroyed: boolean;
2728
+ /**
2729
+ * 父实体引用
2730
+ *
2731
+ * 指向父级实体,用于构建实体层次结构。
2732
+ */
2733
+ private _parent;
2259
2734
  /**
2260
- * 系统初始化
2735
+ * 子实体集合
2261
2736
  *
2262
- * 在系统创建时调用,自动检查场景中已存在的实体是否匹配此系统。
2263
- * 子类可以重写此方法进行额外的初始化操作。
2737
+ * 存储所有子级实体的数组。
2264
2738
  */
2265
- initialize(): void;
2739
+ private _children;
2740
+ /**
2741
+ * 激活状态
2742
+ *
2743
+ * 控制实体是否处于激活状态。
2744
+ */
2745
+ private _active;
2746
+ /**
2747
+ * 实体标签
2748
+ *
2749
+ * 用于分类和查询的数字标签。
2750
+ */
2751
+ private _tag;
2752
+ /**
2753
+ * 启用状态
2754
+ *
2755
+ * 控制实体是否启用更新和处理。
2756
+ */
2757
+ private _enabled;
2266
2758
  /**
2267
- * 当实体的组件发生变化时调用
2759
+ * 更新顺序
2268
2760
  *
2269
- * 检查实体是否仍然符合系统的匹配条件,并相应地添加或移除实体。
2761
+ * 控制实体在系统中的更新优先级。
2762
+ */
2763
+ private _updateOrder;
2764
+ /**
2765
+ * 组件位掩码
2270
2766
  *
2271
- * @param entity 发生变化的实体
2767
+ * 用于快速查询实体拥有的组件类型。
2272
2768
  */
2273
- onChanged(entity: Entity): void;
2769
+ private _componentMask;
2274
2770
  /**
2275
- * 添加实体到系统
2771
+ * 组件类型到索引的映射
2276
2772
  *
2277
- * @param entity 要添加的实体
2773
+ * 用于快速定位组件在数组中的位置。
2278
2774
  */
2279
- add(entity: Entity): void;
2775
+ private _componentTypeToIndex;
2280
2776
  /**
2281
- * 当实体被添加到系统时调用
2282
- *
2283
- * 子类可以重写此方法来处理实体添加事件。
2777
+ * 构造函数
2284
2778
  *
2285
- * @param entity 被添加的实体
2779
+ * @param name - 实体名称
2780
+ * @param id - 实体唯一标识符
2286
2781
  */
2287
- protected onAdded(entity: Entity): void;
2782
+ constructor(name: string, id: number);
2288
2783
  /**
2289
- * 从系统中移除实体
2784
+ * 获取销毁状态
2290
2785
  *
2291
- * @param entity 要移除的实体
2786
+ * @returns 如果实体已被销毁则返回true
2292
2787
  */
2293
- remove(entity: Entity): void;
2788
+ get isDestroyed(): boolean;
2294
2789
  /**
2295
- * 当实体从系统中移除时调用
2296
- *
2297
- * 子类可以重写此方法来处理实体移除事件。
2790
+ * 获取父实体
2298
2791
  *
2299
- * @param entity 被移除的实体
2792
+ * @returns 父实体,如果没有父实体则返回null
2300
2793
  */
2301
- protected onRemoved(entity: Entity): void;
2794
+ get parent(): Entity | null;
2302
2795
  /**
2303
- * 更新系统
2796
+ * 获取子实体数组的只读副本
2304
2797
  *
2305
- * 在每帧调用,处理系统的主要逻辑。
2798
+ * @returns 子实体数组的副本
2306
2799
  */
2307
- update(): void;
2800
+ get children(): readonly Entity[];
2308
2801
  /**
2309
- * 后期更新系统
2802
+ * 获取子实体数量
2310
2803
  *
2311
- * 在所有系统的update方法执行完毕后调用。
2804
+ * @returns 子实体的数量
2312
2805
  */
2313
- lateUpdate(): void;
2806
+ get childCount(): number;
2314
2807
  /**
2315
- * 在系统处理开始前调用
2808
+ * 获取活跃状态
2316
2809
  *
2317
- * 子类可以重写此方法进行预处理操作。
2810
+ * @returns 如果实体处于活跃状态则返回true
2318
2811
  */
2319
- protected begin(): void;
2812
+ get active(): boolean;
2320
2813
  /**
2321
- * 处理实体列表
2814
+ * 设置活跃状态
2322
2815
  *
2323
- * 系统的核心逻辑,子类必须实现此方法来定义具体的处理逻辑。
2816
+ * 设置实体的活跃状态,会影响子实体的有效活跃状态。
2324
2817
  *
2325
- * @param entities 要处理的实体列表
2818
+ * @param value - 新的活跃状态
2326
2819
  */
2327
- protected process(entities: Entity[]): void;
2820
+ set active(value: boolean);
2328
2821
  /**
2329
- * 后期处理实体列表
2822
+ * 获取实体的有效活跃状态
2330
2823
  *
2331
- * 在主要处理逻辑之后执行,子类可以重写此方法。
2824
+ * 考虑父实体的活跃状态,只有当实体本身和所有父实体都处于活跃状态时才返回true。
2332
2825
  *
2333
- * @param entities 要处理的实体列表
2826
+ * @returns 有效的活跃状态
2334
2827
  */
2335
- protected lateProcess(entities: Entity[]): void;
2828
+ get activeInHierarchy(): boolean;
2336
2829
  /**
2337
- * 系统处理完毕后调用
2830
+ * 获取实体标签
2338
2831
  *
2339
- * 子类可以重写此方法进行后处理操作。
2832
+ * @returns 实体的数字标签
2340
2833
  */
2341
- protected end(): void;
2834
+ get tag(): number;
2342
2835
  /**
2343
- * 检查系统是否需要处理
2344
- *
2345
- * 在启用系统时有用,但仅偶尔需要处理。
2346
- * 这只影响处理,不影响事件或订阅列表。
2836
+ * 设置实体标签
2347
2837
  *
2348
- * @returns 如果系统应该处理,则为true,如果不处理则为false
2838
+ * @param value - 新的标签值
2349
2839
  */
2350
- protected checkProcessing(): boolean;
2840
+ set tag(value: number);
2351
2841
  /**
2352
- * 获取系统的性能数据
2842
+ * 获取启用状态
2353
2843
  *
2354
- * @returns 性能数据或undefined
2844
+ * @returns 如果实体已启用则返回true
2355
2845
  */
2356
- getPerformanceData(): PerformanceData | undefined;
2846
+ get enabled(): boolean;
2357
2847
  /**
2358
- * 获取系统的性能统计
2848
+ * 设置启用状态
2359
2849
  *
2360
- * @returns 性能统计或undefined
2361
- */
2362
- getPerformanceStats(): PerformanceStats | undefined;
2363
- /**
2364
- * 重置系统的性能数据
2850
+ * @param value - 新的启用状态
2365
2851
  */
2366
- resetPerformanceData(): void;
2852
+ set enabled(value: boolean);
2367
2853
  /**
2368
- * 获取系统信息的字符串表示
2854
+ * 获取更新顺序
2369
2855
  *
2370
- * @returns 系统信息字符串
2371
- */
2372
- toString(): string;
2373
- }
2374
-
2375
- /**
2376
- * 实体处理器列表管理器
2377
- * 管理场景中的所有实体系统
2378
- */
2379
- declare class EntityProcessorList {
2380
- private _processors;
2381
- private _isDirty;
2382
- /**
2383
- * 设置为脏状态,需要重新排序
2856
+ * @returns 实体的更新顺序值
2384
2857
  */
2385
- setDirty(): void;
2858
+ get updateOrder(): number;
2386
2859
  /**
2387
- * 添加实体处理器
2388
- * @param processor 要添加的处理器
2860
+ * 设置更新顺序
2861
+ *
2862
+ * @param value - 新的更新顺序值
2389
2863
  */
2390
- add(processor: EntitySystem): void;
2864
+ set updateOrder(value: number);
2391
2865
  /**
2392
- * 移除实体处理器
2393
- * @param processor 要移除的处理器
2866
+ * 获取组件位掩码
2867
+ *
2868
+ * @returns 实体的组件位掩码
2394
2869
  */
2395
- remove(processor: EntitySystem): void;
2870
+ get componentMask(): IBigIntLike;
2396
2871
  /**
2397
- * 获取指定类型的处理器
2398
- * @param type 处理器类型
2872
+ * 创建并添加组件
2873
+ *
2874
+ * @param componentType - 组件类型
2875
+ * @param args - 组件构造函数参数
2876
+ * @returns 创建的组件实例
2399
2877
  */
2400
- getProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T | null;
2878
+ createComponent<T extends Component>(componentType: ComponentType<T>, ...args: unknown[]): T;
2401
2879
  /**
2402
- * 开始处理
2880
+ * 内部添加组件方法(不进行重复检查,用于初始化)
2881
+ *
2882
+ * @param component - 要添加的组件实例
2883
+ * @returns 添加的组件实例
2403
2884
  */
2404
- begin(): void;
2885
+ private addComponentInternal;
2405
2886
  /**
2406
- * 结束处理
2887
+ * 添加组件到实体
2888
+ *
2889
+ * @param component - 要添加的组件实例
2890
+ * @returns 添加的组件实例
2891
+ * @throws {Error} 如果组件类型已存在
2407
2892
  */
2408
- end(): void;
2893
+ addComponent<T extends Component>(component: T): T;
2409
2894
  /**
2410
- * 更新所有处理器
2895
+ * 获取指定类型的组件
2896
+ *
2897
+ * @param type - 组件类型
2898
+ * @returns 组件实例或null
2411
2899
  */
2412
- update(): void;
2900
+ getComponent<T extends Component>(type: ComponentType<T>): T | null;
2413
2901
  /**
2414
- * 后期更新所有处理器
2902
+ * 重建组件索引映射
2415
2903
  */
2416
- lateUpdate(): void;
2904
+ private rebuildComponentIndex;
2417
2905
  /**
2418
- * 排序处理器
2906
+ * 检查实体是否有指定类型的组件
2907
+ *
2908
+ * @param type - 组件类型
2909
+ * @returns 如果有该组件则返回true
2419
2910
  */
2420
- private sortProcessors;
2421
- /** 获取处理器列表 */
2422
- get processors(): EntitySystem[];
2423
- /** 获取处理器数量 */
2424
- get count(): number;
2425
- }
2426
-
2427
- /**
2428
- * 世代式ID池管理器
2429
- *
2430
- * 用于管理实体ID的分配和回收,支持世代版本控制以防止悬空引用问题。
2431
- * 世代式ID由索引和版本组成,当ID被回收时版本会递增,确保旧引用失效。
2432
- *
2433
- * 支持动态扩展,理论上可以支持到65535个索引(16位),每个索引65535个版本(16位)。
2434
- * 总计可以处理超过42亿个独特的ID组合,完全满足ECS大规模实体需求。
2435
- *
2436
- * @example
2437
- * ```typescript
2438
- * const pool = new IdentifierPool();
2439
- *
2440
- * // 分配ID
2441
- * const id = pool.checkOut(); // 例如: 65536 (版本1,索引0)
2442
- *
2443
- * // 回收ID
2444
- * pool.checkIn(id);
2445
- *
2446
- * // 验证ID是否有效
2447
- * const isValid = pool.isValid(id); // false,因为版本已递增
2448
- * ```
2449
- */
2450
- declare class IdentifierPool {
2911
+ hasComponent<T extends Component>(type: ComponentType<T>): boolean;
2451
2912
  /**
2452
- * 下一个可用的索引
2913
+ * 获取或创建指定类型的组件
2914
+ *
2915
+ * @param type - 组件类型
2916
+ * @param args - 组件构造函数参数(仅在创建时使用)
2917
+ * @returns 组件实例
2453
2918
  */
2454
- private _nextAvailableIndex;
2919
+ getOrCreateComponent<T extends Component>(type: ComponentType<T>, ...args: unknown[]): T;
2455
2920
  /**
2456
- * 空闲的索引列表
2921
+ * 移除指定的组件
2922
+ *
2923
+ * @param component - 要移除的组件实例
2457
2924
  */
2458
- private _freeIndices;
2925
+ removeComponent(component: Component): void;
2459
2926
  /**
2460
- * 每个索引对应的世代版本
2461
- * 动态扩展的Map,按需分配内存
2927
+ * 移除指定类型的组件
2928
+ *
2929
+ * @param type - 组件类型
2930
+ * @returns 被移除的组件实例或null
2462
2931
  */
2463
- private _generations;
2932
+ removeComponentByType<T extends Component>(type: ComponentType<T>): T | null;
2464
2933
  /**
2465
- * 延迟回收队列
2466
- * 防止在同一帧内立即重用ID,避免时序问题
2934
+ * 移除所有组件
2467
2935
  */
2468
- private _pendingRecycle;
2936
+ removeAllComponents(): void;
2469
2937
  /**
2470
- * 延迟回收时间(毫秒)
2938
+ * 批量添加组件
2939
+ *
2940
+ * @param components - 要添加的组件数组
2941
+ * @returns 添加的组件数组
2471
2942
  */
2472
- private _recycleDelay;
2943
+ addComponents<T extends Component>(components: T[]): T[];
2473
2944
  /**
2474
- * 最大索引限制(16位)
2475
- * 这是框架设计选择:16位索引 + 16位版本 = 32位ID,确保高效位操作
2476
- * 不是硬件限制,而是性能和内存效率的权衡
2945
+ * 批量移除组件类型
2946
+ *
2947
+ * @param componentTypes - 要移除的组件类型数组
2948
+ * @returns 被移除的组件数组
2477
2949
  */
2478
- private static readonly MAX_INDEX;
2950
+ removeComponentsByTypes<T extends Component>(componentTypes: ComponentType<T>[]): (T | null)[];
2479
2951
  /**
2480
- * 最大世代限制(16位)
2952
+ * 获取所有指定类型的组件
2953
+ *
2954
+ * @param type - 组件类型
2955
+ * @returns 组件实例数组
2481
2956
  */
2482
- private static readonly MAX_GENERATION;
2957
+ getComponents<T extends Component>(type: ComponentType<T>): T[];
2483
2958
  /**
2484
- * 内存扩展块大小
2485
- * 当需要更多内存时,一次性预分配的索引数量
2959
+ * 添加子实体
2960
+ *
2961
+ * @param child - 要添加的子实体
2962
+ * @returns 添加的子实体
2486
2963
  */
2487
- private _expansionBlockSize;
2964
+ addChild(child: Entity): Entity;
2488
2965
  /**
2489
- * 统计信息
2966
+ * 移除子实体
2967
+ *
2968
+ * @param child - 要移除的子实体
2969
+ * @returns 是否成功移除
2490
2970
  */
2491
- private _stats;
2971
+ removeChild(child: Entity): boolean;
2492
2972
  /**
2493
- * 构造函数
2494
- *
2495
- * @param recycleDelay 延迟回收时间(毫秒),默认为100ms
2496
- * @param expansionBlockSize 内存扩展块大小,默认为1024
2973
+ * 移除所有子实体
2497
2974
  */
2498
- constructor(recycleDelay?: number, expansionBlockSize?: number);
2975
+ removeAllChildren(): void;
2499
2976
  /**
2500
- * 获取一个可用的ID
2501
- *
2502
- * 返回一个32位ID,高16位为世代版本,低16位为索引。
2977
+ * 根据名称查找子实体
2503
2978
  *
2504
- * @returns 新分配的实体ID
2505
- * @throws {Error} 当达到索引限制时抛出错误
2979
+ * @param name - 子实体名称
2980
+ * @param recursive - 是否递归查找
2981
+ * @returns 找到的子实体或null
2506
2982
  */
2507
- checkOut(): number;
2983
+ findChild(name: string, recursive?: boolean): Entity | null;
2508
2984
  /**
2509
- * 回收一个ID
2510
- *
2511
- * 验证ID的有效性后,将其加入延迟回收队列。
2512
- * ID不会立即可重用,而是在延迟时间后才真正回收。
2985
+ * 根据标签查找子实体
2513
2986
  *
2514
- * @param id 要回收的实体ID
2515
- * @returns 是否成功回收(ID是否有效且未被重复回收)
2987
+ * @param tag - 标签
2988
+ * @param recursive - 是否递归查找
2989
+ * @returns 找到的子实体数组
2516
2990
  */
2517
- checkIn(id: number): boolean;
2991
+ findChildrenByTag(tag: number, recursive?: boolean): Entity[];
2518
2992
  /**
2519
- * 验证ID是否有效
2520
- *
2521
- * 检查ID的索引和世代版本是否匹配当前状态。
2993
+ * 获取根实体
2522
2994
  *
2523
- * @param id 要验证的实体ID
2524
- * @returns ID是否有效
2995
+ * @returns 层次结构的根实体
2525
2996
  */
2526
- isValid(id: number): boolean;
2997
+ getRoot(): Entity;
2527
2998
  /**
2528
- * 获取统计信息
2999
+ * 检查是否是指定实体的祖先
2529
3000
  *
2530
- * @returns 池的当前状态统计
3001
+ * @param entity - 要检查的实体
3002
+ * @returns 如果是祖先则返回true
2531
3003
  */
2532
- getStats(): {
2533
- /** 已分配的总索引数 */
2534
- totalAllocated: number;
2535
- /** 总计回收次数 */
2536
- totalRecycled: number;
2537
- /** 当前活跃实体数 */
2538
- currentActive: number;
2539
- /** 当前空闲的索引数 */
2540
- currentlyFree: number;
2541
- /** 等待回收的ID数 */
2542
- pendingRecycle: number;
2543
- /** 理论最大实体数(设计限制) */
2544
- maxPossibleEntities: number;
2545
- /** 当前使用的最大索引 */
2546
- maxUsedIndex: number;
2547
- /** 内存使用(字节) */
2548
- memoryUsage: number;
2549
- /** 内存扩展次数 */
2550
- memoryExpansions: number;
2551
- /** 平均世代版本 */
2552
- averageGeneration: number;
2553
- /** 世代存储大小 */
2554
- generationStorageSize: number;
2555
- };
3004
+ isAncestorOf(entity: Entity): boolean;
2556
3005
  /**
2557
- * 强制执行延迟回收处理
3006
+ * 检查是否是指定实体的后代
2558
3007
  *
2559
- * 在某些情况下可能需要立即处理延迟回收队列,
2560
- * 比如内存压力大或者需要精确的统计信息时。
3008
+ * @param entity - 要检查的实体
3009
+ * @returns 如果是后代则返回true
2561
3010
  */
2562
- forceProcessDelayedRecycle(): void;
3011
+ isDescendantOf(entity: Entity): boolean;
2563
3012
  /**
2564
- * 清理过期的延迟回收项
2565
- *
2566
- * 将超过延迟时间的回收项真正回收到空闲列表中。
3013
+ * 获取层次深度
2567
3014
  *
2568
- * @param forceAll 是否强制处理所有延迟回收项
2569
- * @private
3015
+ * @returns 在层次结构中的深度(根实体为0)
2570
3016
  */
2571
- private _processDelayedRecycle;
3017
+ getDepth(): number;
2572
3018
  /**
2573
- * 预分配世代信息
3019
+ * 遍历所有子实体(深度优先)
2574
3020
  *
2575
- * @param startIndex 起始索引
2576
- * @param count 分配数量
2577
- * @private
3021
+ * @param callback - 对每个子实体执行的回调函数
3022
+ * @param recursive - 是否递归遍历
2578
3023
  */
2579
- private _preAllocateGenerations;
3024
+ forEachChild(callback: (child: Entity, index: number) => void, recursive?: boolean): void;
2580
3025
  /**
2581
- * 确保指定索引的世代信息存在
2582
- *
2583
- * @param index 索引
2584
- * @private
3026
+ * 活跃状态改变时的回调
2585
3027
  */
2586
- private _ensureGenerationCapacity;
3028
+ private onActiveChanged;
2587
3029
  /**
2588
- * 计算内存使用量
3030
+ * 更新实体
2589
3031
  *
2590
- * @returns 内存使用字节数
2591
- * @private
3032
+ * 调用所有组件的更新方法,并更新子实体。
2592
3033
  */
2593
- private _calculateMemoryUsage;
3034
+ update(): void;
2594
3035
  /**
2595
- * 打包索引和世代为32位ID
3036
+ * 销毁实体
2596
3037
  *
2597
- * @param index 索引(16位)
2598
- * @param generation 世代版本(16位)
2599
- * @returns 打包后的32位ID
2600
- * @private
3038
+ * 移除所有组件、子实体并标记为已销毁。
2601
3039
  */
2602
- private _packId;
3040
+ destroy(): void;
2603
3041
  /**
2604
- * 从ID中解包索引
3042
+ * 比较实体
2605
3043
  *
2606
- * @param id 32位ID
2607
- * @returns 索引部分(16位)
2608
- * @private
3044
+ * @param other - 另一个实体
3045
+ * @returns 比较结果
2609
3046
  */
2610
- private _unpackIndex;
3047
+ compareTo(other: Entity): number;
2611
3048
  /**
2612
- * 从ID中解包世代版本
3049
+ * 获取实体的字符串表示
2613
3050
  *
2614
- * @param id 32位ID
2615
- * @returns 世代版本部分(16位)
2616
- * @private
3051
+ * @returns 实体的字符串描述
2617
3052
  */
2618
- private _unpackGeneration;
3053
+ toString(): string;
2619
3054
  /**
2620
- * 内部ID有效性检查
3055
+ * 获取实体的调试信息(包含组件缓存信息)
2621
3056
  *
2622
- * @param index 索引
2623
- * @param generation 世代版本
2624
- * @returns 是否有效
2625
- * @private
3057
+ * @returns 包含实体详细信息的对象
2626
3058
  */
2627
- private _isValidId;
3059
+ getDebugInfo(): {
3060
+ name: string;
3061
+ id: number;
3062
+ enabled: boolean;
3063
+ active: boolean;
3064
+ activeInHierarchy: boolean;
3065
+ destroyed: boolean;
3066
+ componentCount: number;
3067
+ componentTypes: string[];
3068
+ componentMask: string;
3069
+ parentId: number | null;
3070
+ childCount: number;
3071
+ childIds: number[];
3072
+ depth: number;
3073
+ indexMappingSize: number;
3074
+ };
2628
3075
  }
2629
3076
 
2630
3077
  /**
2631
- * 组件索引类型
2632
- */
2633
- declare enum IndexType {
2634
- /** 哈希索引 - 最快查找 */
2635
- HASH = "hash",
2636
- /** 位图索引 - 内存高效 */
2637
- BITMAP = "bitmap",
2638
- /** 排序索引 - 支持范围查询 */
2639
- SORTED = "sorted"
2640
- }
2641
- /**
2642
- * 索引统计信息
2643
- */
2644
- interface IndexStats {
2645
- /** 索引类型 */
2646
- type: IndexType;
2647
- /** 索引大小 */
2648
- size: number;
2649
- /** 内存使用量(字节) */
2650
- memoryUsage: number;
2651
- /** 查询次数 */
2652
- queryCount: number;
2653
- /** 平均查询时间(毫秒) */
2654
- avgQueryTime: number;
2655
- /** 最后更新时间 */
2656
- lastUpdated: number;
2657
- }
2658
- /**
2659
- * 组件索引接口
2660
- */
2661
- interface IComponentIndex {
2662
- /** 索引类型 */
2663
- readonly type: IndexType;
2664
- /** 添加实体到索引 */
2665
- addEntity(entity: Entity): void;
2666
- /** 从索引中移除实体 */
2667
- removeEntity(entity: Entity): void;
2668
- /** 查询包含指定组件的实体 */
2669
- query(componentType: ComponentType): Set<Entity>;
2670
- /** 批量查询多个组件 */
2671
- queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
2672
- /** 清空索引 */
2673
- clear(): void;
2674
- /** 获取索引统计信息 */
2675
- getStats(): IndexStats;
2676
- }
2677
- /**
2678
- * 哈希索引实现
2679
- *
2680
- * 使用Map数据结构,提供O(1)的查找性能。
2681
- * 适合大多数查询场景。
3078
+ * 查询条件类型
2682
3079
  */
2683
- declare class HashComponentIndex implements IComponentIndex {
2684
- readonly type = IndexType.HASH;
2685
- private _componentToEntities;
2686
- private _entityToComponents;
2687
- private _queryCount;
2688
- private _totalQueryTime;
2689
- private _lastUpdated;
2690
- addEntity(entity: Entity): void;
2691
- removeEntity(entity: Entity): void;
2692
- query(componentType: ComponentType): Set<Entity>;
2693
- queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
2694
- clear(): void;
2695
- getStats(): IndexStats;
3080
+ interface QueryCondition {
3081
+ all: ComponentType[];
3082
+ any: ComponentType[];
3083
+ none: ComponentType[];
3084
+ tag?: number;
3085
+ name?: string;
3086
+ component?: ComponentType;
2696
3087
  }
2697
3088
  /**
2698
- * 位图索引实现
3089
+ * 实体匹配条件描述符
2699
3090
  *
2700
- * 使用位操作进行快速集合运算,内存效率高。
2701
- * 适合有限组件类型和大量实体的场景。
2702
- */
2703
- declare class BitmapComponentIndex implements IComponentIndex {
2704
- readonly type = IndexType.BITMAP;
2705
- private _componentTypeToBit;
2706
- private _entityToBitmap;
2707
- private _bitToEntities;
2708
- private _nextBit;
2709
- private _queryCount;
2710
- private _totalQueryTime;
2711
- private _lastUpdated;
2712
- addEntity(entity: Entity): void;
2713
- removeEntity(entity: Entity): void;
2714
- query(componentType: ComponentType): Set<Entity>;
2715
- queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
2716
- clear(): void;
2717
- getStats(): IndexStats;
2718
- }
2719
- /**
2720
- * 智能组件索引管理器
3091
+ * 用于描述实体查询条件,不执行实际查询
2721
3092
  *
2722
- * 根据使用模式自动选择最优的索引策略。
2723
- * 支持动态切换索引类型以获得最佳性能。
2724
- */
2725
- declare class ComponentIndexManager {
2726
- private _activeIndex;
2727
- private _indexHistory;
2728
- private _autoOptimize;
2729
- private _optimizationThreshold;
2730
- constructor(initialType?: IndexType);
3093
+ * @example
3094
+ * ```typescript
3095
+ * const matcher = Matcher.all(Position, Velocity)
3096
+ * .any(Health, Shield)
3097
+ * .none(Dead);
3098
+ *
3099
+ * // 获取查询条件
3100
+ * const condition = matcher.getCondition();
3101
+ * ```
3102
+ */
3103
+ declare class Matcher {
3104
+ private readonly condition;
3105
+ private constructor();
2731
3106
  /**
2732
- * 添加实体到索引
3107
+ * 创建匹配器,要求所有指定的组件
3108
+ * @param types 组件类型
2733
3109
  */
2734
- addEntity(entity: Entity): void;
3110
+ static all(...types: ComponentType[]): Matcher;
2735
3111
  /**
2736
- * 从索引中移除实体
3112
+ * 创建匹配器,要求至少一个指定的组件
3113
+ * @param types 组件类型
2737
3114
  */
2738
- removeEntity(entity: Entity): void;
3115
+ static any(...types: ComponentType[]): Matcher;
2739
3116
  /**
2740
- * 查询包含指定组件的实体
3117
+ * 创建匹配器,排除指定的组件
3118
+ * @param types 组件类型
2741
3119
  */
2742
- query(componentType: ComponentType): Set<Entity>;
3120
+ static none(...types: ComponentType[]): Matcher;
2743
3121
  /**
2744
- * 批量查询多个组件
3122
+ * 创建按标签查询的匙配器
3123
+ * @param tag 标签值
2745
3124
  */
2746
- queryMultiple(componentTypes: ComponentType[], operation: 'AND' | 'OR'): Set<Entity>;
3125
+ static byTag(tag: number): Matcher;
2747
3126
  /**
2748
- * 手动切换索引类型
3127
+ * 创建按名称查询的匙配器
3128
+ * @param name 实体名称
2749
3129
  */
2750
- switchIndexType(type: IndexType): void;
3130
+ static byName(name: string): Matcher;
2751
3131
  /**
2752
- * 启用/禁用自动优化
3132
+ * 创建单组件查询的匙配器
3133
+ * @param componentType 组件类型
2753
3134
  */
2754
- setAutoOptimize(enabled: boolean): void;
3135
+ static byComponent(componentType: ComponentType): Matcher;
2755
3136
  /**
2756
- * 获取当前索引统计信息
3137
+ * 创建复杂查询构建器
2757
3138
  */
2758
- getStats(): IndexStats;
3139
+ static complex(): Matcher;
2759
3140
  /**
2760
- * 获取所有索引类型的历史统计信息
3141
+ * 创建空匙配器(向后兼容)
2761
3142
  */
2762
- getAllStats(): Map<IndexType, IndexStats>;
3143
+ static empty(): Matcher;
2763
3144
  /**
2764
- * 清空索引
3145
+ * 必须包含所有指定组件
3146
+ * @param types 组件类型
2765
3147
  */
2766
- clear(): void;
3148
+ all(...types: ComponentType[]): Matcher;
2767
3149
  /**
2768
- * 创建指定类型的索引
3150
+ * 必须包含至少一个指定组件
3151
+ * @param types 组件类型
2769
3152
  */
2770
- private createIndex;
3153
+ any(...types: ComponentType[]): Matcher;
2771
3154
  /**
2772
- * 检查是否需要优化索引
3155
+ * 不能包含任何指定组件
3156
+ * @param types 组件类型
2773
3157
  */
2774
- private checkOptimization;
2775
- }
2776
-
2777
- /**
2778
- * 原型标识符
2779
- */
2780
- type ArchetypeId = string;
2781
- /**
2782
- * 原型数据结构
2783
- */
2784
- interface Archetype {
2785
- /** 原型唯一标识符 */
2786
- id: ArchetypeId;
2787
- /** 包含的组件类型 */
2788
- componentTypes: ComponentType[];
2789
- /** 属于该原型的实体列表 */
2790
- entities: Entity[];
2791
- /** 原型创建时间 */
2792
- createdAt: number;
2793
- /** 最后更新时间 */
2794
- updatedAt: number;
2795
- }
2796
- /**
2797
- * 原型查询结果
2798
- */
2799
- interface ArchetypeQueryResult {
2800
- /** 匹配的原型列表 */
2801
- archetypes: Archetype[];
2802
- /** 所有匹配实体的总数 */
2803
- totalEntities: number;
2804
- /** 查询执行时间(毫秒) */
2805
- executionTime: number;
2806
- /** 是否使用了缓存 */
2807
- fromCache: boolean;
2808
- }
2809
- /**
2810
- * Archetype系统
2811
- *
2812
- * 根据实体的组件组合将实体分组到不同的原型中,提供高效的查询性能。
2813
- */
2814
- declare class ArchetypeSystem {
2815
- /** 所有原型的映射表 */
2816
- private _archetypes;
2817
- /** 实体到原型的映射 */
2818
- private _entityToArchetype;
2819
- /** 组件类型到原型的映射 */
2820
- private _componentToArchetypes;
2821
- /** 查询缓存 */
2822
- private _queryCache;
2823
- private _cacheTimeout;
2824
- private _maxCacheSize;
3158
+ none(...types: ComponentType[]): Matcher;
2825
3159
  /**
2826
- * 添加实体到原型系统
3160
+ * 排除指定组件(别名方法)
3161
+ * @param types 组件类型
2827
3162
  */
2828
- addEntity(entity: Entity): void;
3163
+ exclude(...types: ComponentType[]): Matcher;
2829
3164
  /**
2830
- * 从原型系统中移除实体
3165
+ * 至少包含其中之一(别名方法)
3166
+ * @param types 组件类型
2831
3167
  */
2832
- removeEntity(entity: Entity): void;
3168
+ one(...types: ComponentType[]): Matcher;
2833
3169
  /**
2834
- * 查询包含指定组件组合的原型
3170
+ * 按标签查询
3171
+ * @param tag 标签值
2835
3172
  */
2836
- queryArchetypes(componentTypes: ComponentType[], operation?: 'AND' | 'OR'): ArchetypeQueryResult;
3173
+ withTag(tag: number): Matcher;
2837
3174
  /**
2838
- * 获取实体所属的原型
3175
+ * 按名称查询
3176
+ * @param name 实体名称
2839
3177
  */
2840
- getEntityArchetype(entity: Entity): Archetype | undefined;
3178
+ withName(name: string): Matcher;
2841
3179
  /**
2842
- * 获取所有原型
3180
+ * 单组件查询
3181
+ * @param componentType 组件类型
2843
3182
  */
2844
- getAllArchetypes(): Archetype[];
3183
+ withComponent(componentType: ComponentType): Matcher;
2845
3184
  /**
2846
- * 清空所有数据
3185
+ * 移除标签条件
2847
3186
  */
2848
- clear(): void;
3187
+ withoutTag(): Matcher;
2849
3188
  /**
2850
- * 获取实体的组件类型列表
3189
+ * 移除名称条件
2851
3190
  */
2852
- private getEntityComponentTypes;
3191
+ withoutName(): Matcher;
2853
3192
  /**
2854
- * 生成原型ID
3193
+ * 移除单组件条件
2855
3194
  */
2856
- private generateArchetypeId;
3195
+ withoutComponent(): Matcher;
2857
3196
  /**
2858
- * 创建新原型
3197
+ * 获取查询条件(只读)
2859
3198
  */
2860
- private createArchetype;
3199
+ getCondition(): Readonly<QueryCondition>;
2861
3200
  /**
2862
- * 检查原型是否包含所有指定组件
3201
+ * 检查是否为空条件
2863
3202
  */
2864
- private archetypeContainsAllComponents;
3203
+ isEmpty(): boolean;
2865
3204
  /**
2866
- * 更新组件索引
3205
+ * 重置所有条件
2867
3206
  */
2868
- private updateComponentIndexes;
3207
+ reset(): Matcher;
2869
3208
  /**
2870
- * 使查询缓存失效
3209
+ * 克隆匹配器
2871
3210
  */
2872
- private invalidateQueryCache;
3211
+ clone(): Matcher;
3212
+ /**
3213
+ * 字符串表示
3214
+ */
3215
+ toString(): string;
2873
3216
  }
2874
3217
 
2875
3218
  /**
2876
- * 实体查询结果接口
2877
- */
2878
- interface QueryResult {
2879
- entities: Entity[];
2880
- count: number;
2881
- /** 查询执行时间(毫秒) */
2882
- executionTime: number;
2883
- /** 是否来自缓存 */
2884
- fromCache: boolean;
2885
- }
2886
- /**
2887
- * 高性能实体查询系统
2888
- *
2889
- * 提供快速的实体查询功能,支持按组件类型、标签、名称等多种方式查询实体。
2890
- * 系统采用多级索引和智能缓存机制,确保在大量实体场景下的查询性能。
3219
+ * 实体系统的基类
2891
3220
  *
2892
- * 主要特性:
2893
- * - 支持单组件和多组件查询
2894
- * - 自动索引管理和缓存优化
2895
- * - WebAssembly计算加速(如果可用)
2896
- * - 详细的性能统计信息
3221
+ * 用于处理一组符合特定条件的实体。系统是ECS架构中的逻辑处理单元,
3222
+ * 负责对拥有特定组件组合的实体执行业务逻辑。
2897
3223
  *
2898
3224
  * @example
2899
3225
  * ```typescript
2900
- * // 查询所有包含Position和Velocity组件的实体
2901
- * const movingEntities = querySystem.queryAll(PositionComponent, VelocityComponent);
3226
+ * class MovementSystem extends EntitySystem {
3227
+ * constructor() {
3228
+ * super(Transform, Velocity);
3229
+ * }
2902
3230
  *
2903
- * // 查询特定标签的实体
2904
- * const playerEntities = querySystem.queryByTag(PLAYER_TAG);
3231
+ * protected process(entities: Entity[]): void {
3232
+ * for (const entity of entities) {
3233
+ * const transform = entity.getComponent(Transform);
3234
+ * const velocity = entity.getComponent(Velocity);
3235
+ * transform.position.add(velocity.value);
3236
+ * }
3237
+ * }
3238
+ * }
2905
3239
  * ```
2906
3240
  */
2907
- declare class QuerySystem {
2908
- private entities;
2909
- private entityIndex;
2910
- private indexDirty;
2911
- private queryCache;
2912
- private cacheMaxSize;
2913
- private cacheTimeout;
2914
- private componentPoolManager;
2915
- private bitMaskOptimizer;
2916
- private indexUpdateBatcher;
2917
- private componentIndexManager;
2918
- private archetypeSystem;
2919
- private dirtyTrackingSystem;
2920
- private queryStats;
2921
- constructor();
2922
- /**
2923
- * 设置实体列表并重建索引
2924
- *
2925
- * 当实体集合发生大规模变化时调用此方法。
2926
- * 系统将重新构建所有索引以确保查询性能。
2927
- *
2928
- * @param entities 新的实体列表
2929
- */
2930
- setEntities(entities: Entity[]): void;
2931
- /**
2932
- * 添加单个实体到查询系统
2933
- *
2934
- * 将新实体添加到查询系统中,并自动更新相关索引。
2935
- * 为了提高批量添加性能,可以延迟缓存清理。
2936
- *
2937
- * @param entity 要添加的实体
2938
- * @param deferCacheClear 是否延迟缓存清理(用于批量操作)
2939
- */
2940
- addEntity(entity: Entity, deferCacheClear?: boolean): void;
2941
- /**
2942
- * 批量添加实体
2943
- *
2944
- * 高效地批量添加多个实体,减少缓存清理次数。
2945
- * 使用Set来避免O(n)的重复检查。
2946
- *
2947
- * @param entities 要添加的实体列表
2948
- */
2949
- addEntities(entities: Entity[]): void;
2950
- /**
2951
- * 批量添加实体(无重复检查版本)
2952
- *
2953
- * 假设所有实体都是新的,跳过重复检查以获得最大性能。
2954
- * 仅在确保没有重复实体时使用。
2955
- *
2956
- * @param entities 要添加的实体列表
2957
- */
2958
- addEntitiesUnchecked(entities: Entity[]): void;
2959
- /**
2960
- * 从查询系统移除实体
2961
- *
2962
- * 从查询系统中移除指定实体,并清理相关索引。
2963
- *
2964
- * @param entity 要移除的实体
2965
- */
2966
- removeEntity(entity: Entity): void;
2967
- /**
2968
- * 将实体添加到各种索引中(优化版本)
2969
- */
2970
- private addEntityToIndexes;
3241
+ declare abstract class EntitySystem implements ISystemBase {
3242
+ private _updateOrder;
3243
+ private _enabled;
3244
+ private _performanceMonitor;
3245
+ private _systemName;
3246
+ private _initialized;
3247
+ private _matcher;
3248
+ private _trackedEntities;
3249
+ private _lastQueryResult;
2971
3250
  /**
2972
- * 从各种索引中移除实体
3251
+ * 获取系统处理的实体列表(动态查询)
2973
3252
  */
2974
- private removeEntityFromIndexes;
3253
+ get entities(): readonly Entity[];
2975
3254
  /**
2976
- * 重建所有索引
2977
- *
2978
- * 清空并重新构建所有查询索引。
2979
- * 通常在大量实体变更后调用以确保索引一致性。
3255
+ * 获取系统的更新时序
2980
3256
  */
2981
- private rebuildIndexes;
3257
+ get updateOrder(): number;
3258
+ set updateOrder(value: number);
2982
3259
  /**
2983
- * 查询包含所有指定组件的实体
2984
- *
2985
- * 返回同时包含所有指定组件类型的实体列表。
2986
- * 系统会自动选择最高效的查询策略,包括索引查找和缓存机制。
2987
- *
2988
- * @param componentTypes 要查询的组件类型列表
2989
- * @returns 查询结果,包含匹配的实体和性能信息
2990
- *
2991
- * @example
2992
- * ```typescript
2993
- * // 查询同时具有位置和速度组件的实体
2994
- * const result = querySystem.queryAll(PositionComponent, VelocityComponent);
2995
- * console.log(`找到 ${result.count} 个移动实体`);
2996
- * ```
3260
+ * 获取系统的启用状态
2997
3261
  */
2998
- queryAll(...componentTypes: ComponentType[]): QueryResult;
3262
+ get enabled(): boolean;
2999
3263
  /**
3000
- * 多组件查询算法
3001
- *
3002
- * 针对多组件查询场景的高效算法实现。
3003
- * 通过选择最小的组件集合作为起点,减少需要检查的实体数量。
3004
- *
3005
- * @param componentTypes 组件类型列表
3006
- * @returns 匹配的实体列表
3264
+ * 设置系统的启用状态
3007
3265
  */
3008
- private queryMultipleComponents;
3266
+ set enabled(value: boolean);
3009
3267
  /**
3010
- * 线性扫描查询
3011
- *
3012
- * 当索引不可用时的备用查询方法。
3013
- * 遍历所有实体进行组件匹配检查。
3014
- *
3015
- * @param componentTypes 组件类型列表
3016
- * @returns 匹配的实体列表
3268
+ * 获取系统名称
3017
3269
  */
3018
- private queryByLinearScan;
3270
+ get systemName(): string;
3271
+ constructor(matcher?: Matcher);
3272
+ private _scene;
3019
3273
  /**
3020
- * 查询包含任意指定组件的实体
3021
- *
3022
- * 返回包含任意一个指定组件类型的实体列表。
3023
- * 使用集合合并算法确保高效的查询性能。
3024
- *
3025
- * @param componentTypes 要查询的组件类型列表
3026
- * @returns 查询结果,包含匹配的实体和性能信息
3027
- *
3028
- * @example
3029
- * ```typescript
3030
- * // 查询具有武器或护甲组件的实体
3031
- * const result = querySystem.queryAny(WeaponComponent, ArmorComponent);
3032
- * console.log(`找到 ${result.count} 个装备实体`);
3033
- * ```
3274
+ * 这个系统所属的场景
3034
3275
  */
3035
- queryAny(...componentTypes: ComponentType[]): QueryResult;
3276
+ get scene(): Scene | null;
3277
+ set scene(value: Scene | null);
3036
3278
  /**
3037
- * 查询不包含任何指定组件的实体
3038
- *
3039
- * 返回不包含任何指定组件类型的实体列表。
3040
- * 适用于排除特定类型实体的查询场景。
3041
- *
3042
- * @param componentTypes 要排除的组件类型列表
3043
- * @returns 查询结果,包含匹配的实体和性能信息
3044
- *
3045
- * @example
3046
- * ```typescript
3047
- * // 查询不具有AI和玩家控制组件的实体(如静态物体)
3048
- * const result = querySystem.queryNone(AIComponent, PlayerControlComponent);
3049
- * console.log(`找到 ${result.count} 个静态实体`);
3050
- * ```
3279
+ * 获取实体匹配器
3051
3280
  */
3052
- queryNone(...componentTypes: ComponentType[]): QueryResult;
3281
+ get matcher(): Matcher;
3053
3282
  /**
3054
- * 按标签查询实体
3055
- *
3056
- * 返回具有指定标签的所有实体。
3057
- * 标签查询使用专用索引,具有很高的查询性能。
3058
- *
3059
- * @param tag 要查询的标签值
3060
- * @returns 查询结果,包含匹配的实体和性能信息
3061
- *
3062
- * @example
3063
- * ```typescript
3064
- * // 查询所有玩家实体
3065
- * const players = querySystem.queryByTag(PLAYER_TAG);
3066
- * ```
3283
+ * 设置更新时序
3284
+ * @param order 更新时序
3067
3285
  */
3068
- queryByTag(tag: number): QueryResult;
3286
+ setUpdateOrder(order: number): void;
3069
3287
  /**
3070
- * 按名称查询实体
3071
- *
3072
- * 返回具有指定名称的所有实体。
3073
- * 名称查询使用专用索引,适用于查找特定的命名实体。
3074
- *
3075
- * @param name 要查询的实体名称
3076
- * @returns 查询结果,包含匹配的实体和性能信息
3288
+ * 系统初始化(框架调用)
3077
3289
  *
3078
- * @example
3079
- * ```typescript
3080
- * // 查找名为"Player"的实体
3081
- * const player = querySystem.queryByName("Player");
3082
- * ```
3290
+ * 在系统创建时调用。框架内部使用,用户不应直接调用。
3083
3291
  */
3084
- queryByName(name: string): QueryResult;
3292
+ initialize(): void;
3085
3293
  /**
3086
- * 按单个组件类型查询实体
3087
- *
3088
- * 返回包含指定组件类型的所有实体。
3089
- * 这是最基础的查询方法,具有最高的查询性能。
3294
+ * 系统初始化回调
3090
3295
  *
3091
- * @param componentType 要查询的组件类型
3092
- * @returns 查询结果,包含匹配的实体和性能信息
3296
+ * 子类可以重写此方法进行初始化操作。
3297
+ */
3298
+ protected onInitialize(): void;
3299
+ /**
3300
+ * 重置系统状态
3093
3301
  *
3094
- * @example
3095
- * ```typescript
3096
- * // 查询所有具有位置组件的实体
3097
- * const entitiesWithPosition = querySystem.queryByComponent(PositionComponent);
3098
- * ```
3302
+ * 当系统从场景中移除时调用,重置初始化状态以便重新添加时能正确初始化。
3099
3303
  */
3100
- queryByComponent<T extends Component>(componentType: ComponentType<T>): QueryResult;
3304
+ reset(): void;
3101
3305
  /**
3102
- * 从缓存获取查询结果
3306
+ * 查询匹配的实体
3103
3307
  */
3104
- private getFromCache;
3308
+ private queryEntities;
3105
3309
  /**
3106
- * 添加查询结果到缓存
3310
+ * 检查是否为单一条件查询
3107
3311
  */
3108
- private addToCache;
3312
+ private isSingleCondition;
3109
3313
  /**
3110
- * 清理缓存
3314
+ * 执行单一条件查询
3111
3315
  */
3112
- private cleanupCache;
3316
+ private executeSingleConditionQuery;
3113
3317
  /**
3114
- * 清除所有查询缓存
3318
+ * 执行复合查询
3115
3319
  */
3116
- private clearQueryCache;
3320
+ private executeComplexQuery;
3117
3321
  /**
3118
- * 公共方法:清理查询缓存
3322
+ * 更新系统
3119
3323
  *
3120
- * 用于外部调用清理缓存,通常在批量操作后使用。
3324
+ * 在每帧调用,处理系统的主要逻辑。
3121
3325
  */
3122
- clearCache(): void;
3326
+ update(): void;
3123
3327
  /**
3124
- * 批量更新实体组件
3125
- *
3126
- * 对大量实体进行批量组件更新操作。
3127
- *
3128
- * @param updates 更新操作列表,包含实体ID和新的组件掩码
3328
+ * 后期更新系统
3129
3329
  *
3130
- * @example
3131
- * ```typescript
3132
- * // 批量更新实体的组件配置
3133
- * const updates = [
3134
- * { entityId: 1, componentMask: BigInt(0b1011) },
3135
- * { entityId: 2, componentMask: BigInt(0b1101) }
3136
- * ];
3137
- * querySystem.batchUpdateComponents(updates);
3138
- * ```
3330
+ * 在所有系统的update方法执行完毕后调用。
3139
3331
  */
3140
- batchUpdateComponents(updates: Array<{
3141
- entityId: number;
3142
- componentMask: bigint;
3143
- }>): void;
3332
+ lateUpdate(): void;
3144
3333
  /**
3145
- * 创建组件掩码
3146
- *
3147
- * 根据组件类型列表生成对应的位掩码。
3148
- * 使用位掩码优化器进行缓存和预计算。
3334
+ * 在系统处理开始前调用
3149
3335
  *
3150
- * @param componentTypes 组件类型列表
3151
- * @returns 生成的位掩码
3336
+ * 子类可以重写此方法进行预处理操作。
3152
3337
  */
3153
- private createComponentMask;
3338
+ protected onBegin(): void;
3154
3339
  /**
3155
- * 获取系统统计信息
3340
+ * 处理实体列表
3156
3341
  *
3157
- * 返回查询系统的详细统计信息,包括实体数量、索引状态、
3158
- * 查询性能统计等,用于性能监控和调试。
3342
+ * 系统的核心逻辑,子类必须实现此方法来定义具体的处理逻辑。
3159
3343
  *
3160
- * @returns 系统统计信息对象
3344
+ * @param entities 要处理的实体列表
3161
3345
  */
3162
- getStats(): {
3163
- entityCount: number;
3164
- indexStats: {
3165
- maskIndexSize: number;
3166
- componentIndexSize: number;
3167
- tagIndexSize: number;
3168
- nameIndexSize: number;
3169
- };
3170
- queryStats: {
3171
- totalQueries: number;
3172
- cacheHits: number;
3173
- indexHits: number;
3174
- linearScans: number;
3175
- archetypeHits: number;
3176
- dirtyChecks: number;
3177
- cacheHitRate: string;
3178
- };
3179
- optimizationStats: {
3180
- componentIndex: any;
3181
- archetypeSystem: any;
3182
- dirtyTracking: any;
3183
- };
3184
- cacheStats: {
3185
- size: number;
3186
- hitRate: string;
3187
- };
3188
- };
3346
+ protected process(entities: Entity[]): void;
3189
3347
  /**
3190
- * 切换组件索引类型
3348
+ * 后期处理实体列表
3191
3349
  *
3192
- * @param indexType 新的索引类型
3193
- */
3194
- switchComponentIndexType(indexType: IndexType): void;
3195
- /**
3196
- * 配置脏标记系统
3350
+ * 在主要处理逻辑之后执行,子类可以重写此方法。
3197
3351
  *
3198
- * @param batchSize 批处理大小
3199
- * @param maxProcessingTime 最大处理时间
3200
- */
3201
- configureDirtyTracking(batchSize: number, maxProcessingTime: number): void;
3202
- /**
3203
- * 手动触发性能优化
3204
- */
3205
- optimizePerformance(): void;
3206
- /**
3207
- * 开始新的帧
3352
+ * @param entities 要处理的实体列表
3208
3353
  */
3209
- beginFrame(): void;
3354
+ protected lateProcess(entities: Entity[]): void;
3210
3355
  /**
3211
- * 结束当前帧
3356
+ * 系统处理完毕后调用
3357
+ *
3358
+ * 子类可以重写此方法进行后处理操作。
3212
3359
  */
3213
- endFrame(): void;
3360
+ protected onEnd(): void;
3214
3361
  /**
3215
- * 标记实体组件已修改(用于脏标记追踪)
3362
+ * 检查系统是否需要处理
3216
3363
  *
3217
- * @param entity 修改的实体
3218
- * @param componentTypes 修改的组件类型
3364
+ * 在启用系统时有用,但仅偶尔需要处理。
3365
+ * 这只影响处理,不影响事件或订阅列表。
3366
+ *
3367
+ * @returns 如果系统应该处理,则为true,如果不处理则为false
3219
3368
  */
3220
- markEntityDirty(entity: Entity, componentTypes: ComponentType[]): void;
3369
+ protected onCheckProcessing(): boolean;
3221
3370
  /**
3222
- * 获取实体所属的原型信息
3371
+ * 获取系统的性能数据
3223
3372
  *
3224
- * @param entity 要查询的实体
3373
+ * @returns 性能数据或undefined
3225
3374
  */
3226
- getEntityArchetype(entity: Entity): Archetype | undefined;
3227
- }
3228
- /**
3229
- * 查询构建器
3230
- *
3231
- * 提供链式API来构建复杂的实体查询条件。
3232
- * 支持组合多种查询条件,创建灵活的查询表达式。
3233
- *
3234
- * @example
3235
- * ```typescript
3236
- * const result = new QueryBuilder(querySystem)
3237
- * .withAll(PositionComponent, VelocityComponent)
3238
- * .without(DeadComponent)
3239
- * .execute();
3240
- * ```
3241
- */
3242
- declare class QueryBuilder {
3243
- private conditions;
3244
- private querySystem;
3245
- constructor(querySystem: QuerySystem);
3375
+ getPerformanceData(): PerformanceData | undefined;
3246
3376
  /**
3247
- * 添加"必须包含所有组件"条件
3377
+ * 获取系统的性能统计
3248
3378
  *
3249
- * @param componentTypes 必须包含的组件类型
3250
- * @returns 查询构建器实例,支持链式调用
3379
+ * @returns 性能统计或undefined
3251
3380
  */
3252
- withAll(...componentTypes: ComponentType[]): QueryBuilder;
3381
+ getPerformanceStats(): PerformanceStats | undefined;
3253
3382
  /**
3254
- * 添加"必须包含任意组件"条件
3255
- *
3256
- * @param componentTypes 必须包含其中任意一个的组件类型
3257
- * @returns 查询构建器实例,支持链式调用
3383
+ * 重置系统的性能数据
3258
3384
  */
3259
- withAny(...componentTypes: ComponentType[]): QueryBuilder;
3385
+ resetPerformanceData(): void;
3260
3386
  /**
3261
- * 添加"不能包含任何组件"条件
3387
+ * 获取系统信息的字符串表示
3262
3388
  *
3263
- * @param componentTypes 不能包含的组件类型
3264
- * @returns 查询构建器实例,支持链式调用
3389
+ * @returns 系统信息字符串
3265
3390
  */
3266
- without(...componentTypes: ComponentType[]): QueryBuilder;
3391
+ toString(): string;
3267
3392
  /**
3268
- * 执行查询并返回结果
3269
- *
3270
- * 根据已添加的查询条件执行实体查询。
3271
- *
3272
- * @returns 查询结果,包含匹配的实体和性能信息
3393
+ * 更新实体跟踪,检查新增和移除的实体
3273
3394
  */
3274
- execute(): QueryResult;
3395
+ private updateEntityTracking;
3275
3396
  /**
3276
- * 创建组件掩码
3397
+ * 当实体被添加到系统时调用
3398
+ *
3399
+ * 子类可以重写此方法来处理实体添加事件。
3400
+ *
3401
+ * @param entity 被添加的实体
3277
3402
  */
3278
- private createComponentMask;
3403
+ protected onAdded(_entity: Entity): void;
3279
3404
  /**
3280
- * 重置查询构建器
3405
+ * 当实体从系统中移除时调用
3281
3406
  *
3282
- * 清除所有已添加的查询条件,重新开始构建查询。
3407
+ * 子类可以重写此方法来处理实体移除事件。
3283
3408
  *
3284
- * @returns 查询构建器实例,支持链式调用
3409
+ * @param entity 被移除的实体
3285
3410
  */
3286
- reset(): QueryBuilder;
3411
+ protected onRemoved(_entity: Entity): void;
3287
3412
  }
3288
3413
 
3289
3414
  /**
3290
- * 事件处理器函数类型
3291
- */
3292
- type EventHandler<T = any> = (event: T) => void;
3293
- /**
3294
- * 异步事件处理器函数类型
3295
- */
3296
- type AsyncEventHandler<T = any> = (event: T) => Promise<void>;
3297
- /**
3298
- * 事件监听器配置
3299
- */
3300
- interface EventListenerConfig {
3301
- /** 是否只执行一次 */
3302
- once?: boolean;
3303
- /** 优先级(数字越大优先级越高) */
3304
- priority?: number;
3305
- /** 是否异步执行 */
3306
- async?: boolean;
3307
- /** 执行上下文 */
3308
- context?: any;
3309
- }
3310
- /**
3311
- * 事件统计信息
3312
- */
3313
- interface EventStats {
3314
- /** 事件类型 */
3315
- eventType: string;
3316
- /** 监听器数量 */
3317
- listenerCount: number;
3318
- /** 触发次数 */
3319
- triggerCount: number;
3320
- /** 总执行时间(毫秒) */
3321
- totalExecutionTime: number;
3322
- /** 平均执行时间(毫秒) */
3323
- averageExecutionTime: number;
3324
- /** 最后触发时间 */
3325
- lastTriggerTime: number;
3326
- }
3327
- /**
3328
- * 事件批处理配置
3329
- */
3330
- interface EventBatchConfig {
3331
- /** 批处理大小 */
3332
- batchSize: number;
3333
- /** 批处理延迟(毫秒) */
3334
- delay: number;
3335
- /** 是否启用批处理 */
3336
- enabled: boolean;
3337
- }
3338
- /**
3339
- * 类型安全的高性能事件系统
3340
- * 支持同步/异步事件、优先级、批处理等功能
3415
+ * 实体处理器列表管理器
3416
+ * 管理场景中的所有实体系统
3341
3417
  */
3342
- declare class TypeSafeEventSystem {
3343
- private listeners;
3344
- private stats;
3345
- private batchQueue;
3346
- private batchTimers;
3347
- private batchConfigs;
3348
- private nextListenerId;
3349
- private isEnabled;
3350
- private maxListeners;
3418
+ declare class EntityProcessorList {
3419
+ private _processors;
3420
+ private _isDirty;
3351
3421
  /**
3352
- * 添加事件监听器
3353
- * @param eventType 事件类型
3354
- * @param handler 事件处理器
3355
- * @param config 监听器配置
3356
- * @returns 监听器ID(用于移除)
3422
+ * 设置为脏状态,需要重新排序
3357
3423
  */
3358
- on<T>(eventType: string, handler: EventHandler<T>, config?: EventListenerConfig): string;
3424
+ setDirty(): void;
3359
3425
  /**
3360
- * 添加一次性事件监听器
3361
- * @param eventType 事件类型
3362
- * @param handler 事件处理器
3363
- * @param config 监听器配置
3364
- * @returns 监听器ID
3426
+ * 添加实体处理器
3427
+ * @param processor 要添加的处理器
3365
3428
  */
3366
- once<T>(eventType: string, handler: EventHandler<T>, config?: EventListenerConfig): string;
3429
+ add(processor: EntitySystem): void;
3367
3430
  /**
3368
- * 添加异步事件监听器
3369
- * @param eventType 事件类型
3370
- * @param handler 异步事件处理器
3371
- * @param config 监听器配置
3372
- * @returns 监听器ID
3431
+ * 移除实体处理器
3432
+ * @param processor 要移除的处理器
3373
3433
  */
3374
- onAsync<T>(eventType: string, handler: AsyncEventHandler<T>, config?: EventListenerConfig): string;
3434
+ remove(processor: EntitySystem): void;
3375
3435
  /**
3376
- * 移除事件监听器
3377
- * @param eventType 事件类型
3378
- * @param listenerId 监听器ID
3379
- * @returns 是否成功移除
3436
+ * 获取指定类型的处理器
3437
+ * @param type 处理器类型
3380
3438
  */
3381
- off(eventType: string, listenerId: string): boolean;
3439
+ getProcessor<T extends EntitySystem>(type: new (...args: unknown[]) => T): T | null;
3382
3440
  /**
3383
- * 移除指定事件类型的所有监听器
3384
- * @param eventType 事件类型
3441
+ * 开始处理
3442
+ *
3443
+ * 对所有处理器进行排序以确保正确的执行顺序。
3385
3444
  */
3386
- offAll(eventType: string): void;
3445
+ begin(): void;
3387
3446
  /**
3388
- * 触发事件
3389
- * @param eventType 事件类型
3390
- * @param event 事件数据
3391
- * @returns Promise(如果有异步监听器)
3447
+ * 结束处理
3392
3448
  */
3393
- emit<T>(eventType: string, event: T): Promise<void>;
3449
+ end(): void;
3394
3450
  /**
3395
- * 同步触发事件(忽略异步监听器)
3396
- * @param eventType 事件类型
3397
- * @param event 事件数据
3451
+ * 更新所有处理器
3398
3452
  */
3399
- emitSync<T>(eventType: string, event: T): void;
3453
+ update(): void;
3400
3454
  /**
3401
- * 设置事件批处理配置
3402
- * @param eventType 事件类型
3403
- * @param config 批处理配置
3455
+ * 后期更新所有处理器
3404
3456
  */
3405
- setBatchConfig(eventType: string, config: EventBatchConfig): void;
3457
+ lateUpdate(): void;
3406
3458
  /**
3407
- * 立即处理指定事件类型的批处理队列
3408
- * @param eventType 事件类型
3459
+ * 排序处理器
3409
3460
  */
3410
- flushBatch(eventType: string): void;
3461
+ private sortProcessors;
3462
+ /** 获取处理器列表 */
3463
+ get processors(): EntitySystem[];
3464
+ /** 获取处理器数量 */
3465
+ get count(): number;
3466
+ }
3467
+
3468
+ /**
3469
+ * 世代式ID池管理器
3470
+ *
3471
+ * 用于管理实体ID的分配和回收,支持世代版本控制以防止悬空引用问题。
3472
+ * 世代式ID由索引和版本组成,当ID被回收时版本会递增,确保旧引用失效。
3473
+ *
3474
+ * 支持动态扩展,理论上可以支持到65535个索引(16位),每个索引65535个版本(16位)。
3475
+ * 总计可以处理超过42亿个独特的ID组合,完全满足ECS大规模实体需求。
3476
+ *
3477
+ * @example
3478
+ * ```typescript
3479
+ * const pool = new IdentifierPool();
3480
+ *
3481
+ * // 分配ID
3482
+ * const id = pool.checkOut(); // 例如: 65536 (版本1,索引0)
3483
+ *
3484
+ * // 回收ID
3485
+ * pool.checkIn(id);
3486
+ *
3487
+ * // 验证ID是否有效
3488
+ * const isValid = pool.isValid(id); // false,因为版本已递增
3489
+ * ```
3490
+ */
3491
+ declare class IdentifierPool {
3411
3492
  /**
3412
- * 获取事件统计信息
3413
- * @param eventType 事件类型(可选)
3414
- * @returns 统计信息
3493
+ * 下一个可用的索引
3415
3494
  */
3416
- getStats(eventType?: string): EventStats | Map<string, EventStats>;
3495
+ private _nextAvailableIndex;
3417
3496
  /**
3418
- * 重置统计信息
3419
- * @param eventType 事件类型(可选,不指定则重置所有)
3497
+ * 空闲的索引列表
3420
3498
  */
3421
- resetStats(eventType?: string): void;
3499
+ private _freeIndices;
3422
3500
  /**
3423
- * 启用/禁用事件系统
3424
- * @param enabled 是否启用
3501
+ * 每个索引对应的世代版本
3502
+ * 动态扩展的Map,按需分配内存
3425
3503
  */
3426
- setEnabled(enabled: boolean): void;
3504
+ private _generations;
3427
3505
  /**
3428
- * 检查是否有指定事件类型的监听器
3429
- * @param eventType 事件类型
3430
- * @returns 是否有监听器
3506
+ * 延迟回收队列
3507
+ * 防止在同一帧内立即重用ID,避免时序问题
3431
3508
  */
3432
- hasListeners(eventType: string): boolean;
3509
+ private _pendingRecycle;
3433
3510
  /**
3434
- * 获取指定事件类型的监听器数量
3435
- * @param eventType 事件类型
3436
- * @returns 监听器数量
3511
+ * 延迟回收时间(毫秒)
3437
3512
  */
3438
- getListenerCount(eventType: string): number;
3513
+ private _recycleDelay;
3439
3514
  /**
3440
- * 清空所有事件监听器和数据
3515
+ * 最大索引限制(16位)
3516
+ * 这是框架设计选择:16位索引 + 16位版本 = 32位ID,确保高效位操作
3517
+ * 不是硬件限制,而是性能和内存效率的权衡
3441
3518
  */
3442
- clear(): void;
3519
+ private static readonly MAX_INDEX;
3443
3520
  /**
3444
- * 设置每个事件类型的最大监听器数量
3445
- * @param max 最大数量
3521
+ * 最大世代限制(16位)
3446
3522
  */
3447
- setMaxListeners(max: number): void;
3523
+ private static readonly MAX_GENERATION;
3448
3524
  /**
3449
- * 添加监听器的内部实现
3450
- * @param eventType 事件类型
3451
- * @param handler 事件处理器
3452
- * @param config 配置
3453
- * @returns 监听器ID
3525
+ * 内存扩展块大小
3526
+ * 当需要更多内存时,一次性预分配的索引数量
3527
+ */
3528
+ private _expansionBlockSize;
3529
+ /**
3530
+ * 统计信息
3531
+ */
3532
+ private _stats;
3533
+ /**
3534
+ * 构造函数
3535
+ *
3536
+ * @param recycleDelay 延迟回收时间(毫秒),默认为100ms
3537
+ * @param expansionBlockSize 内存扩展块大小,默认为1024
3538
+ */
3539
+ constructor(recycleDelay?: number, expansionBlockSize?: number);
3540
+ /**
3541
+ * 获取一个可用的ID
3542
+ *
3543
+ * 返回一个32位ID,高16位为世代版本,低16位为索引。
3544
+ *
3545
+ * @returns 新分配的实体ID
3546
+ * @throws {Error} 当达到索引限制时抛出错误
3547
+ */
3548
+ checkOut(): number;
3549
+ /**
3550
+ * 回收一个ID
3551
+ *
3552
+ * 验证ID的有效性后,将其加入延迟回收队列。
3553
+ * ID不会立即可重用,而是在延迟时间后才真正回收。
3554
+ *
3555
+ * @param id 要回收的实体ID
3556
+ * @returns 是否成功回收(ID是否有效且未被重复回收)
3557
+ */
3558
+ checkIn(id: number): boolean;
3559
+ /**
3560
+ * 验证ID是否有效
3561
+ *
3562
+ * 检查ID的索引和世代版本是否匹配当前状态。
3563
+ *
3564
+ * @param id 要验证的实体ID
3565
+ * @returns ID是否有效
3566
+ */
3567
+ isValid(id: number): boolean;
3568
+ /**
3569
+ * 获取统计信息
3570
+ *
3571
+ * @returns 池的当前状态统计
3454
3572
  */
3455
- private addListener;
3573
+ getStats(): {
3574
+ /** 已分配的总索引数 */
3575
+ totalAllocated: number;
3576
+ /** 总计回收次数 */
3577
+ totalRecycled: number;
3578
+ /** 当前活跃实体数 */
3579
+ currentActive: number;
3580
+ /** 当前空闲的索引数 */
3581
+ currentlyFree: number;
3582
+ /** 等待回收的ID数 */
3583
+ pendingRecycle: number;
3584
+ /** 理论最大实体数(设计限制) */
3585
+ maxPossibleEntities: number;
3586
+ /** 当前使用的最大索引 */
3587
+ maxUsedIndex: number;
3588
+ /** 内存使用(字节) */
3589
+ memoryUsage: number;
3590
+ /** 内存扩展次数 */
3591
+ memoryExpansions: number;
3592
+ /** 平均世代版本 */
3593
+ averageGeneration: number;
3594
+ /** 世代存储大小 */
3595
+ generationStorageSize: number;
3596
+ };
3456
3597
  /**
3457
- * 执行事件的内部实现
3458
- * @param eventType 事件类型
3459
- * @param event 事件数据
3598
+ * 强制执行延迟回收处理
3599
+ *
3600
+ * 在某些情况下可能需要立即处理延迟回收队列,
3601
+ * 比如内存压力大或者需要精确的统计信息时。
3460
3602
  */
3461
- private executeEvent;
3603
+ forceProcessDelayedRecycle(): void;
3462
3604
  /**
3463
- * 按优先级排序监听器
3464
- * @param listeners 监听器数组
3465
- * @returns 排序后的监听器数组
3605
+ * 清理过期的延迟回收项
3606
+ *
3607
+ * 将超过延迟时间的回收项真正回收到空闲列表中。
3608
+ *
3609
+ * @param forceAll 是否强制处理所有延迟回收项
3610
+ * @private
3466
3611
  */
3467
- private sortListenersByPriority;
3612
+ private _processDelayedRecycle;
3468
3613
  /**
3469
- * 移除指定的监听器
3470
- * @param eventType 事件类型
3471
- * @param listenerIds 要移除的监听器ID数组
3614
+ * 预分配世代信息
3615
+ *
3616
+ * @param startIndex 起始索引
3617
+ * @param count 分配数量
3618
+ * @private
3472
3619
  */
3473
- private removeListeners;
3620
+ private _preAllocateGenerations;
3474
3621
  /**
3475
- * 添加事件到批处理队列
3476
- * @param eventType 事件类型
3477
- * @param event 事件数据
3622
+ * 确保指定索引的世代信息存在
3623
+ *
3624
+ * @param index 索引
3625
+ * @private
3478
3626
  */
3479
- private addToBatch;
3627
+ private _ensureGenerationCapacity;
3480
3628
  /**
3481
- * 处理批处理事件
3482
- * @param eventType 事件类型
3483
- * @param batch 批处理事件数组
3629
+ * 计算内存使用量
3630
+ *
3631
+ * @returns 内存使用字节数
3632
+ * @private
3484
3633
  */
3485
- private processBatch;
3634
+ private _calculateMemoryUsage;
3486
3635
  /**
3487
- * 清除指定事件类型的批处理
3488
- * @param eventType 事件类型
3636
+ * 打包索引和世代为32位ID
3637
+ *
3638
+ * @param index 索引(16位)
3639
+ * @param generation 世代版本(16位)
3640
+ * @returns 打包后的32位ID
3641
+ * @private
3489
3642
  */
3490
- private clearBatch;
3643
+ private _packId;
3491
3644
  /**
3492
- * 清除所有批处理
3645
+ * 从ID中解包索引
3646
+ *
3647
+ * @param id 32位ID
3648
+ * @returns 索引部分(16位)
3649
+ * @private
3493
3650
  */
3494
- private clearAllBatches;
3651
+ private _unpackIndex;
3495
3652
  /**
3496
- * 更新事件统计信息
3497
- * @param eventType 事件类型
3498
- * @param executionTime 执行时间
3653
+ * 从ID中解包世代版本
3654
+ *
3655
+ * @param id 32位ID
3656
+ * @returns 世代版本部分(16位)
3657
+ * @private
3499
3658
  */
3500
- private updateStats;
3659
+ private _unpackGeneration;
3501
3660
  /**
3502
- * 创建空的统计信息
3503
- * @param eventType 事件类型
3504
- * @returns 空的统计信息
3661
+ * 内部ID有效性检查
3662
+ *
3663
+ * @param index 索引
3664
+ * @param generation 世代版本
3665
+ * @returns 是否有效
3666
+ * @private
3505
3667
  */
3506
- private createEmptyStats;
3668
+ private _isValidId;
3507
3669
  }
3508
3670
 
3509
3671
  /**
@@ -3687,7 +3849,7 @@ declare class Scene {
3687
3849
  * 获取指定类型的EntitySystem处理器
3688
3850
  * @param type 处理器类型
3689
3851
  */
3690
- getEntityProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T | null;
3852
+ getEntityProcessor<T extends EntitySystem>(type: new (...args: unknown[]) => T): T | null;
3691
3853
  /**
3692
3854
  * 获取场景统计信息
3693
3855
  */
@@ -3863,13 +4025,13 @@ declare class SceneBuilder {
3863
4025
  * @param system 系统实例
3864
4026
  * @returns 场景构建器
3865
4027
  */
3866
- withSystem(system: any): SceneBuilder;
4028
+ withSystem(system: EntitySystem): SceneBuilder;
3867
4029
  /**
3868
4030
  * 批量添加系统
3869
4031
  * @param systems 系统数组
3870
4032
  * @returns 场景构建器
3871
4033
  */
3872
- withSystems(...systems: any[]): SceneBuilder;
4034
+ withSystems(...systems: EntitySystem[]): SceneBuilder;
3873
4035
  /**
3874
4036
  * 构建并返回场景
3875
4037
  * @returns 构建的场景
@@ -3881,7 +4043,7 @@ declare class SceneBuilder {
3881
4043
  */
3882
4044
  declare class ComponentBuilder<T extends Component> {
3883
4045
  private component;
3884
- constructor(componentClass: new (...args: any[]) => T, ...args: any[]);
4046
+ constructor(componentClass: new (...args: unknown[]) => T, ...args: unknown[]);
3885
4047
  /**
3886
4048
  * 设置组件属性
3887
4049
  * @param property 属性名
@@ -3934,7 +4096,7 @@ declare class ECSFluentAPI {
3934
4096
  * @param args 构造参数
3935
4097
  * @returns 组件构建器
3936
4098
  */
3937
- createComponent<T extends Component>(componentClass: new (...args: any[]) => T, ...args: any[]): ComponentBuilder<T>;
4099
+ createComponent<T extends Component>(componentClass: new (...args: unknown[]) => T, ...args: unknown[]): ComponentBuilder<T>;
3938
4100
  /**
3939
4101
  * 创建查询构建器
3940
4102
  * @returns 查询构建器
@@ -4010,7 +4172,7 @@ declare class ECSFluentAPI {
4010
4172
  entityCount: number;
4011
4173
  systemCount: number;
4012
4174
  componentStats: Map<string, any>;
4013
- queryStats: any;
4175
+ queryStats: unknown;
4014
4176
  eventStats: Map<string, any>;
4015
4177
  };
4016
4178
  }
@@ -4510,6 +4672,10 @@ declare class Core {
4510
4672
  * Core配置
4511
4673
  */
4512
4674
  private _config;
4675
+ /**
4676
+ * 兼容性信息
4677
+ */
4678
+ private _environmentInfo;
4513
4679
  /**
4514
4680
  * 创建核心实例
4515
4681
  *
@@ -4592,7 +4758,7 @@ declare class Core {
4592
4758
  * @param type - 管理器类型构造函数
4593
4759
  * @returns 管理器实例,如果未找到则返回null
4594
4760
  */
4595
- static getGlobalManager<T extends GlobalManager>(type: new (...args: any[]) => T): T | null;
4761
+ static getGlobalManager<T extends GlobalManager>(type: new (...args: unknown[]) => T): T | null;
4596
4762
  /**
4597
4763
  * 调度定时器
4598
4764
  *
@@ -4604,7 +4770,7 @@ declare class Core {
4604
4770
  * @param onTime - 定时器触发时的回调函数
4605
4771
  * @returns 创建的定时器实例
4606
4772
  */
4607
- static schedule(timeInSeconds: number, repeats: boolean | undefined, context: any | undefined, onTime: (timer: ITimer) => void): Timer;
4773
+ static schedule<TContext = unknown>(timeInSeconds: number, repeats: boolean | undefined, context: TContext | undefined, onTime: (timer: ITimer<TContext>) => void): Timer<TContext>;
4608
4774
  /**
4609
4775
  * 获取ECS流式API
4610
4776
  *
@@ -4633,6 +4799,18 @@ declare class Core {
4633
4799
  * @returns 调试状态
4634
4800
  */
4635
4801
  static get isDebugEnabled(): boolean;
4802
+ /**
4803
+ * 获取环境兼容性信息
4804
+ *
4805
+ * @returns 环境兼容性信息
4806
+ */
4807
+ static getEnvironmentInfo(): EnvironmentInfo | null;
4808
+ /**
4809
+ * 检查BigInt是否支持
4810
+ *
4811
+ * @returns 是否支持BigInt
4812
+ */
4813
+ static get supportsBigInt(): boolean;
4636
4814
  /**
4637
4815
  * 场景切换回调
4638
4816
  *
@@ -4645,6 +4823,12 @@ declare class Core {
4645
4823
  * 执行核心系统的初始化逻辑。
4646
4824
  */
4647
4825
  protected initialize(): void;
4826
+ /**
4827
+ * 记录兼容性信息
4828
+ *
4829
+ * 在控制台输出当前环境的兼容性信息和建议。
4830
+ */
4831
+ private logCompatibilityInfo;
4648
4832
  /**
4649
4833
  * 内部更新方法
4650
4834
  *
@@ -4656,17 +4840,17 @@ declare class Core {
4656
4840
  /**
4657
4841
  * 用于包装事件的一个小类
4658
4842
  */
4659
- declare class FuncPack {
4843
+ declare class FuncPack<TContext = unknown> {
4660
4844
  /** 函数 */
4661
4845
  func: Function;
4662
4846
  /** 上下文 */
4663
- context: any;
4664
- constructor(func: Function, context: any);
4847
+ context: TContext;
4848
+ constructor(func: Function, context: TContext);
4665
4849
  }
4666
4850
  /**
4667
4851
  * 用于事件管理
4668
4852
  */
4669
- declare class Emitter<T> {
4853
+ declare class Emitter<T, TContext = unknown> {
4670
4854
  private _messageTable;
4671
4855
  constructor();
4672
4856
  /**
@@ -4675,7 +4859,7 @@ declare class Emitter<T> {
4675
4859
  * @param handler 监听函数
4676
4860
  * @param context 监听上下文
4677
4861
  */
4678
- addObserver(eventType: T, handler: Function, context: any): void;
4862
+ addObserver(eventType: T, handler: Function, context: TContext): void;
4679
4863
  /**
4680
4864
  * 移除监听项
4681
4865
  * @param eventType 事件类型
@@ -4687,7 +4871,7 @@ declare class Emitter<T> {
4687
4871
  * @param eventType 事件类型
4688
4872
  * @param data 事件数据
4689
4873
  */
4690
- emit(eventType: T, ...data: any[]): void;
4874
+ emit<TData = unknown>(eventType: T, ...data: TData[]): void;
4691
4875
  /**
4692
4876
  * 判断是否存在该类型的观察者
4693
4877
  * @param eventType 事件类型
@@ -4833,11 +5017,7 @@ declare class EventTypeValidator {
4833
5017
  * 子类需要实现processSystem方法,用于实现具体的处理逻辑
4834
5018
  */
4835
5019
  declare abstract class ProcessingSystem extends EntitySystem {
4836
- /**
4837
- * 当实体发生变化时,不进行任何操作
4838
- * @param entity 发生变化的实体
4839
- */
4840
- onChanged(entity: Entity): void;
5020
+ constructor(matcher?: Matcher);
4841
5021
  /**
4842
5022
  * 处理实体,每帧调用processSystem方法进行处理
4843
5023
  * @param entities 实体数组,未被使用
@@ -4855,11 +5035,7 @@ declare abstract class ProcessingSystem extends EntitySystem {
4855
5035
  * 被动的实体系统不会对实体进行任何修改,只会被动地接收实体的变化事件
4856
5036
  */
4857
5037
  declare abstract class PassiveSystem extends EntitySystem {
4858
- /**
4859
- * 当实体发生变化时,不进行任何操作
4860
- * @param entity 发生变化的实体
4861
- */
4862
- onChanged(entity: Entity): void;
5038
+ constructor(matcher?: Matcher);
4863
5039
  /**
4864
5040
  * 不进行任何处理
4865
5041
  * @param entities 实体数组,未被使用
@@ -4881,16 +5057,16 @@ declare abstract class IntervalSystem extends EntitySystem {
4881
5057
  private intervalRemainder;
4882
5058
  /**
4883
5059
  * 构造函数,初始化时间间隔
4884
- * @param matcher 实体匹配器
4885
5060
  * @param interval 时间间隔
5061
+ * @param matcher 实体匹配器
4886
5062
  */
4887
- constructor(matcher: Matcher, interval: number);
5063
+ constructor(interval: number, matcher?: Matcher);
4888
5064
  /**
4889
5065
  * 判断是否需要进行处理
4890
5066
  * 如果需要进行处理,则更新累积增量和时间间隔余数,返回true
4891
5067
  * 否则返回false
4892
5068
  */
4893
- protected checkProcessing(): boolean;
5069
+ protected onCheckProcessing(): boolean;
4894
5070
  /**
4895
5071
  * 获取本系统上次处理后的实际delta值
4896
5072
  * 实际delta值等于时间间隔加上时间间隔余数
@@ -4898,6 +5074,168 @@ declare abstract class IntervalSystem extends EntitySystem {
4898
5074
  protected getIntervalDelta(): number;
4899
5075
  }
4900
5076
 
5077
+ /**
5078
+ * 高性能位操作类
5079
+ *
5080
+ * 基于BigInt实现,支持任意数量的位操作。
5081
+ * 自动适配运行环境,在不支持BigInt的环境中使用兼容实现。
5082
+ *
5083
+ * @example
5084
+ * ```typescript
5085
+ * const bits = new Bits();
5086
+ * bits.set(0);
5087
+ * bits.set(5);
5088
+ * console.log(bits.get(0)); // true
5089
+ * console.log(bits.get(1)); // false
5090
+ * ```
5091
+ */
5092
+ declare class Bits {
5093
+ private _value;
5094
+ /**
5095
+ * 构造函数
5096
+ * @param initialValue 初始值,可以是IBigIntLike或数值
5097
+ */
5098
+ constructor(initialValue?: IBigIntLike | number | string);
5099
+ /**
5100
+ * 设置指定位置的位为1
5101
+ * @param index 位索引(从0开始)
5102
+ * @throws {Error} 当索引为负数时抛出错误
5103
+ */
5104
+ set(index: number): void;
5105
+ /**
5106
+ * 清除指定位置的位(设为0)
5107
+ * @param index 位索引(从0开始)
5108
+ * @throws {Error} 当索引为负数时抛出错误
5109
+ */
5110
+ clear(index: number): void;
5111
+ /**
5112
+ * 获取指定位置的位值
5113
+ * @param index 位索引(从0开始)
5114
+ * @returns 位值(true表示1,false表示0)
5115
+ */
5116
+ get(index: number): boolean;
5117
+ /**
5118
+ * 检查是否包含所有指定的位
5119
+ * @param other 另一个Bits对象
5120
+ * @returns 是否包含所有指定的位
5121
+ */
5122
+ containsAll(other: Bits): boolean;
5123
+ /**
5124
+ * 检查是否包含任意一个指定的位
5125
+ * @param other 另一个Bits对象
5126
+ * @returns 是否包含任意一个指定的位
5127
+ */
5128
+ intersects(other: Bits): boolean;
5129
+ /**
5130
+ * 检查是否不包含任何指定的位
5131
+ * @param other 另一个Bits对象
5132
+ * @returns 是否不包含任何指定的位
5133
+ */
5134
+ excludes(other: Bits): boolean;
5135
+ /**
5136
+ * 清空所有位
5137
+ */
5138
+ clearAll(): void;
5139
+ /**
5140
+ * 检查是否为空(没有设置任何位)
5141
+ * @returns 是否为空
5142
+ */
5143
+ isEmpty(): boolean;
5144
+ /**
5145
+ * 获取设置的位数量
5146
+ * @returns 设置为1的位数量
5147
+ */
5148
+ cardinality(): number;
5149
+ /**
5150
+ * 位运算:与
5151
+ * @param other 另一个Bits对象
5152
+ * @returns 新的Bits对象,包含与运算结果
5153
+ */
5154
+ and(other: Bits): Bits;
5155
+ /**
5156
+ * 位运算:或
5157
+ * @param other 另一个Bits对象
5158
+ * @returns 新的Bits对象,包含或运算结果
5159
+ */
5160
+ or(other: Bits): Bits;
5161
+ /**
5162
+ * 位运算:异或
5163
+ * @param other 另一个Bits对象
5164
+ * @returns 新的Bits对象,包含异或运算结果
5165
+ */
5166
+ xor(other: Bits): Bits;
5167
+ /**
5168
+ * 位运算:非
5169
+ * @param maxBits 最大位数限制,默认64位
5170
+ * @returns 新的Bits对象,包含非运算结果
5171
+ */
5172
+ not(maxBits?: number): Bits;
5173
+ /**
5174
+ * 复制另一个Bits对象
5175
+ * @param other 要复制的Bits对象
5176
+ */
5177
+ copyFrom(other: Bits): void;
5178
+ /**
5179
+ * 创建当前Bits的副本
5180
+ * @returns 新的Bits对象副本
5181
+ */
5182
+ clone(): Bits;
5183
+ /**
5184
+ * 获取原始值
5185
+ * @returns 原始的IBigIntLike值
5186
+ */
5187
+ getValue(): IBigIntLike;
5188
+ /**
5189
+ * 设置原始值
5190
+ * @param value 新的值,可以是IBigIntLike或数值
5191
+ */
5192
+ setValue(value: IBigIntLike | number | string): void;
5193
+ /**
5194
+ * 获取调试信息
5195
+ * @returns 返回显示设置位索引的字符串
5196
+ */
5197
+ toString(): string;
5198
+ /**
5199
+ * 获取二进制表示
5200
+ * @param maxBits 最大位数,默认64位
5201
+ * @returns 二进制字符串表示
5202
+ */
5203
+ toBinaryString(maxBits?: number): string;
5204
+ /**
5205
+ * 获取十六进制表示
5206
+ * @returns 十六进制字符串表示
5207
+ */
5208
+ toHexString(): string;
5209
+ /**
5210
+ * 从二进制字符串创建Bits
5211
+ * @param binaryString 二进制字符串
5212
+ * @returns 新的Bits对象
5213
+ */
5214
+ static fromBinaryString(binaryString: string): Bits;
5215
+ /**
5216
+ * 从十六进制字符串创建Bits
5217
+ * @param hexString 十六进制字符串
5218
+ * @returns 新的Bits对象
5219
+ */
5220
+ static fromHexString(hexString: string): Bits;
5221
+ /**
5222
+ * 比较两个Bits对象是否相等
5223
+ * @param other 另一个Bits对象
5224
+ * @returns 是否相等
5225
+ */
5226
+ equals(other: Bits): boolean;
5227
+ /**
5228
+ * 获取最高位的索引
5229
+ * @returns 最高位的索引,如果为空则返回-1
5230
+ */
5231
+ getHighestBitIndex(): number;
5232
+ /**
5233
+ * 获取最低位的索引
5234
+ * @returns 最低位的索引,如果为空则返回-1
5235
+ */
5236
+ getLowestBitIndex(): number;
5237
+ }
5238
+
4901
5239
  /**
4902
5240
  * 组件类型管理器
4903
5241
  * 负责管理组件类型的注册和ID分配
@@ -4917,7 +5255,7 @@ declare class ComponentTypeManager {
4917
5255
  * @param componentType 组件类型构造函数
4918
5256
  * @returns 组件类型ID
4919
5257
  */
4920
- getTypeId<T extends Component>(componentType: new (...args: any[]) => T): number;
5258
+ getTypeId<T extends Component>(componentType: new (...args: unknown[]) => T): number;
4921
5259
  /**
4922
5260
  * 获取组件类型名称
4923
5261
  * @param typeId 组件类型ID
@@ -4929,7 +5267,7 @@ declare class ComponentTypeManager {
4929
5267
  * @param componentTypes 组件类型构造函数数组
4930
5268
  * @returns Bits对象
4931
5269
  */
4932
- createBits(...componentTypes: (new (...args: any[]) => Component)[]): Bits;
5270
+ createBits(...componentTypes: (new (...args: unknown[]) => Component)[]): Bits;
4933
5271
  /**
4934
5272
  * 获取实体的组件位掩码
4935
5273
  * @param components 组件数组
@@ -5170,6 +5508,22 @@ declare class EntityManager {
5170
5508
  * ```
5171
5509
  */
5172
5510
  createEntity(name?: string): Entity;
5511
+ /**
5512
+ * 批量创建实体
5513
+ *
5514
+ * 为了优化大量实体创建的性能,批量处理索引更新和事件发射。
5515
+ * 适用于需要创建大量实体的场景,如子弹、粒子等。
5516
+ *
5517
+ * @param count 要创建的实体数量
5518
+ * @param namePrefix 实体名称前缀,默认为 Entity
5519
+ * @param skipEvents 是否跳过事件发射以提升性能,默认为 false
5520
+ * @returns 创建的实体数组
5521
+ *
5522
+ * @example
5523
+ * const bullets = entityManager.createEntitiesBatch(100, "Bullet", true);
5524
+ * const particles = entityManager.createEntitiesBatch(500, "Particle");
5525
+ */
5526
+ createEntitiesBatch(count: number, namePrefix?: string, skipEvents?: boolean): Entity[];
5173
5527
  /**
5174
5528
  * 销毁实体
5175
5529
  *
@@ -5600,6 +5954,8 @@ declare class IndexUpdateBatcher {
5600
5954
 
5601
5955
  /**
5602
5956
  * 位掩码优化器,用于预计算和缓存常用的组件掩码
5957
+ *
5958
+ * 使用BigInt兼容层确保在所有平台上的正常运行。
5603
5959
  */
5604
5960
  declare class BitMaskOptimizer {
5605
5961
  private static instance;
@@ -5618,32 +5974,51 @@ declare class BitMaskOptimizer {
5618
5974
  getComponentTypeId(componentName: string): number | undefined;
5619
5975
  /**
5620
5976
  * 创建单个组件的掩码
5977
+ * @param componentName 组件名称
5978
+ * @returns 组件掩码
5621
5979
  */
5622
- createSingleComponentMask(componentName: string): bigint;
5980
+ createSingleComponentMask(componentName: string): IBigIntLike;
5623
5981
  /**
5624
5982
  * 创建多个组件的组合掩码
5983
+ * @param componentNames 组件名称数组
5984
+ * @returns 组合掩码
5625
5985
  */
5626
- createCombinedMask(componentNames: string[]): bigint;
5986
+ createCombinedMask(componentNames: string[]): IBigIntLike;
5627
5987
  /**
5628
5988
  * 检查掩码是否包含指定组件
5989
+ * @param mask 要检查的掩码
5990
+ * @param componentName 组件名称
5991
+ * @returns 是否包含指定组件
5629
5992
  */
5630
- maskContainsComponent(mask: bigint, componentName: string): boolean;
5993
+ maskContainsComponent(mask: IBigIntLike, componentName: string): boolean;
5631
5994
  /**
5632
5995
  * 检查掩码是否包含所有指定组件
5996
+ * @param mask 要检查的掩码
5997
+ * @param componentNames 组件名称数组
5998
+ * @returns 是否包含所有指定组件
5633
5999
  */
5634
- maskContainsAllComponents(mask: bigint, componentNames: string[]): boolean;
6000
+ maskContainsAllComponents(mask: IBigIntLike, componentNames: string[]): boolean;
5635
6001
  /**
5636
6002
  * 检查掩码是否包含任一指定组件
6003
+ * @param mask 要检查的掩码
6004
+ * @param componentNames 组件名称数组
6005
+ * @returns 是否包含任一指定组件
5637
6006
  */
5638
- maskContainsAnyComponent(mask: bigint, componentNames: string[]): boolean;
6007
+ maskContainsAnyComponent(mask: IBigIntLike, componentNames: string[]): boolean;
5639
6008
  /**
5640
6009
  * 添加组件到掩码
6010
+ * @param mask 原始掩码
6011
+ * @param componentName 要添加的组件名称
6012
+ * @returns 新的掩码
5641
6013
  */
5642
- addComponentToMask(mask: bigint, componentName: string): bigint;
6014
+ addComponentToMask(mask: IBigIntLike, componentName: string): IBigIntLike;
5643
6015
  /**
5644
6016
  * 从掩码中移除组件
6017
+ * @param mask 原始掩码
6018
+ * @param componentName 要移除的组件名称
6019
+ * @returns 新的掩码
5645
6020
  */
5646
- removeComponentFromMask(mask: bigint, componentName: string): bigint;
6021
+ removeComponentFromMask(mask: IBigIntLike, componentName: string): IBigIntLike;
5647
6022
  /**
5648
6023
  * 预计算常用掩码组合
5649
6024
  */
@@ -5666,11 +6041,11 @@ declare class BitMaskOptimizer {
5666
6041
  /**
5667
6042
  * 将掩码转换为组件名称数组
5668
6043
  */
5669
- maskToComponentNames(mask: bigint): string[];
6044
+ maskToComponentNames(mask: IBigIntLike): string[];
5670
6045
  /**
5671
6046
  * 获取掩码中组件的数量
5672
6047
  */
5673
- getComponentCount(mask: bigint): number;
6048
+ getComponentCount(mask: IBigIntLike): number;
5674
6049
  }
5675
6050
 
5676
6051
  /**
@@ -5783,7 +6158,7 @@ declare class NumberExtension {
5783
6158
  * @param value 要转换的值
5784
6159
  * @returns 转换后的数字,如果值为undefined则返回0
5785
6160
  */
5786
- static toNumber(value: any): number;
6161
+ static toNumber(value: unknown): number;
5787
6162
  }
5788
6163
 
5789
6164
  /**
@@ -6183,5 +6558,5 @@ declare class SnapshotExtension {
6183
6558
  private static deepCompare;
6184
6559
  }
6185
6560
 
6186
- export { ArchetypeSystem, AsyncEventHandler$1 as AsyncEventHandler, BitMaskOptimizer, BitmapComponentIndex, Bits, Component, ComponentDataCollector, ComponentIndexManager, ComponentPool, ComponentPoolManager, ComponentStorage, ComponentTypeManager, Core, DebugManager, DirtyFlag, DirtyTrackingSystem, ECSEventType, ECSFluentAPI, EVENT_TYPES, Emitter, Entity, EntityDataCollector, EntityList, EntityManager, EntityProcessorList, EntityQueryBuilder, EntitySystem, EventBus, EventHandler$1 as EventHandler, EventPriority, EventTypeValidator, FuncPack, GlobalEventBus, GlobalManager, HashComponentIndex, IdentifierPool, IndexType, IndexUpdateBatcher, IntervalSystem, Matcher, NumberExtension, PassiveSystem, PerformanceDataCollector, PerformanceMonitor, PerformanceWarningType, Pool, PoolManager, ProcessingSystem, QuerySystem, Scene, SceneDataCollector, Serializable, SnapshotConfigDecorator, SnapshotExtension, SnapshotManager, SystemDataCollector, TieredObjectPool, Time, Timer, TimerManager, TypeSafeEventSystem, TypeUtils, WebSocketManager, createECSAPI };
6187
- export type { Archetype, ArchetypeQueryResult, ComponentSnapshot, ComponentType$1 as ComponentType, DirtyData, DirtyListener, EntitySnapshot, EventListenerConfig, EventStats, IComponent, IComponentDebugData, IComponentEventData, ICoreConfig, IECSDebugConfig, IECSDebugData, IEntityDebugData, IEntityEventData, IEventBus, IEventData, IEventListenerConfig, IEventStats, IPerformanceDebugData, IPerformanceEventData, IPoolable, ISceneDebugData, ISceneEventData, ISnapshotExtension, ISnapshotable, ISystemBase, ISystemDebugData, ISystemEventData, ITimer, PerformanceData, PerformanceStats, PerformanceThresholds, PerformanceWarning, PoolStats, SceneSnapshot, SnapshotConfig };
6561
+ export { ArchetypeSystem, AsyncEventHandler, BitMaskOptimizer, BitmapComponentIndex, Bits, Component, ComponentDataCollector, ComponentIndexManager, ComponentPool, ComponentPoolManager, ComponentStorage, ComponentTypeManager, Core, DebugManager, DirtyFlag, DirtyTrackingSystem, ECSEventType, ECSFluentAPI, EVENT_TYPES, Emitter, Entity, EntityDataCollector, EntityList, EntityManager, EntityProcessorList, EntityQueryBuilder, EntitySystem, EventBus, EventHandler, EventPriority, EventTypeValidator, FuncPack, GlobalEventBus, GlobalManager, HashComponentIndex, IdentifierPool, IndexType, IndexUpdateBatcher, IntervalSystem, Matcher, NumberExtension, PassiveSystem, PerformanceDataCollector, PerformanceMonitor, PerformanceWarningType, Pool, PoolManager, ProcessingSystem, QuerySystem, Scene, SceneDataCollector, Serializable, SnapshotConfigDecorator, SnapshotExtension, SnapshotManager, SystemDataCollector, TieredObjectPool, Time, Timer, TimerManager, TypeSafeEventSystem, TypeUtils, WebSocketManager, createECSAPI };
6562
+ export type { Archetype, ArchetypeQueryResult, ComponentSnapshot, ComponentType$1 as ComponentType, DirtyData, DirtyListener, EntitySnapshot, EventListenerConfig, EventStats, IComponent, IComponentDebugData, IComponentEventData, ICoreConfig, IECSDebugConfig, IECSDebugData, IEntityDebugData, IEntityEventData, IEntityHierarchyNode, IEventBus, IEventData, IEventListenerConfig, IEventStats, IPerformanceDebugData, IPerformanceEventData, IPoolable, ISceneDebugData, ISceneEventData, ISnapshotExtension, ISnapshotable, ISystemBase, ISystemDebugData, ISystemEventData, ITimer, PerformanceData, PerformanceStats, PerformanceThresholds, PerformanceWarning, PoolStats, SceneSnapshot, SnapshotConfig };