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 빼기가 원본과 같다)
Grid utility.
- A port of the original misc/grid.js. Gathers things rebuilt in every grid game, like connectivity checks (flood fill) and tile collision checks.
- Whether a cell is passable is decided by the is_passable_handler(column, row) callback. All functions are static.
- Differences from the original: The original { column, row } object is returned as GridCell, and the { x, y } of to_world_center as Vector2. Cell indexes are i32 so negative cells (outside the top left of the world) can be represented. Coordinates and cell math are f64 like the original (JS number). (the 0.000001 subtraction in test_rect_overlap matches the original)
GridCellstruct traits Clone, Copy, Debug, Default, PartialEq, Eq, Hash
칸 좌표. (원본 { column, row })
Cell coordinate. (original { column, row })
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));생성.
Constructor.
- columni32
- 가로 칸 번호.Horizontal cell index.
- rowi32
- 세로 칸 번호.Vertical cell index.
반환ReturnsGridCell 그 칸 좌표.That cell coordinate.
가로 칸 번호. (왼쪽이 0, 바깥은 음수)
Horizontal cell index. (left is 0, outside is negative)
반환Returnsi32
세로 칸 번호. (위가 0, 바깥은 음수)
Vertical cell index. (top is 0, outside is negative)
반환Returnsi32
Gridstruct
그리드 유틸. (정적 함수 묶음)
Grid utility. (a set of static functions)
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>#플러드필. (시작 칸에서 상하좌우로 이어지는 통과 가능한 칸들을 모두 방문한 순서대로 돌려준다)
- 콜백은 격자 안의 칸으로만 부른다. 스택으로 돌아 깊이 우선 차례다.
Flood fill. (returns every passable cell connected up, down, left, and right from the start cell, in visiting order)
- The callback is only called with cells inside the grid. Runs with a stack, so the order is depth first.
- column_counti32
- 가로 칸 수.Number of columns.
- row_counti32
- 세로 칸 수.Number of rows.
- startGridCell
- 시작 칸.Start cell.
- is_passable_handlerimpl FnMut(i32, i32) -> bool
- (column, row) 칸을 지날 수 있으면 true 를 돌려주는 함수.Function that returns true if the (column, row) cell is passable.
반환ReturnsVec<GridCell> 닿은 칸 목록. (시작 칸이 맨 앞) 시작 칸이 격자 밖이거나 막혀 있으면 빈 목록.List of reached cells. (start cell first) An empty list if the start cell is outside the grid or blocked.
짝Counterpart: vanilla.js Grid.floodFill
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#연결성 검사. (시작 칸에서 닿는 칸 수가 expected_count 와 같은지. 예: 모든 부품이 코어와 붙어 있는지)
Connectivity check. (whether the number of cells reached from the start cell equals expected_count. e.g. whether every part is connected to the core)
- column_counti32
- 가로 칸 수.Number of columns.
- row_counti32
- 세로 칸 수.Number of rows.
- startGridCell
- 시작 칸. (예: 코어 자리)Start cell. (e.g. the core position)
- is_passable_handlerimpl FnMut(i32, i32) -> bool
- (column, row) 칸을 지날 수 있으면 true 를 돌려주는 함수.Function that returns true if the (column, row) cell is passable.
- expected_countusize
- 모두 이어져 있으면 닿아야 할 칸 수. (예: 부품 수)Number of cells that should be reached if everything is connected. (e.g. the part count)
반환Returnsbool flood_fill 로 닿은 칸 수가 expected_count 와 같으면 true.true if the number of cells reached by flood_fill equals expected_count.
짝Counterpart: vanilla.js Grid.isAllConnected
pub fn test_rect_overlap(world_rect: Rect, tile_size: f64, mut is_blocked_handler: impl FnMut(i32, i32) -> bool) -> bool#사각형-타일 충돌 검사. (월드 사각형이 걸치는 타일을 돌며 막힌 타일과 겹치면 true)
- 이동 판정에서 "이동 뒤 자리가 장애물과 겹치면 취소" 하는 데 쓴다.
- 오른쪽, 아래 끝이 타일 경계에 딱 맞으면 그 다음 타일은 걸치지 않는다. 위에서 아래, 왼쪽에서 오른쪽 차례로 묻고 처음 막힌 타일에서 멈춘다.
Rectangle-tile collision test. (walks the tiles a world rectangle spans and returns true if it overlaps a blocked tile)
- Used in movement checks to "cancel if the position after moving overlaps an obstacle".
- If the right or bottom edge lands exactly on a tile boundary, the next tile is not spanned. Asks top to bottom, left to right, and stops at the first blocked tile.
- world_rectRect
- 검사할 월드 사각형.World rectangle to test.
- tile_sizef64
- 타일 한 변의 길이. (월드 단위)Length of one side of a tile. (world units)
- is_blocked_handlerimpl FnMut(i32, i32) -> bool
- (column, row) 타일이 막혔으면 true 를 돌려주는 함수. (음수 칸도 묻는다)Function that returns true if the (column, row) tile is blocked. (negative cells are asked too)
반환Returnsbool 걸치는 타일 가운데 막힌 것이 있으면 true.true if any spanned tile is blocked.
짝Counterpart: vanilla.js Grid.testRectOverlap
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));월드 좌표 → 타일 좌표.
World coordinate → tile coordinate.
- world_xf64
- 월드 x.World x.
- world_yf64
- 월드 y.World y.
- tile_sizef64
- 타일 한 변의 길이. (월드 단위)Length of one side of a tile. (world units)
반환ReturnsGridCell 그 점이 든 칸. (내림하므로 음수 좌표는 음수 칸)The cell containing that point. (rounds down, so negative coordinates give negative cells)
짝Counterpart: vanilla.js Grid.toCell
타일 좌표 → 타일 가운데 월드 좌표.
Tile coordinate → world coordinate of the tile center.
- columni32
- 가로 칸 번호.Horizontal cell index.
- rowi32
- 세로 칸 번호.Vertical cell index.
- tile_sizef64
- 타일 한 변의 길이. (월드 단위)Length of one side of a tile. (world units)
반환ReturnsVector2 그 칸 가운데의 월드 좌표.World coordinate of that cell's center.
짝Counterpart: vanilla.js Grid.toWorldCenter