@tamb/gamegrid
    Preparing search index...

    Interface IGameGrid

    Public contract implemented by GameGrid.

    Methods stay flat on the instance. Docs group them by job (not DOM vs data): Matrix (logical cells), Movement (focus and history), View (optional markup), State, Options, and Zoom.

    GameGrid.setCell / GameGrid.setMatrix write the matrix only. GameGrid.refreshCells / GameGrid.refresh / GameGrid.render paint. Movement updates state and events; it highlights when mounted.

    For DOM events (CustomEvents), see IGameGridEventDetail and gridEventsEnum.

    import GameGrid, { cellTypeEnum, gridEventsEnum, type GameGridDOMEvent } from "@tamb/gamegrid";

    const matrix = [
    [{ type: cellTypeEnum.OPEN }, { type: cellTypeEnum.BARRIER }],
    [{ type: cellTypeEnum.OPEN }, { type: cellTypeEnum.OPEN }],
    ];

    const grid = new GameGrid(
    { matrix, options: { wasdControls: true } },
    document.querySelector("#root")!,
    );

    grid.moveDown();
    window.addEventListener(gridEventsEnum.MOVE_LAND, (e: Event) => {
    const { gameGridInstance } = (e as GameGridDOMEvent).detail;
    console.log(gameGridInstance.getActiveCell().type);
    });
    interface IGameGrid {
        options: IOptions;
        refs: IRefsObject;
        clearZoom(options?: IZoomOptions): void;
        destroy(): void;
        getActiveCell(): ICell;
        getActiveRegion(): IRegionTile | null;
        getAllCellsByType(type: string): ICell[];
        getCell(coords: number[] | readonly [number, number]): ICell;
        getFractionZoom(
            divisions: number,
            tileX: number,
            tileY: number,
        ): IZoomBounds;
        getMatrix(): ICell[][];
        getOptions(): IOptions;
        getPreviousCell(): ICell;
        getQuadrantZoom(quadrant: ZoomQuadrant): IZoomBounds;
        getRegionAt(
            coords: number[] | readonly [number, number],
            divisions?: number,
        ): IRegionTile;
        getState(): IState;
        getZoom(): IZoomBounds | null;
        getZoomAround(
            center: number[] | readonly [number, number],
            radiusX: number,
            radiusY?: number,
        ): IZoomBounds;
        moveDown(): void;
        moveLeft(): void;
        moveRight(): void;
        moveTo(
            coordsOrPath:
                | number[]
                | readonly [number, number]
                | (number[] | readonly [number, number])[],
        ): void;
        moveUp(): void;
        refresh(): void;
        refreshCells(cells: ICellRefresh | ICellRefresh[]): void;
        render(container: HTMLElement): void;
        rewind(steps?: number): void;
        rewindTo(index: number): void;
        setActiveCell(x: number, y: number, direction?: string): void;
        setCell(coords: number[] | readonly [number, number], cell: ICell): void;
        setMatrix(matrix: ICell[][]): void;
        setOptions(newOptions: IOptions): void;
        setStateSync(obj: StatePatch): void;
        setZoom(bounds: IZoomBounds, options?: IZoomOptions): void;
        unrewind(steps?: number): void;
        unrewindTo(index: number): void;
        zoomAround(
            center: number[] | readonly [number, number],
            radiusX: number,
            radiusY?: number,
            options?: IZoomOptions,
        ): void;
        zoomFraction(
            divisions: number,
            tileX: number,
            tileY: number,
            options?: IZoomOptions,
        ): void;
        zoomQuadrant(quadrant: ZoomQuadrant, options?: IZoomOptions): void;
    }

    Implemented by

    Index

    Logical grid data. GameGrid.setCell and GameGrid.setMatrix do not paint. Call a View method when mounted nodes should catch up. GameGrid.getActiveCell / GameGrid.getPreviousCell read matrix fields immediately and overlay current from refs.

    Focus and history. Updates IState, fires callbacks and gridEventsEnum events, and highlights the active cell when rendered. Not a matrix write.

    Mount, paint, and tear down markup. Optional — omit the constructor container and skip GameGrid.render for headless use. GameGrid.refreshCells also writes the matrix when cell is provided.

    Authoritative IState. GameGrid.setStateSync runs middleware and does not emit grid CustomEvents.

    Runtime behaviour toggles. GameGrid.setOptions does not swap the matrix or re-render.

    Viewport window and region tiles. Applying zoom rebuilds the visible window when mounted.

    • Logical cell from GameGrid.getMatrix: matrix[coords[1]][coords[0]] — raw matrix lookup (bounds unchecked).

      Parameters

      • coords: number[] | readonly [number, number]

        [x, y].

      Returns ICell

      const cell = grid.getCell([2, 1]); // column 2, row 1
      
    • Logical matrix backing the grid (matrix[row][column]matrix[y][x]).

      Returns ICell[][]

      const rows = grid.getMatrix();
      const cell = rows[1][2]; // row 1, column 2 — same as getCell([2, 1])
    • Replace the logical cell at coords (matrix[y][x]). Does not render, refresh, or patch DOM/refs.

      Parameters

      • coords: number[] | readonly [number, number]

        [x, y].

      • cell: ICell

        Stored by reference, same as GameGrid.setMatrix.

      Returns void

      Bounds unchecked, matching GameGrid.getCell. Data-only: does not patch DOM, refs, or emit events. Movement / blockOnType read the new cell immediately. Call GameGrid.refreshCells with { coords } (or { coords, cell } instead of this method) to update mounted nodes; use GameGrid.refresh / GameGrid.render when the grid shape changes.

      grid.setCell([2, 1], { type: cellTypeEnum.INTERACTIVE });
      grid.refreshCells({ coords: [2, 1] }); // paint if mounted
    • Walk to one cell, or along an explicit list of cells, through GameGrid.setActiveCell.

      Parameters

      • coordsOrPath: number[] | readonly [number, number] | (number[] | readonly [number, number])[]

        A single [x, y] or an array of [x, y] steps. Not pathfinding (no A*): gaps teleport.

      Returns void

      Each step uses the existing block / collide / wrap / zoom-edge rules. Stops when a step does not land on the requested cell (blocked, finite-edge clamp, or wrap to a different cell). Skips steps that are already the active cell. Not rate-limited by IOptions.moveDebounce. Does not dispatch directional gridEventsEnum.MOVE_UP / MOVE_RIGHT / MOVE_DOWN / MOVE_LEFT (same as a cell click).

      grid.moveTo([2, 1]);
      
      grid.moveTo([
      [0, 1],
      [0, 2],
      [1, 2],
      ]);
    • Jump to index in IState.moves (0 = oldest remaining).

      Parameters

      • index: number

      Returns void

      No-op when index is not an integer in range, or it is already the current (last) entry. Truncates history after the chosen index (later entries move to IState.future). Same events as GameGrid.rewind.

      grid.rewindTo(0); // jump to the oldest remaining history entry
      
    • Jump forward to index in the combined trail (moves then future).

      Parameters

      • index: number

      Returns void

      index is counted from the oldest remaining IState.moves entry (0), through the current cell, into IState.future. No-op when index is not an integer strictly ahead of the current entry, or past the newest future coord. Same events as GameGrid.unrewind.

      grid.rewind(3);
      grid.unrewindTo(2); // jump forward in the combined moves + future trail

    After GameGrid.render, hydrated rows/cells and container. Headless grids mirror cells onto the logical matrix until mount.

    const root = grid.refs.container; // HTMLElement after render, null while headless
    
    • Detach listeners when rendered and clear injected structure; resets rendered in state via GameGrid.setStateSync.

      Returns void

      Idempotent-friendly: always dispatches gridEventsEnum.DESTROYED whether or not DOM was present. Middleware pre / post run for the rendered: false patch.

      grid.destroy();
      
    • Write optional cell data and rebuild one or more cell nodes from the current matrix.

      Parameters

      Returns void

      Flow: GameGrid.setCell is data-only (movement reads the new type immediately; DOM/refs stay stale). Call this afterward with { coords } to paint those tiles, or pass { coords, cell } to write and paint in one step. Headless grids update matrix data only. Off-screen cells under zoom stay current: null. Does not rebuild the whole grid — use GameGrid.refresh when dimensions or the zoom window change. Dispatches gridEventsEnum.CELLS_REFRESHED once with detail.cells.

      grid.setCell([2, 0], { type: cellTypeEnum.OPEN });
      grid.refreshCells({ coords: [2, 0] });
      grid.refreshCells({ coords: [1, 1], cell: { type: cellTypeEnum.BARRIER } });
      
    • Mount markup into container, wire keyboard/pointer handlers, and highlight the current active cell.

      Parameters

      • container: HTMLElement

      Returns void

      Clears/rebuilds refs for this mount. Prefer GameGrid.refresh after the first paint when rebuilding from the same host. Dispatches gridEventsEnum.RENDERED once the container is patched and listeners attach. Does not call GameGrid.setActiveCell — no move / collide / land / ICell.eventTypes events, and currentDirection is left as-is. Skips stylesheet injection when IOptions.injectStyles is false.

      const grid = new GameGrid({ matrix });
      grid.moveRight();
      grid.render(document.querySelector("#stage")!);
    • Authoritative IState backing movement callbacks and renders.

      Returns IState

      const { activeCoords, moves, future } = grid.getState();
      
    • Apply partial state with MiddlewareFn pre (mutate patch) → merge → post.

      Parameters

      Returns void

      Middleware runs around the merge inside this call; does not emit grid CustomEvents.

      grid.setStateSync({ activeCoords: [1, 0], myScore: 3 });
      
    options: IOptions

    Runtime toggles: input, collisions, middleware, callbacks, styling. Merged from ctor defaults and GameGrid.setOptions.

    grid.options.wasdControls; // current merged value
    grid.setOptions({ wasdControls: true });
    • Shallow-merge behaviours into IGameGrid.options without swapping the matrix snapshot or re-rendering.

      Parameters

      Returns void

      grid.setOptions({ moveDebounce: 40, wasdControls: true });
      
    • Compute zoom bounds for a fraction tile (divisions×divisions grid).

      Parameters

      • divisions: number
      • tileX: number
      • tileY: number

      Returns IZoomBounds

      const tile = grid.getFractionZoom(3, 1, 1); // center ninth
      
    • Region tile for coords; divisions defaults to IOptions.regionDivisions.

      Parameters

      • coords: number[] | readonly [number, number]
      • Optionaldivisions: number

      Returns IRegionTile

      const region = grid.getRegionAt([4, 1], 2);
      
    • Current zoom bounds or null when no zoom is active.

      Returns IZoomBounds | null

      const zoom = grid.getZoom(); // { minX, minY, maxX, maxY } or null
      
    • Compute zoom bounds around a center cell ± radii, clipped to the matrix.

      Parameters

      • center: number[] | readonly [number, number]
      • radiusX: number
      • OptionalradiusY: number

      Returns IZoomBounds

      const bounds = grid.getZoomAround([2, 2], 1); // 3×3 window around [2, 2]
      
    • Compute bounds then GameGrid.setZoom.

      Parameters

      • center: number[] | readonly [number, number]
      • radiusX: number
      • OptionalradiusY: number
      • Optionaloptions: IZoomOptions

      Returns void

      grid.zoomAround([2, 2], 1, 1, { animate: true });
      
    • Compute fraction bounds then GameGrid.setZoom.

      Parameters

      • divisions: number
      • tileX: number
      • tileY: number
      • Optionaloptions: IZoomOptions

      Returns void

      grid.zoomFraction(3, 0, 1);