mwg API
    Preparing search index...

    Class LayeredSprite

    A character assembled from independently swappable, independently tintable layers - skin, eyes, hair, garments, worn equipment - rather than one flat sprite per combination. A second palette is a colour change on one layer; worn equipment shows because it is another layer added on top, not a redrawn sprite for every gear combination a game might have. tools/make-example-assets.mjs already draws its characters this way; this is the runtime counterpart that keeps the layers together as one sprite that moves as a unit.

    import { LayeredSprite } from '@datamoc/mw_games/two-d/render';
    import type { Texture2D } from '@datamoc/mw_games/two-d/render';

    declare const skinTexture: Texture2D;
    declare const hairTexture: Texture2D;
    declare const swordTexture: Texture2D;

    const hero = new LayeredSprite();
    hero.addLayer('skin', skinTexture, 0);
    hero.addLayer('hair', hairTexture, 1);
    hero.addLayer('weapon', swordTexture, 2);

    hero.layer('hair')?.lerpTint(0x8b4513, 1); // dye it brown
    hero.setTexture('weapon', swordTexture); // equip a different sword later
    console.log(hero.hasLayer('weapon')); // true

    hero.alpha = 0.5; // the whole assembled character turns translucent, every layer at once

    LayeredSprite is a plain Pixi Container, not itself a TintedSprite: its own alpha still fades every layer together, since Pixi composes a container's alpha into each child's when rendering, the same way it would for any other container of sprites. A single layer can still be faded on its own through layer(name), which returns the TintedSprite directly.

    Hierarchy

    • Container
      • LayeredSprite
    Index
    • Parameters

      • Optionaloptions: ContainerOptions<ContainerChild>

      Returns LayeredSprite

    • Adds or replaces a named layer.

      Parameters

      • name: string
      • texture: Texture
      • order: number = 0

        stacking order, higher drawn on top; equal order keeps insertion order

      Returns TintedSprite

    • swaps a layer's texture without touching its tint, order, or identity - gear changing

      Parameters

      • name: string
      • texture: Texture

      Returns void