* 按单个组件类型查询实体 * * 返回包含指定组件类型的所有实体。 * 这是最基础的查询方法,具有最高的查询性能。 * * @param componentType 要查询的组件类型 * @returns 查询结果,包含匹配的实体和性能信息 * * @example * ```typescript * // 查询所有具有位置组件的实体 * const entitiesWithPosition = querySystem.queryByComponent(PositionCompone
(componentType: ComponentType<T>)
| 620 | * ``` |
| 621 | */ |
| 622 | public queryByComponent<T extends Component>(componentType: ComponentType<T>): QueryResult { |
| 623 | const startTime = performance.now(); |
| 624 | this._queryStats.totalQueries++; |
| 625 | |
| 626 | const cacheKey = this.generateCacheKey('component', [componentType]); |
| 627 | |
| 628 | // 检查缓存 |
| 629 | const cached = this.getFromCache(cacheKey); |
| 630 | if (cached) { |
| 631 | this._queryStats.cacheHits++; |
| 632 | return { |
| 633 | entities: cached, |
| 634 | count: cached.length, |
| 635 | executionTime: performance.now() - startTime, |
| 636 | fromCache: true |
| 637 | }; |
| 638 | } |
| 639 | |
| 640 | this._queryStats.indexHits++; |
| 641 | const entities = this._archetypeSystem.getEntitiesByComponent(componentType); |
| 642 | |
| 643 | // 缓存结果 |
| 644 | this.addToCache(cacheKey, entities); |
| 645 | |
| 646 | return { |
| 647 | entities, |
| 648 | count: entities.length, |
| 649 | executionTime: performance.now() - startTime, |
| 650 | fromCache: false |
| 651 | }; |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * 从缓存获取查询结果 |
no test coverage detected