@tamb/gamegrid
    Preparing search index...

    Class GameGrid

    Stateful 2‑D lattice with collision rules and optional HTMLElement projection.

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

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

    window.addEventListener(gridEventsEnum.MOVE_LAND, (e: Event) => {
    const { gameGridInstance } = (e as GameGridDOMEvent).detail;
    console.log(gameGridInstance.getState().activeCoords);
    });
    const grid = new GameGrid({ matrix });
    grid.moveRight(); // mutates internal state without touching the DOM
    grid.render(document.querySelector("#stage")!); // mount later

    Implements

    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]; // 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] });
    • 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 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);

    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")!);
    • 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;
    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);
      
    • 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);
      
    • 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);
      
    • 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);
      
    • Copies matrix/config, merges IConfig.state, runs GameGrid.render when container is passed, then dispatches gridEventsEnum.CREATED.

      Parameters

      Returns GameGrid

      const grid = new GameGrid({ matrix, options: { wasdControls: true } }, root);
      
      const grid = new GameGrid({ matrix, state: { activeCoords: [1, 0] } });
      grid.moveDown();
      grid.render(root);