@esengine/ecs-framework 2.1.26 → 2.1.27

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.26
2
+ * @esengine/ecs-framework v2.1.27
3
3
  * TypeScript definitions
4
4
  */
5
5
  /**
@@ -1493,37 +1493,6 @@ declare class EntityComparer {
1493
1493
  */
1494
1494
  compare(self: Entity, other: Entity): number;
1495
1495
  }
1496
- /**
1497
- * 组件缓存配置
1498
- */
1499
- interface ComponentCacheConfig {
1500
- maxSize: number;
1501
- ttl: number;
1502
- enableLRU: boolean;
1503
- }
1504
- /**
1505
- * 高性能组件缓存
1506
- */
1507
- declare class ComponentCache {
1508
- private cache;
1509
- private accessOrder;
1510
- private config;
1511
- constructor(config?: ComponentCacheConfig);
1512
- get<T extends Component>(type: ComponentType<T>): T | null;
1513
- set<T extends Component>(type: ComponentType<T>, component: T): void;
1514
- delete(type: ComponentType): boolean;
1515
- clear(): void;
1516
- has(type: ComponentType): boolean;
1517
- private evictLeastRecentlyUsed;
1518
- private updateAccessOrder;
1519
- private removeFromAccessOrder;
1520
- getStats(): {
1521
- size: number;
1522
- maxSize: number;
1523
- hitRate: number;
1524
- averageAccessCount: number;
1525
- };
1526
- }
1527
1496
  /**
1528
1497
  * 游戏实体类
1529
1498
  *
@@ -1644,18 +1613,6 @@ declare class Entity {
1644
1613
  * 用于快速定位组件在数组中的位置。
1645
1614
  */
1646
1615
  private _componentTypeToIndex;
1647
- /**
1648
- * 组件缓存
1649
- *
1650
- * 高性能组件访问缓存。
1651
- */
1652
- private _componentCache;
1653
- /**
1654
- * 组件访问统计
1655
- *
1656
- * 记录组件访问的性能统计信息。
1657
- */
1658
- private _componentAccessStats;
1659
1616
  /**
1660
1617
  * 构造函数
1661
1618
  *
@@ -1781,24 +1738,6 @@ declare class Entity {
1781
1738
  * @returns 组件实例或null
1782
1739
  */
1783
1740
  getComponent<T extends Component>(type: ComponentType<T>): T | null;
1784
- /**
1785
- * 更新组件访问统计
1786
- *
1787
- * @param type - 组件类型
1788
- */
1789
- private updateComponentAccessStats;
1790
- /**
1791
- * 记录缓存命中
1792
- *
1793
- * @param type - 组件类型
1794
- */
1795
- private recordCacheHit;
1796
- /**
1797
- * 记录缓存未命中
1798
- *
1799
- * @param type - 组件类型
1800
- */
1801
- private recordCacheMiss;
1802
1741
  /**
1803
1742
  * 重建组件索引映射
1804
1743
  */
