@tastic/grid
v0.1.3
Published
Pure coordinate math and BFS reachability for tile/board-based games: bounds checks, cell/pixel conversion, adjacency, and flood-fill reachability queries
Maintainers
Readme
@tastic/grid
Pure coordinate math and BFS reachability for tile/board-based games: bounds checks, cell/pixel conversion, adjacency, and flood-fill reachability queries. No React or React Native dependency in src/ itself.
Part of the @tastic package ecosystem (the game-focused corner of the @rific/InfiniteToken fleet). Re-exports Direction/flipDirection/isOppositeDirection from @tastic/input rather than redefining them, so every fleet game shares one direction vocabulary.
Install
npm install @tastic/grid @tastic/inputUsage
import { cellKey, computeGridSize, distanceToNearestTarget, isInBounds, stepCell } from '@tastic/grid'
const grid = computeGridSize(320, 320, 32) // { cols: 10, rows: 10 }
const occupied = new Set(['3,3', '3,4', '3,5'])
const next = stepCell({ x: 2, y: 4 }, 'right')
if (isInBounds(next, grid) && !occupied.has(cellKey(next))) {
// move is legal
}
// Is an enemy at (0, 0) able to reach the player at (5, 5) at all, given the current walls/blocks?
const reachable = distanceToNearestTarget({ x: 0, y: 0 }, grid, occupied, new Set([cellKey({ x: 5, y: 5 })]))
const isTrapped = reachable === nullAPI
GridCell,GridSize- plain coordinate/dimension shapesALL_DIRECTIONS: the 4 cardinal directions, fixed ordercomputeGridSize(width, height, cellPx): how many whole cells fit a pixel areacellToPixel(cell, cellPx)/pixelToCell(point, cellPx)- inverse conversions between grid and pixel spacecellKey(cell): stable string key forSet/Mapoccupancy trackingisInBounds(cell, grid): bounds checkwrapCell(cell, grid): re-enters an off-grid cell from the opposite edge, for wrap-around/torus topologystepCell(cell, direction): one step in a cardinal directionisAdjacent(a, b): true when two cells are exactly one step apartcountReachableCells(start, grid, occupied, maxCount?): BFS: how many cells are reachable fromstartwithout crossingoccupiedcells or the grid edgedistanceToNearestTarget(start, grid, occupied, targets, maxCount?): BFS: shortest step-distance fromstartto any cell intargets, ornullif none is reachable - the shape a "can this entity still reach that cell" / trapped check needsDirection,flipDirection,isOppositeDirection- re-exported from@tastic/input
License
MIT
