# misc/grid (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/grid/

Grid utility.

## Grid extends Object

Grid utility.
- Gathers things reinvented in every grid game, like connectivity checks (flood fill) and tile collision tests.
- Whether a cell is passable is decided by the isPassableHandler(column, row) callback.

Example: Count connected cells and convert coordinates

```js
import { Grid } from "./build/vanilla.js";

const mapRows = [
	"..#.",
	"..#.",
	"..#.",
];
const isPassable = (column, row) => {
	return mapRows[row][column] === ".";
};
const visitedList = Grid.floodFill(4, 3, { column: 0, row: 0 }, isPassable);
console.log(visitedList.length);
console.log(Grid.isAllConnected(4, 3, { column: 0, row: 0 }, isPassable, 9));

const cell = Grid.toCell(70, 20, 32);
const center = Grid.toWorldCenter(cell.column, cell.row, 32);
console.log(cell.column, cell.row, center.x, center.y);
```

- `static floodFill(columnCount, rowCount, start, isPassableHandler)` (static, since 0.3.0): Flood fill. (static)
  - Visits all passable cells connected up, down, left and right from the start cell, and returns them as an array.
  - `columnCount` number: Number of grid columns.
  - `rowCount` number: Number of grid rows.
  - `start` object: { column, row }
  - `isPassableHandler` Function: (column, row) => boolean
  - returns object[]: Array of { column, row } of the visited cells.
- `static isAllConnected(columnCount, rowCount, start, isPassableHandler, expectedCount)` (static, since 0.3.0): Connectivity check. (static)
  - Tests whether the number of cells reachable from the start cell equals expectedCount.
    (e.g. whether all parts are attached to the core in an assembly game)
  - `columnCount` number: Number of grid columns.
  - `rowCount` number: Number of grid rows.
  - `start` object: { column, row }
  - `isPassableHandler` Function: (column, row) => boolean
  - `expectedCount` number: Number of cells that should be connected. (including the start cell)
  - returns boolean: True if the number of cells reachable from the start cell equals expectedCount.
- `static testRectOverlap(worldRect, tileSize, isBlockedHandler)` (static, since 0.3.0): Rectangle-tile collision check. (static)
  - Iterates over the tile range a world rectangle spans and tests whether it overlaps a blocked tile.
  - Used in movement tests as "cancel if the position after moving overlaps an obstacle".
  - `worldRect` Rect: World rectangle to test.
  - `tileSize` number: Size of one tile. (world units)
  - `isBlockedHandler` Function: (column, row) => boolean
  - returns boolean: True if it overlaps a blocked tile.

  Example: Check whether the destination overlaps a wall

  ```js
  import { Grid, Rect } from "./build/vanilla.js";
  
  const mapRows = [
  	"....",
  	".#..",
  ];
  const isBlocked = (column, row) => {
  	if (row < 0 || row >= mapRows.length || column < 0 || column >= mapRows[row].length) {
  		return true;
  	}
  	return mapRows[row][column] === "#";
  };
  const nextRect = Rect.create(20, 20, 16, 16);
  console.log(Grid.testRectOverlap(nextRect, 32, isBlocked));
  const freeRect = Rect.create(70, 4, 16, 16);
  console.log(Grid.testRectOverlap(freeRect, 32, isBlocked));
  ```

- `static toCell(worldX, worldY, tileSize)` (static, since 0.3.0): World coordinates → tile coordinates. (static)
  - `worldX` number: World X coordinate.
  - `worldY` number: World Y coordinate.
  - `tileSize` number: Size of one tile. (world units)
  - returns object: { column, row }
- `static toWorldCenter(column, row, tileSize)` (static, since 0.3.0): Tile coordinates → world coordinates of the tile center. (static)
  - `column` number: Column index.
  - `row` number: Row index.
  - `tileSize` number: Size of one tile. (world units)
  - returns object: { x, y }
