CharGrid
A level written as rows of characters, one per cell: what each character means is up to the game. Finds cells, groups and runs of characters, maps cells to world positions on the ground (x/z), and builds a BGE.SolidWorld from the solid ones. Row 0 is -z, column 0 is -x.
Subclass it to keep a level's own findings (start cell, exits) with the grid, or override isSolid() for solids that aren't a fixed set of characters.
Properties
colCount(integer)rowCount(integer)cellSize(float)origin(BGE.Math.Vector) — The -x/-z corner of cell (0, 0)solidKinds(string)grid(Array.<Array.<string>>) — Each row's characters, [row][col]; rows shorter than colCount are padded with ""
Constructor
new CharGrid(
rows: Array.<string>,
args: BGE.CharGridArgs,
): CharGridParameters
rows(Array.<string>)args(BGE.CharGridArgs)
Instance Methods
isInside(col: integer, row: integer): boolean
Whether (col, row) is on the grid.
Parameters
col(integer)row(integer)
Returns
boolean
kindAt(col: integer, row: integer): string
The character at (col, row), "" outside the grid.
Parameters
col(integer)row(integer)
Returns
string
isKindOf(col: integer, row: integer, kinds: string): boolean
Whether the character at (col, row) is one of kinds (case-sensitive). False outside the grid.
Parameters
col(integer)row(integer)kinds(string) — characters to match, e.g. "=-"
Returns
boolean
isSolid(col: integer, row: integer): boolean
Whether a cell blocks: one of solidKinds, or outside the grid. Override for other rules.
Parameters
col(integer)row(integer)
Returns
boolean
cellCenter(col: float, row: float): BGE.Math.Vector
World position of a cell's centre on the ground (y 0). Takes fractions, e.g. the middle of a group.
Parameters
col(float)row(float)
Returns
BGE.Math.Vector
groupCenter(group: BGE.CharGridGroup): BGE.Math.Vector
World position of the middle of a group of cells (y 0).
Parameters
group(BGE.CharGridGroup)
Returns
BGE.Math.Vector
cellAt(x: float, z: float): BGE.CharGridCell
The cell a world position is in (may be outside the grid, with kind "").
Parameters
x(float)z(float)
Returns
cellsOf(kinds: string): Array.<BGE.CharGridCell>
Every cell whose character is one of kinds, row by row.
Parameters
kinds(string)
Returns
Array.<BGE.CharGridCell>
cellsExcept(kinds: string): Array.<BGE.CharGridCell>
Every cell whose character isn't one of kinds, row by row, e.g. every floor cell with cellsExcept("#").
Parameters
kinds(string)
Returns
Array.<BGE.CharGridCell>
wallSides(col: integer, row: integer): Array.<string>
Sides of a cell (BGE.GridSides, in the order n, e, s, w) with a solid neighbour.
Parameters
col(integer)row(integer)
Returns
Array.<string>
openSides(col: integer, row: integer): Array.<string>
Sides of a cell (in the order n, e, s, w) with an open neighbour.
Parameters
col(integer)row(integer)
Returns
Array.<string>
findRectGroups(kinds: string): Array.<BGE.CharGridGroup>
Rectangles of cells of one kind each, for things drawn as blocks of characters (a table drawn === over two rows). Row by row: each new cell starts a group as wide as the run of its kind to the right, grown down while whole rows below match.
Parameters
kinds(string) — characters to group
Returns
Array.<BGE.CharGridGroup>
findConnectedGroups(kinds: string): Array.<BGE.CharGridGroup>
Each connected block (4-way) of one kind, as its bounding rectangle, in the order their first cells come row by row. For blocks that aren't rectangles, e.g. a building's outline.
Parameters
kinds(string) — characters to group
Returns
Array.<BGE.CharGridGroup>
boundsOf(kinds: string): BGE.CharGridGroup
The rectangle round every cell whose character is one of kinds, wherever they are; its kind is the first such cell's. Invalid when there are none.
Parameters
kinds(string)
Returns
findRuns(
kinds: string,
minLength?: integer,
): Array.<BGE.CharGridGroup>
Straight runs of one kind at least minLength long: along each row (rows 1 high), then down each column (cols 1 wide). A cell where runs cross, e.g. a fence's corner, is in both.
Parameters
kinds(string) — characters that make runsminLength(integer, optional, default: 2) — shortest run to keep
Returns
Array.<BGE.CharGridGroup>
toSolidWorld(kinds?: string): BGE.SolidWorld
A BGE.SolidWorld (its y is the world's z) with a cell-sized solid for each solid cell, row by row, and a wall one cell thick round the grid.
Parameters
kinds(string, optional, default: "\"\"") — characters to make solid; "" (the default) uses isSolid()
Returns