# misc/grid (vanilla.js 0.4.0)

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

그리드 유틸.

## Grid extends Object

그리드 유틸.
- 연결성 검사(플러드필)와 타일 충돌 판정처럼 격자 게임마다 재발명되던 것을 모았다.
- 칸의 통과 여부는 isPassableHandler(column, row) 콜백으로 정한다.

예제: 이어진 칸 세기와 좌표 바꾸기

```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): 플러드필. (정적)
  - 시작 칸에서 상하좌우로 이어지는 통과 가능한 칸들을 모두 방문해 배열로 반환한다.
  - `columnCount` number: 격자의 열 수.
  - `rowCount` number: 격자의 행 수.
  - `start` object: { column, row }
  - `isPassableHandler` Function: (column, row) => boolean
  - returns object[]: 방문한 칸들의 { column, row } 배열.
- `static isAllConnected(columnCount, rowCount, start, isPassableHandler, expectedCount)` (static, since 0.3.0): 연결성 검사. (정적)
  - 시작 칸에서 도달할 수 있는 칸 수가 expectedCount 와 같은지 판정한다.
    (예: 조립형 게임에서 모든 부품이 코어와 붙어 있는지)
  - `columnCount` number: 격자의 열 수.
  - `rowCount` number: 격자의 행 수.
  - `start` object: { column, row }
  - `isPassableHandler` Function: (column, row) => boolean
  - `expectedCount` number: 이어져 있어야 할 칸 수. (시작 칸 포함)
  - returns boolean: 시작 칸에서 닿는 칸 수가 expectedCount 와 같으면 참.
- `static testRectOverlap(worldRect, tileSize, isBlockedHandler)` (static, since 0.3.0): 사각형-타일 충돌 검사. (정적)
  - 월드 사각형이 걸치는 타일 범위를 순회하며 막힌 타일과 겹치는지 판정한다.
  - 이동 판정에서 "이동 후 위치가 장애물과 겹치면 취소" 용도로 쓴다.
  - `worldRect` Rect: 검사할 월드 사각형.
  - `tileSize` number: 타일 한 칸의 크기. (월드 단위)
  - `isBlockedHandler` Function: (column, row) => boolean
  - returns boolean: 막힌 타일과 겹치면 참.

  예제: 이동할 자리가 벽과 겹치는지 보기

  ```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): 월드 좌표 → 타일 좌표. (정적)
  - `worldX` number: 월드 X 좌표.
  - `worldY` number: 월드 Y 좌표.
  - `tileSize` number: 타일 한 칸의 크기. (월드 단위)
  - returns object: { column, row }
- `static toWorldCenter(column, row, tileSize)` (static, since 0.3.0): 타일 좌표 → 타일 중심 월드 좌표. (정적)
  - `column` number: 열 번호.
  - `row` number: 행 번호.
  - `tileSize` number: 타일 한 칸의 크기. (월드 단위)
  - returns object: { x, y }
