* 批量添加实体 * * 高效地批量添加多个实体,减少缓存清理次数。 * 使用Set来避免O(n)的重复检查。 * * @param entities 要添加的实体列表
(entities: Entity[])
| 132 | * @param entities 要添加的实体列表 |
| 133 | */ |
| 134 | public addEntities(entities: Entity[]): void { |
| 135 | if (entities.length === 0) return; |
| 136 | |
| 137 | // 使用Set来快速检查重复 |
| 138 | const existingIds = new Set(this._entities.map((e) => e.id)); |
| 139 | let addedCount = 0; |
| 140 | |
| 141 | for (const entity of entities) { |
| 142 | if (!existingIds.has(entity.id)) { |
| 143 | this._entities.push(entity); |
| 144 | this.addEntityToIndexes(entity); |
| 145 | |
| 146 | // 更新索引管理器 |
| 147 | this._archetypeSystem.addEntity(entity); |
| 148 | |
| 149 | existingIds.add(entity.id); |
| 150 | addedCount++; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // 只在有实体被添加时才清理缓存 |
| 155 | if (addedCount > 0) { |
| 156 | this.clearQueryCache(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * 批量添加实体(无重复检查版本) |
no test coverage detected