hex-world
    Preparing search index...

    Class TerritoryLayer

    Per-cell ownership: who holds which hex, drawn as translucent faction tints with an outline around each faction's holdings.

    Ownership lives in the map's metadata channel (HexMap.cellData), so it serializes with the map for free — save through serializeMap / serializeMapJSON / .hexpack and the borders come back exactly as they were, no companion file. The layer itself holds no cell state; it is a view over that data plus a faction roster.

    A cell can be held outright (claim) or contested (setInfluence), where several factions hold fractional weights and the fill is their weighted color blend. Contested cells count toward whichever faction has the largest share for border and ownedCells purposes.

    Mutations only mark the layer dirty; call update once per frame (or refresh straight after a batch of edits) to rebuild the geometry, so a thousand-cell flood fill costs one rebuild instead of a thousand.

    const territory = new TerritoryLayer({
    overlays: world.overlays,
    map: () => world.map,
    factions: [
    { id: 'red', name: 'Kelmar', color: 0xdd4433 },
    { id: 'blue', name: 'Ossiran', color: 0x3377dd },
    ],
    });
    territory.claim(10, 10, 'red');
    territory.setInfluence(11, 10, { red: 0.6, blue: 0.4 }); // contested border hex
    territory.refresh();
    Index

    Constructors

    Accessors

    Methods

    • The faction holding a cell — the sole owner, or the largest share of a contested cell. null when unowned. Ties resolve to the roster order so the answer is stable across calls.

      Parameters

      • col: number
      • row: number

      Returns string | null

    • Per-faction influence on a cell, normalized to sum to 1. An outright claim reads as { [ownerId]: 1 }. null when unowned.

      Parameters

      • col: number
      • row: number

      Returns Record<string, number> | null

    • Every cell whose dominant owner is factionId.

      Parameters

      • factionId: string

      Returns { col: number; row: number }[]

    • Cell counts per faction id, for scoreboards. Unowned cells are not counted.

      Returns Map<string, number>

    • Give a cell outright to a faction. Out-of-bounds cells are skipped.

      Parameters

      • col: number
      • row: number
      • factionId: string

      Returns void

    • Give many cells to one faction — a conquest, a starting region, a flood fill.

      Parameters

      • cells: Iterable<{ col: number; row: number }>
      • factionId: string

      Returns void

    • Mark a cell contested, with a weight per faction (any positive scale — they are normalized on read). Weights of zero or less are dropped; an empty result releases the cell.

      Parameters

      • col: number
      • row: number
      • weights: Record<string, number>

      Returns void

    • Return many cells to no-one.

      Parameters

      • cells: Iterable<{ col: number; row: number }>

      Returns void

    • Wipe all ownership from the map, leaving every other metadata key intact.

      Parameters

      • OptionalfactionId: string

        Limit the wipe to one faction's holdings.

      Returns void

    • Show or hide the whole layer without discarding ownership data.

      Parameters

      • visible: boolean

      Returns void

    • Rebuild only if something changed since the last build. Cheap to call every frame.

      Returns void

    • Rebuild the territory geometry now: one vertex-colored fill over every owned cell (contested cells carrying their blended tint) plus one outline per faction.

      Returns void