@@ -1849,35 +1788,6 @@ declare class Entity {
1849
1788
  * @returns 被移除的组件数组
1850
1789
  */
1851
1790
  removeComponentsByTypes<T extends Component>(componentTypes: ComponentType<T>[]): (T | null)[];
1852
- /**
1853
- * 获取组件缓存统计信息
1854
- *
1855
- * @returns 缓存统计信息
1856
- */
1857
- getComponentCacheStats(): {
1858
- cacheStats: ReturnType<ComponentCache['getStats']>;
1859
- accessStats: Map<string, {
1860
- accessCount: number;
1861
- lastAccessed: number;
1862
- cacheHits: number;
1863
- cacheMisses: number;
1864
- hitRate: number;
1865
- }>;
1866
- indexMappingSize: number;
1867
- totalComponents: number;
1868
- };
1869
- /**
1870
- * 预热组件缓存
1871
- *
1872
- * 将所有组件添加到缓存中,提升后续访问性能
1873
- */
1874
- warmUpComponentCache(): void;
1875
- /**
1876
- * 清理组件缓存
1877
- *
1878
- * 清除过期的缓存项,释放内存
1879
- */
1880
- cleanupComponentCache(): void;
1881
1791
  /**
1882
1792
  * 获取所有指定类型的组件
1883
1793
  *
@@ -2000,20 +1910,6 @@ declare class Entity {
2000
1910
  childCount: number;
2001
1911
  childIds: number[];
2002
1912
  depth: number;
2003
- componentCache: {
2004
- size: number;
2005
- maxSize: number;
2006
- hitRate: number;
2007
- averageAccessCount: number;
2008
- };
2009
- componentAccessStats: Array<{
2010
- componentType: string;
2011
- accessCount: number;
2012
- cacheHits: number;
2013
- cacheMisses: number;
2014
- hitRate: number;
2015
- lastAccessed: string;
2016
- }>;
2017
1913
  indexMappingSize: number;
2018
1914
  };
2019
1915
  }
@@ -2348,8 +2244,8 @@ declare abstract class EntitySystem implements ISystemBase {
2348
2244
  /**
2349
2245
  * 这个系统所属的场景
2350
2246
  */
2351
- get scene(): Scene;
2352
- set scene(value: Scene);
2247
+ get scene(): Scene | null;
2248
+ set scene(value: Scene | null);
2353
2249
  private _matcher;
2354
2250
  /**
2355
2251
  * 获取实体匹配器
@@ -2529,21 +2425,206 @@ declare class EntityProcessorList {
2529
2425
  }
2530
2426
 
2531
2427
  /**
2532
- * ID池管理器
2533
- * 用于管理实体ID的分配和回收
2428
+ * 世代式ID池管理器
2429
+ *
2430
+ * 用于管理实体ID的分配和回收,支持世代版本控制以防止悬空引用问题。
2431
+ * 世代式ID由索引和版本组成,当ID被回收时版本会递增,确保旧引用失效。
2432
+ *
2433
+ * 支持动态扩展,理论上可以支持到65535个索引(16位),每个索引65535个版本(16位)。
2434
+ * 总计可以处理超过42亿个独特的ID组合,完全满足ECS大规模实体需求。
2435
+ *
2436
+ * @example
2437
+ * ```typescript
2438
+ * const pool = new IdentifierPool();
2439
+ *
2440
+ * // 分配ID
2441
+ * const id = pool.checkOut(); // 例如: 65536 (版本1,索引0)
2442
+ *
2443
+ * // 回收ID
2444
+ * pool.checkIn(id);
2445
+ *
2446
+ * // 验证ID是否有效
2447
+ * const isValid = pool.isValid(id); // false,因为版本已递增
2448
+ * ```
2534
2449
  */
2535
2450
  declare class IdentifierPool {
2536
- private _nextAvailableId;
2537
- private _ids;
2451
+ /**
2452
+ * 下一个可用的索引
2453
+ */
2454
+ private _nextAvailableIndex;
2455
+ /**
2456
+ * 空闲的索引列表
2457
+ */
2458
+ private _freeIndices;
2459
+ /**
2460
+ * 每个索引对应的世代版本
2461
+ * 动态扩展的Map,按需分配内存
2462
+ */
2463
+ private _generations;
2464
+ /**
2465
+ * 延迟回收队列
2466
+ * 防止在同一帧内立即重用ID,避免时序问题
2467
+ */
2468
+ private _pendingRecycle;
2469
+ /**
2470
+ * 延迟回收时间(毫秒)
2471
+ */
2472
+ private _recycleDelay;
2473
+ /**
2474
+ * 最大索引限制(16位)
2475
+ * 这是框架设计选择:16位索引 + 16位版本 = 32位ID,确保高效位操作
2476
+ * 不是硬件限制,而是性能和内存效率的权衡
2477
+ */
2478
+ private static readonly MAX_INDEX;
2479
+ /**
2480
+ * 最大世代限制(16位)
2481
+ */
2482
+ private static readonly MAX_GENERATION;
2483
+ /**
2484
+ * 内存扩展块大小
2485
+ * 当需要更多内存时,一次性预分配的索引数量
2486
+ */
2487
+ private _expansionBlockSize;
2488
+ /**
2489
+ * 统计信息
2490
+ */
2491
+ private _stats;
2492
+ /**
2493
+ * 构造函数
2494
+ *
2495
+ * @param recycleDelay 延迟回收时间(毫秒),默认为100ms
2496
+ * @param expansionBlockSize 内存扩展块大小,默认为1024
2497
+ */
2498
+ constructor(recycleDelay?: number, expansionBlockSize?: number);
2538
2499
  /**
2539
2500
  * 获取一个可用的ID
2501
+ *
2502
+ * 返回一个32位ID,高16位为世代版本,低16位为索引。
2503
+ *
2504
+ * @returns 新分配的实体ID
2505
+ * @throws {Error} 当达到索引限制时抛出错误
2540
2506
  */
2541
2507
  checkOut(): number;
2542
2508
  /**
2543
2509
  * 回收一个ID
2544
- * @param id 要回收的ID
2510
+ *
2511
+ * 验证ID的有效性后,将其加入延迟回收队列。
2512
+ * ID不会立即可重用,而是在延迟时间后才真正回收。
2513
+ *
2514
+ * @param id 要回收的实体ID
2515
+ * @returns 是否成功回收(ID是否有效且未被重复回收)
2516
+ */
2517
+ checkIn(id: number): boolean;
2518
+ /**
2519
+ * 验证ID是否有效
2520
+ *
2521
+ * 检查ID的索引和世代版本是否匹配当前状态。
2522
+ *
2523
+ * @param id 要验证的实体ID
2524
+ * @returns ID是否有效
2525
+ */
2526
+ isValid(id: number): boolean;
2527
+ /**
2528
+ * 获取统计信息
2529
+ *
2530
+ * @returns 池的当前状态统计
2545
2531
  */
2546
- checkIn(id: number): void;
2532
+ getStats(): {
2533
+ /** 已分配的总索引数 */
2534
+ totalAllocated: number;
2535
+ /** 总计回收次数 */
2536
+ totalRecycled: number;
2537
+ /** 当前活跃实体数 */
2538
+ currentActive: number;
2539
+ /** 当前空闲的索引数 */
2540
+ currentlyFree: number;
2541
+ /** 等待回收的ID数 */
2542
+ pendingRecycle: number;
2543
+ /** 理论最大实体数(设计限制) */
2544
+ maxPossibleEntities: number;
2545
+ /** 当前使用的最大索引 */
2546
+ maxUsedIndex: number;
2547
+ /** 内存使用(字节) */
2548
+ memoryUsage: number;
2549
+ /** 内存扩展次数 */
2550
+ memoryExpansions: number;
2551
+ /** 平均世代版本 */
2552
+ averageGeneration: number;
2553
+ /** 世代存储大小 */
2554
+ generationStorageSize: number;
2555
+ };
2556
+ /**
2557
+ * 强制执行延迟回收处理
2558
+ *
2559
+ * 在某些情况下可能需要立即处理延迟回收队列,
2560
+ * 比如内存压力大或者需要精确的统计信息时。
2561
+ */
2562
+ forceProcessDelayedRecycle(): void;
2563
+ /**
2564
+ * 清理过期的延迟回收项
2565
+ *
2566
+ * 将超过延迟时间的回收项真正回收到空闲列表中。
2567
+ *
2568
+ * @param forceAll 是否强制处理所有延迟回收项
2569
+ * @private
2570
+ */
2571
+ private _processDelayedRecycle;
2572
+ /**
2573
+ * 预分配世代信息
2574
+ *
2575
+ * @param startIndex 起始索引
2576
+ * @param count 分配数量
2577
+ * @private
2578
+ */
2579
+ private _preAllocateGenerations;
2580
+ /**
2581
+ * 确保指定索引的世代信息存在
2582
+ *
2583
+ * @param index 索引
2584
+ * @private
2585
+ */
2586
+ private _ensureGenerationCapacity;
2587
+ /**
2588
+ * 计算内存使用量
2589
+ *
2590
+ * @returns 内存使用字节数
2591
+ * @private
2592
+ */
2593
+ private _calculateMemoryUsage;
2594
+ /**
2595
+ * 打包索引和世代为32位ID
2596
+ *
2597
+ * @param index 索引(16位)
2598
+ * @param generation 世代版本(16位)
2599
+ * @returns 打包后的32位ID
2600
+ * @private
2601
+ */
2602
+ private _packId;
2603
+ /**
2604
+ * 从ID中解包索引
2605
+ *
2606
+ * @param id 32位ID
2607
+ * @returns 索引部分(16位)
2608
+ * @private
2609
+ */
2610
+ private _unpackIndex;
2611
+ /**
2612
+ * 从ID中解包世代版本
2613
+ *
2614
+ * @param id 32位ID
2615
+ * @returns 世代版本部分(16位)
2616
+ * @private
2617
+ */
2618
+ private _unpackGeneration;
2619
+ /**
2620
+ * 内部ID有效性检查
2621
+ *
2622
+ * @param index 索引
2623
+ * @param generation 世代版本
2624
+ * @returns 是否有效
2625
+ * @private
2626
+ */
2627
+ private _isValidId;
2547
2628
  }
2548
2629
 
2549
2630
  /**
@@ -5654,6 +5735,10 @@ declare class ComponentPoolManager {
5654
5735
  * 清空所有池
5655
5736
  */
5656
5737
  clearAll(): void;
5738
+ /**
5739
+ * 重置管理器,移除所有注册的池
5740
+ */
5741
+ reset(): void;
5657
5742
  /**
5658
5743
  * 获取池统计信息
5659
5744
  */