SolidGrid

class SolidGrid(val columns: Int, val rows: Int, val tileSize: Float)(source)

A level's solids: at most one per cell of a grid of columns by rows cells, each tileSize wide, plus moving ones. Set it as PhysicsWorld.solids and every particle is pushed out of it: a point particle can only be inside the one tile it is in, so only that tile and the moving solids are checked; a round one checks the tiles its radius reaches.

toString gives its size, for example SolidGrid(40x30, tileSize=16.0, moving=1).

Throws

if columns or rows is not more than 0, or tileSize is not more than 0.

Samples

// A level 40 tiles across and 20 down, each 16 units: a floor, and a slope up to a ledge.
val grid = SolidGrid(columns = 40, rows = 20, tileSize = 16f)
for (column in 0 until 40) grid.fill(column, 19)
grid.fill(10, 18, shape = SolidShape.RISING)
grid.fill(11, 18)
val world = PhysicsWorld()
world.solids = grid
assertPrints(grid.isSolidAt(8f, 310f), "true")
assertPrints(grid.atOrNull(11, 18)?.shape, "BOX")
assertPrints(grid.atOrNull(12, 18), "null")

Constructors

Link copied to clipboard
constructor(columns: Int, rows: Int, tileSize: Float)

Properties

Link copied to clipboard

Number of cells across.

Link copied to clipboard

Solids that are not tied to a cell, such as a crusher, in the order they were added. Checked every time.

Link copied to clipboard
val rows: Int

Number of cells down.

Link copied to clipboard

Width and height of one cell, in world units.

Functions

Link copied to clipboard
fun add(solid: Solid)

Adds solid, made with Solid.moving, to the solids that move. Adding one already there does nothing.

Link copied to clipboard
fun atOrNull(column: Int, row: Int): Solid?

Returns the solid in cell (column, row), or null for air and for cells outside the grid.

Link copied to clipboard
fun fill(column: Int, row: Int, shape: SolidShape = SolidShape.BOX, tags: SolidTags = SolidTags.NONE, material: Material = Material.DEFAULT): Solid

Fills cell (column, row) with a solid of shape carrying tags and material, and returns it. A solid already in the cell is replaced.

Link copied to clipboard
fun fillTopHalf(column: Int, row: Int, tags: SolidTags = SolidTags.NONE, material: Material = Material.DEFAULT): Solid

Fills only the top half of cell (column, row), such as a stub hanging from a ceiling, and returns the solid.

Link copied to clipboard
fun isSolidAt(x: Float, y: Float, margin: Float = 0.0f): Boolean

Returns true if the point (x, y) is inside any solid by more than margin units; see Solid.contains.

Link copied to clipboard
fun remove(solid: Solid)

Removes solid from the solids that move. Removing one that is not there does nothing.

Link copied to clipboard
fun removeAt(column: Int, row: Int)

Empties cell (column, row). Emptying a cell that is air or outside the grid does nothing.

Link copied to clipboard
open override fun toString(): String