# misc/grid (vanilla-engine 0.4.0)

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

그리드 유틸.
- 원본 misc/grid.js 를 옮긴 것이다. 연결성 검사(플러드필)와 타일 충돌 판정처럼 격자 게임마다 다시 만들던 것을 모았다.
- 칸의 통과 여부는 is_passable_handler(column, row) 콜백으로 정한다. 모두 정적 함수다.
- 원본과 다른 점:
  원본의 { column, row } 객체는 GridCell, to_world_center 의 { x, y } 는 Vector2 로 돌려준다.
  칸 번호는 음수 칸(월드 왼쪽 위 바깥)도 나타내도록 i32 다.
  좌표와 칸 계산은 원본(JS number)처럼 f64 다. (test_rect_overlap 의 0.000001 빼기가 원본과 같다)

## GridCell (struct) (Clone, Copy, Debug, Default, PartialEq, Eq, Hash)

칸 좌표. (원본 { column, row })

예제: 월드 좌표와 칸 좌표 오가기

```rust
use vanilla::{Grid, GridCell, Vector2};

let cell = Grid::to_cell(70.0, 31.9, 32.0);
assert_eq!(cell, GridCell::create(2, 0));
assert_eq!(Grid::to_world_center(cell.column, cell.row, 32.0), Vector2::create(80.0, 16.0));
assert_eq!(Grid::to_cell(-1.0, 0.0, 32.0), GridCell::create(-1, 0));
```

- `pub column: i32` (field, since 0.3.2): 가로 칸 번호. (왼쪽이 0, 바깥은 음수)
  - returns i32
- `pub row: i32` (field, since 0.3.2): 세로 칸 번호. (위가 0, 바깥은 음수)
  - returns i32
- `pub const fn create(column: i32, row: i32) -> GridCell` (static, since 0.3.2): 생성.
  - `column` i32: 가로 칸 번호.
  - `row` i32: 세로 칸 번호.
  - returns GridCell: 그 칸 좌표.

## Grid (struct)

그리드 유틸. (정적 함수 묶음)

예제: 벽으로 나뉜 구역 세기

```rust
use vanilla::{Grid, GridCell};

let map_rows = ["..#..", "..#..", "###..", "....."];
let is_passable = |column: i32, row: i32| map_rows[row as usize].as_bytes()[column as usize] != b'#';

let top_left_area = Grid::flood_fill(5, 4, GridCell::create(0, 0), is_passable);
assert_eq!(top_left_area.len(), 4);
assert_eq!(top_left_area[0], GridCell::create(0, 0));

assert!(Grid::is_all_connected(5, 4, GridCell::create(4, 3), is_passable, 11));
assert!(Grid::flood_fill(5, 4, GridCell::create(2, 0), is_passable).is_empty());
```

- `pub fn flood_fill(column_count: i32, row_count: i32, start: GridCell, mut is_passable_handler: impl FnMut(i32, i32) -> bool) -> Vec<GridCell>` (static, since 0.3.2): 플러드필. (시작 칸에서 상하좌우로 이어지는 통과 가능한 칸들을 모두 방문한 순서대로 돌려준다)
  - 콜백은 격자 안의 칸으로만 부른다. 스택으로 돌아 깊이 우선 차례다.
  - `column_count` i32: 가로 칸 수.
  - `row_count` i32: 세로 칸 수.
  - `start` GridCell: 시작 칸.
  - `is_passable_handler` impl FnMut(i32, i32) -> bool: (column, row) 칸을 지날 수 있으면 true 를 돌려주는 함수.
  - returns Vec<GridCell>: 닿은 칸 목록. (시작 칸이 맨 앞) 시작 칸이 격자 밖이거나 막혀 있으면 빈 목록.
- `pub fn is_all_connected(column_count: i32, row_count: i32, start: GridCell, is_passable_handler: impl FnMut(i32, i32) -> bool, expected_count: usize) -> bool` (static, since 0.3.2): 연결성 검사. (시작 칸에서 닿는 칸 수가 expected_count 와 같은지. 예: 모든 부품이 코어와 붙어 있는지)
  - `column_count` i32: 가로 칸 수.
  - `row_count` i32: 세로 칸 수.
  - `start` GridCell: 시작 칸. (예: 코어 자리)
  - `is_passable_handler` impl FnMut(i32, i32) -> bool: (column, row) 칸을 지날 수 있으면 true 를 돌려주는 함수.
  - `expected_count` usize: 모두 이어져 있으면 닿아야 할 칸 수. (예: 부품 수)
  - returns bool: flood_fill 로 닿은 칸 수가 expected_count 와 같으면 true.
- `pub fn test_rect_overlap(world_rect: Rect, tile_size: f64, mut is_blocked_handler: impl FnMut(i32, i32) -> bool) -> bool` (static, since 0.3.2): 사각형-타일 충돌 검사. (월드 사각형이 걸치는 타일을 돌며 막힌 타일과 겹치면 true)
  - 이동 판정에서 "이동 뒤 자리가 장애물과 겹치면 취소" 하는 데 쓴다.
  - 오른쪽, 아래 끝이 타일 경계에 딱 맞으면 그 다음 타일은 걸치지 않는다. 위에서 아래, 왼쪽에서 오른쪽 차례로 묻고 처음 막힌 타일에서 멈춘다.
  - `world_rect` Rect: 검사할 월드 사각형.
  - `tile_size` f64: 타일 한 변의 길이. (월드 단위)
  - `is_blocked_handler` impl FnMut(i32, i32) -> bool: (column, row) 타일이 막혔으면 true 를 돌려주는 함수. (음수 칸도 묻는다)
  - returns bool: 걸치는 타일 가운데 막힌 것이 있으면 true.

  예제: 움직인 뒤 벽과 겹치면 움직임 취소

  ```rust
  use vanilla::{Grid, Rect, Vector2};
  
  let is_blocked = |column: i32, _row: i32| column >= 3;
  let mut hero_position = Vector2::create(40.0, 0.0);
  let hero_size = Vector2::create(24.0, 24.0);
  let next_position = Vector2::create(hero_position.x + 40.0, hero_position.y);
  let next_rect = Rect::create(next_position.x, next_position.y, hero_size.x, hero_size.y);
  if !Grid::test_rect_overlap(next_rect, 32.0, is_blocked) {
  	hero_position = next_position;
  }
  assert_eq!(hero_position, Vector2::create(40.0, 0.0));
  ```

- `pub fn to_cell(world_x: f64, world_y: f64, tile_size: f64) -> GridCell` (static, since 0.3.2): 월드 좌표 → 타일 좌표.
  - `world_x` f64: 월드 x.
  - `world_y` f64: 월드 y.
  - `tile_size` f64: 타일 한 변의 길이. (월드 단위)
  - returns GridCell: 그 점이 든 칸. (내림하므로 음수 좌표는 음수 칸)
- `pub fn to_world_center(column: i32, row: i32, tile_size: f64) -> Vector2` (static, since 0.3.2): 타일 좌표 → 타일 가운데 월드 좌표.
  - `column` i32: 가로 칸 번호.
  - `row` i32: 세로 칸 번호.
  - `tile_size` f64: 타일 한 변의 길이. (월드 단위)
  - returns Vector2: 그 칸 가운데의 월드 좌표.
