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

  1. Object
  2. 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

cols int
the width in tiles
rows int
the height in tiles
tileSizePx int
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

lines String[]
the rows, top first
tileSizePx int
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 int
the column
r int
the row

Returns

a Tile kind

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 int
the column
r int
the row
kind int
the new Tile kind

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 int
a fixed-point x

Returns

the column index

row

public int row(int y)
The row containing a fixed-point y.

Parameters

y int
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 int
left, fixed point
y int
top, fixed point
w int
width, fixed point
h int
height, fixed point
kind int
the Tile kind

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 int
left, fixed point
y int
top, fixed point
w int
width, fixed point
h int
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