Class 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).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanblocked(int x, int y, int w, int h) Whether a rectangle overlaps any solid cell, as a crouching actor asks before it stands up.intcol(int x) The column containing a fixed-point x.longA digest of every change made withset, in order, so a replay's hash covers broken blocks without walking the whole grid each time.intget(int c, int r) The kind of a cell, with the out-of-bounds rule in the class description.intgetCols()The width in tiles.intThe height of the level, fixed point.intgetRows()The height in tiles.intThe side of a tile, fixed point.intgetWidth()The width of the level, fixed point.booleanoverlaps(int x, int y, int w, int h, int kind) Whether any cell a rectangle touches is of a kind.static TileGridBuilds a grid from rows of level-file characters (seeTile).introw(int y) The row containing a fixed-point y.voidset(int c, int r, int kind) Changes a cell, for breakable blocks and the level editor.String[]toLines()The grid as level-file rows, the inverse ofparse.
-
Constructor Details
-
TileGrid
public TileGrid(int cols, int rows, int tileSizePx) Creates an empty grid.
Parameters
cols: the width in tilesrows: the height in tilestileSizePx: the side of a tile in pixels, a whole number
-
-
Method Details
-
parse
Builds a grid from rows of level-file characters (see
Tile). Short rows are padded with empty cells.Parameters
lines: the rows, top firsttileSizePx: 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#ONE -
getHeight
public int getHeight()The height of the level, fixed point.
Returns
the pixel height scaled by
Fx#ONE -
get
public int get(int c, int r) The kind of a cell, with the out-of-bounds rule in the class description.
Parameters
c: the columnr: the row
Returns
a
Tilekind -
set
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
c: the columnr: the rowkind: the newTilekind
-
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
x: a fixed-point x
Returns
the column index
-
row
public int row(int y) The row containing a fixed-point y.
Parameters
y: 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
x: left, fixed pointy: top, fixed pointw: width, fixed pointh: height, fixed pointkind: theTilekind
Returns
true if one of the touched cells is
kind -
blocked
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
x: left, fixed pointy: top, fixed pointw: width, fixed pointh: height, fixed point
Returns
true if the rectangle is blocked
-
toLines
The grid as level-file rows, the inverse of
parse.Returns
one string per row, top first
-