1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-09 16:21:51 +00:00

Use Set for entities access during belt recompute

Create a method to return all entities with the specified component as a
set (without array indirection), and use the method for belt
recomputations.
This commit is contained in:
Даниїл Григор'єв 2025-05-03 02:59:57 +03:00
parent 9cbb797ef6
commit 69fb06e817
No known key found for this signature in database
GPG Key ID: B890DF16341D8C1D
2 changed files with 11 additions and 6 deletions

View File

@ -149,12 +149,19 @@ export class EntityManager extends BasicSerializableObject {
/**
* Returns all entities having the given component
* @deprecated use {@link getEntitiesWithComponent} instead
*/
getAllWithComponent(componentHandle: typeof Component): Entity[] {
// TODO: Convert usages to set as well
return [...(this.componentToEntity[componentHandle.getId()] ?? new Set())];
}
/**
* A version of {@link getAllWithComponent} that returns a Set
*/
getEntitiesWithComponent(componentHandle: typeof Component): Set<Entity> {
return new Set(this.componentToEntity[componentHandle.getId()] ?? []);
}
/**
* Unregisters all components of an entity from the component to entity mapping
*/

View File

@ -7,12 +7,11 @@ import { AtlasSprite } from "../../core/sprites";
import { fastArrayDeleteValue } from "../../core/utils";
import { enumDirection, enumDirectionToVector, enumInvertedDirections, Vector } from "../../core/vector";
import { BeltPath } from "../belt_path";
import { arrayBeltVariantToRotation, MetaBeltBuilding } from "../buildings/belt";
import { getCodeFromBuildingData } from "../building_codes";
import { arrayBeltVariantToRotation, MetaBeltBuilding } from "../buildings/belt";
import { BeltComponent } from "../components/belt";
import { Entity } from "../entity";
import { GameSystem } from "../game_system";
import { GameSystemWithFilter } from "../game_system_with_filter";
import { MapChunkView } from "../map_chunk_view";
import { defaultBuildingVariant } from "../meta_building";
@ -424,10 +423,9 @@ export class BeltSystem extends GameSystem {
const result = [];
const beltEntities = this.root.entityMgr.getAllWithComponent(BeltComponent);
const beltEntities = this.root.entityMgr.getEntitiesWithComponent(BeltComponent);
for (let i = 0; i < beltEntities.length; ++i) {
const entity = beltEntities[i];
for (const entity of beltEntities) {
if (visitedUids.has(entity.uid)) {
continue;
}