ecspresso
    Preparing search index...

    Function createSpatialIndexPlugin

    • Create a spatial index plugin for ECSpresso.

      Provides a uniform-grid spatial hash that accelerates collision detection. When installed alongside the collision or physics2D plugins, they automatically use the spatial index for broadphase instead of O(n²) brute-force.

      Also provides proximity query methods for game logic (e.g. "find all enemies within 200 units").

      Type Parameters

      • G extends string = "spatialIndex"

      Parameters

      Returns Plugin<
          WorldConfigFrom<
              SpatialIndexComponentTypes,
              {},
              SpatialIndexResourceTypes,
          >,
          EmptyConfig,
          "spatial-index-rebuild-fixedUpdate"
          | "spatial-index-rebuild-postUpdate",
          G,
      >

      const ecs = ECSpresso.create()
      .withPlugin(createTransformPlugin())
      .withPlugin(createCollisionPlugin({ layers }))
      .withPlugin(createSpatialIndexPlugin({ cellSize: 128 }))
      .build();

      // Proximity query in a system:
      const si = ecs.getResource('spatialIndex');
      const nearby = si.queryRadius(playerX, playerY, 200);