vanilladocs
vanillapunch.com
API / misc/grid

misc/grid

그리드 유틸.

Grid utility.

Gridextends Object

짝Counterpart: vanilla-engine Grid

그리드 유틸.

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

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
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);
정적staticstatic floodFill(columnCount, rowCount, start, isPassableHandler)#0.3.0 부터since 0.3.0소스source

플러드필. (정적)

  • 시작 칸에서 상하좌우로 이어지는 통과 가능한 칸들을 모두 방문해 배열로 반환한다.

Flood fill. (static)

  • Visits all passable cells connected up, down, left and right from the start cell, and returns them as an array.
columnCountnumber
격자의 열 수.Number of grid columns.
rowCountnumber
격자의 행 수.Number of grid rows.
startobject
{ column, row }
isPassableHandlerFunction
(column, row) => boolean

반환Returnsobject[] 방문한 칸들의 { column, row } 배열.Array of { column, row } of the visited cells.

짝Counterpart: vanilla-engine Grid.flood_fill

정적staticstatic isAllConnected(columnCount, rowCount, start, isPassableHandler, expectedCount)#0.3.0 부터since 0.3.0소스source

연결성 검사. (정적)

  • 시작 칸에서 도달할 수 있는 칸 수가 expectedCount 와 같은지 판정한다. (예: 조립형 게임에서 모든 부품이 코어와 붙어 있는지)

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)
columnCountnumber
격자의 열 수.Number of grid columns.
rowCountnumber
격자의 행 수.Number of grid rows.
startobject
{ column, row }
isPassableHandlerFunction
(column, row) => boolean
expectedCountnumber
이어져 있어야 할 칸 수. (시작 칸 포함)Number of cells that should be connected. (including the start cell)

반환Returnsboolean 시작 칸에서 닿는 칸 수가 expectedCount 와 같으면 참.True if the number of cells reachable from the start cell equals expectedCount.

짝Counterpart: vanilla-engine Grid.is_all_connected

정적staticstatic testRectOverlap(worldRect, tileSize, isBlockedHandler)#0.3.0 부터since 0.3.0소스source

사각형-타일 충돌 검사. (정적)

  • 월드 사각형이 걸치는 타일 범위를 순회하며 막힌 타일과 겹치는지 판정한다.
  • 이동 판정에서 "이동 후 위치가 장애물과 겹치면 취소" 용도로 쓴다.

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".
worldRectRect
검사할 월드 사각형.World rectangle to test.
tileSizenumber
타일 한 칸의 크기. (월드 단위)Size of one tile. (world units)
isBlockedHandlerFunction
(column, row) => boolean

반환Returnsboolean 막힌 타일과 겹치면 참.True if it overlaps a blocked tile.

짝Counterpart: vanilla-engine Grid.test_rect_overlap

예제Example이동할 자리가 벽과 겹치는지 보기Check whether the destination overlaps a wall
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));
정적staticstatic toCell(worldX, worldY, tileSize)#0.3.0 부터since 0.3.0소스source

월드 좌표 → 타일 좌표. (정적)

World coordinates → tile coordinates. (static)

worldXnumber
월드 X 좌표.World X coordinate.
worldYnumber
월드 Y 좌표.World Y coordinate.
tileSizenumber
타일 한 칸의 크기. (월드 단위)Size of one tile. (world units)

반환Returnsobject { column, row }

짝Counterpart: vanilla-engine Grid.to_cell

정적staticstatic toWorldCenter(column, row, tileSize)#0.3.0 부터since 0.3.0소스source

타일 좌표 → 타일 중심 월드 좌표. (정적)

Tile coordinates → world coordinates of the tile center. (static)

columnnumber
열 번호.Column index.
rownumber
행 번호.Row index.
tileSizenumber
타일 한 칸의 크기. (월드 단위)Size of one tile. (world units)

반환Returnsobject { x, y }

짝Counterpart: vanilla-engine Grid.to_world_center