Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class TileGrid
- Object
- TileGrid
The collision map of a level: a grid of Tile kinds.
Cells outside the grid read as Tile#SOLID to the left and right, so nothing
walks off the side of a level, and as Tile#EMPTY above and below, so a
actor can jump above the top row and falls out of the bottom one (which a
PlatformWorld treats as a death).
Constructors
public TileGrid(int cols, int rows, int tileSizePx) | Creates an empty grid. |
Methods
public static TileGrid parse(String[] lines, int tileSizePx) | Builds a grid from rows of level-file characters (see Tile). |
public int getCols() | The width in tiles. |
public int getRows() | The height in tiles. |
public int getTileSize() | The side of a tile, fixed point. |
public int getWidth() | The width of the level, fixed point. |
public int getHeight() | The height of the level, fixed point. |
public int get(int c, int r) | The kind of a cell, with the out-of-bounds rule in the class description. |
public void set(int c, int r, int kind) | Changes a cell, for breakable blocks and the level editor. |
public long editsHash() | A digest of every change made with set, in order, so a replay’s hash covers broken blocks without walking the whole grid each time. |
public int col(int x) | The column containing a fixed-point x. |
public int row(int y) | The row containing a fixed-point y. |
public boolean overlaps(int x, int y, int w, int h, int kind) | Whether any cell a rectangle touches is of a kind. |
public boolean blocked(int x, int y, int w, int h) | Whether a rectangle overlaps any solid cell, as a crouching actor asks before it stands up. |
public String[] toLines() | The grid as level-file rows, the inverse of parse. |
Inherited methods
Constructor details
TileGrid
public TileGrid(int cols, int rows, int tileSizePx)Creates an empty grid.
Parameters
colsint- the width in tiles
rowsint- the height in tiles
tileSizePxint- the side of a tile in pixels, a whole number
Method details
parse
public static TileGrid parse(String[] lines, int tileSizePx)Builds a grid from rows of level-file characters (see
Tile). Short rows are
padded with empty cells.Parameters
linesString[]- the rows, top first
tileSizePxint- the side of a tile in pixels
Returns
the grid
Throws
IllegalArgumentException- on an actor that is not a tile
getCols
public int getCols()The width in tiles.
Returns
the column count
getRows
public int getRows()The height in tiles.
Returns
the row count
getTileSize
public int getTileSize()The side of a tile, fixed point.
Returns
the tile size
getWidth
public int getWidth()The width of the level, fixed point.
Returns
the pixel width scaled by
Fx#ONEgetHeight
public int getHeight()The height of the level, fixed point.
Returns
the pixel height scaled by
Fx#ONEget
public int get(int c, int r)The kind of a cell, with the out-of-bounds rule in the class description.
Parameters
cint- the column
rint- the row
Returns
a
Tile kindset
public void set(int c, int r, int kind)Changes a cell, for breakable blocks and the level editor. Cells outside the
grid are ignored.
Parameters
cint- the column
rint- the row
kindint- the new
Tilekind
editsHash
public long editsHash()A digest of every change made with
set, in order, so a replay’s hash
covers broken blocks without walking the whole grid each time.Returns
the digest; 0 while the grid is as it was loaded
col
public int col(int x)The column containing a fixed-point x.
Parameters
xint- a fixed-point x
Returns
the column index
row
public int row(int y)The row containing a fixed-point y.
Parameters
yint- a fixed-point y
Returns
the row index
overlaps
public boolean overlaps(int x, int y, int w, int h, int kind)Whether any cell a rectangle touches is of a kind.
Parameters
xint- left, fixed point
yint- top, fixed point
wint- width, fixed point
hint- height, fixed point
kindint- the
Tilekind
Returns
true if one of the touched cells is
kindblocked
public boolean blocked(int x, int y, int w, int h)Whether a rectangle overlaps any solid cell, as a crouching actor asks
before it stands up.
Parameters
xint- left, fixed point
yint- top, fixed point
wint- width, fixed point
hint- height, fixed point
Returns
true if the rectangle is blocked
toLines
public String[] toLines()The grid as level-file rows, the inverse of
parse.Returns
one string per row, top first