From 69fb06e81774c83f11f3b1054c41a4a6d9513a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=97=D0=BB=20=D0=93=D1=80=D0=B8?= =?UTF-8?q?=D0=B3=D0=BE=D1=80=27=D1=94=D0=B2?= Date: Sat, 3 May 2025 02:59:57 +0300 Subject: [PATCH] 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. --- src/js/game/entity_manager.ts | 9 ++++++++- src/js/game/systems/belt.js | 8 +++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/js/game/entity_manager.ts b/src/js/game/entity_manager.ts index 0d603a9e..0e0e4645 100644 --- a/src/js/game/entity_manager.ts +++ b/src/js/game/entity_manager.ts @@ -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 { + return new Set(this.componentToEntity[componentHandle.getId()] ?? []); + } + /** * Unregisters all components of an entity from the component to entity mapping */ diff --git a/src/js/game/systems/belt.js b/src/js/game/systems/belt.js index c0db6ff7..6893e546 100644 --- a/src/js/game/systems/belt.js +++ b/src/js/game/systems/belt.js @@ -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; }