ecspresso
    Preparing search index...

    Function createCollisionPairHandler

    • Create a collision pair handler that routes collision events to layer-pair-specific callbacks.

      Registering "a:b" automatically handles both (layerA=a, layerB=b) and (layerA=b, layerB=a). Entity arguments are swapped to match the declared key order. If both "a:b" and "b:a" are explicitly registered, each gets its own handler with no implicit reverse.

      Type Parameters

      • W = unknown

        The ECS world type (e.g. ECSpresso<C, E, R>). Defaults to unknown.

      • L extends string = string

        Union of valid layer names. Defaults to string. Provide specific layer names for compile-time key validation: createCollisionPairHandler<ECS, keyof typeof layers>({...})

      Parameters

      • pairs: { [K in `${string}:${string}`]?: CollisionPairCallback<W> }

        Object mapping "layerA:layerB" keys to callbacks

      Returns (ctx: { data: CollisionEvent<L>; ecs: W }) => void

      A dispatch function to call with collision event data and ECS instance

      // Basic usage:
      const handler = createCollisionPairHandler<ECS>({
      'playerProjectile:enemy': (projectileId, enemyId, ecs) => {
      ecs.commands.removeEntity(projectileId);
      },
      });

      // With layer name validation:
      const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });
      type Layer = LayersOf<typeof layers>;
      const handler = createCollisionPairHandler<ECS, Layer>({
      'player:enemy': (playerId, enemyId, ecs) => { ... },
      });

      ecs.eventBus.subscribe('collision', (data) => handler({ data, ecs }));