Class TileGrid

java.lang.Object
com.codename1.gaming.platformer.TileGrid

public final class TileGrid extends Object

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
    Constructor
    Description
    TileGrid(int cols, int rows, int tileSizePx)
    Creates an empty grid.
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    int
    col(int x)
    The column containing a fixed-point x.
    long
    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.
    int
    get(int c, int r)
    The kind of a cell, with the out-of-bounds rule in the class description.
    int
    The width in tiles.
    int
    The height of the level, fixed point.
    int
    The height in tiles.
    int
    The side of a tile, fixed point.
    int
    The width of the level, fixed point.
    boolean
    overlaps(int x, int y, int w, int h, int kind)
    Whether any cell a rectangle touches is of a kind.
    static TileGrid
    parse(String[] lines, int tileSizePx)
    Builds a grid from rows of level-file characters (see Tile).
    int
    row(int y)
    The row containing a fixed-point y.
    void
    set(int c, int r, int kind)
    Changes a cell, for breakable blocks and the level editor.
    The grid as level-file rows, the inverse of parse.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TileGrid

      public TileGrid(int cols, int rows, int tileSizePx)

      Creates an empty grid.

      Parameters
      • cols: the width in tiles
      • rows: the height in tiles
      • tileSizePx: 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: the rows, top first
      • tileSizePx: 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 column
      • r: 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: the column
      • r: the row
      • kind: 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: 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 point
      • y: top, fixed point
      • w: width, fixed point
      • h: height, fixed point
      • kind: 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: left, fixed point
      • y: top, fixed point
      • w: width, fixed point
      • h: 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