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