Fork me on GitHub
View source

package com.haxepunk.masks

class Grid


extends Hitbox

Uses a hash grid to determine collision, faster than using hundreds of Entities for tiled levels, etc.

Constructor

View source

new (width : Int, height : Int, tileWidth : Int, tileHeight : Int, x : Int = 0, y : Int = 0) : Void

Constructor. The actual size of the grid is determined by dividing the width/height by tileWidth/tileHeight, and stored in the properties columns/rows.


Parameters:
width   

Width of the grid, in pixels.

height   

Height of the grid, in pixels.

tileWidth   

Width of a grid tile, in pixels.

tileHeight   

Height of a grid tile, in pixels.

x   

X offset of the grid.

y   

Y offset of the grid.

Variables

read only columns : Int

How many columns the grid has

read only data : Array<Array<Bool>>

The grid data.

read only rows : Int

How many rows the grid has.

read only tileHeight : Int

The tile height.

read only tileWidth : Int

The tile width.

usePositions : Bool

If x/y positions should be used instead of columns/rows (the default). Columns/rows means screen coordinates relative to the width/height specified in the constructor. X/y means grid coordinates, relative to the grid size.

Functions

View source

clearRect (column : Int = 0, row : Int = 0, width : Int = 1, height : Int = 1) : Void

Makes the rectangular region of tiles non-solid.


Parameters:
column   

First column.

row   

First row.

width   

Columns to fill.

height   

Rows to fill.

View source

clearTile (column : Int = 0, row : Int = 0) : Void

Makes the tile non-solid.


Parameters:
column   

Tile column.

row   

Tile row.

View source

clone () : Grid

Make a copy of the grid.


Returns:

Return a copy of the grid.

View source

getTile (column : Int = 0, row : Int = 0) : Bool

Gets the value of a tile.


Parameters:
column   

Tile column.

row   

Tile row.


Returns:

tile value.

View source

loadFrom2DArray (array : Array<Array<Int>>) : Void

Loads the grid data from an array.


Parameters:
array   

The array data, which is a set of tile values (0 or 1)

View source

loadFromString (str : String, columnSep : String = ",", rowSep : String = "\n") : Void

Loads the grid data from a string.


Parameters:
str   

The string data, which is a set of tile values (0 or 1) separated by the columnSep and rowSep strings.

columnSep   

The string that separates each tile value on a row, default is ",".

rowSep   

The string that separates each row of tiles, default is "\n".

View source

saveToString (columnSep : String = ",", rowSep : String = "\n", solid : String = "true", empty : String = "false") : String

Saves the grid data to a string.


Parameters:
columnSep   

The string that separates each tile value on a row, default is ",".

rowSep   

The string that separates each row of tiles, default is "\n".


Returns:

The string version of the grid.

View source

setRect (column : Int = 0, row : Int = 0, width : Int = 1, height : Int = 1, solid : Bool = true) : Void

Sets the value of a rectangle region of tiles.


Parameters:
column   

First column.

row   

First row.

width   

Columns to fill.

height   

Rows to fill.

solid   

Value to fill.

View source

setTile (column : Int = 0, row : Int = 0, solid : Bool = true) : Void

Sets the value of the tile.


Parameters:
column   

Tile column.

row   

Tile row.

solid   

If the tile should be solid.