Tilemap Documentation
Tilemap Methods
b8.Tilemap.addWallTileSet(name, tileIds)
Add a wall tile set to the tilemap system. The tile set should be an array of 16 tile ids, corresponding to the 16 possible bitmask values.
Parameters
- name (string): The name of the wall tile set. This should match the name used in the tile pattern (e.g. “wall:brick”).
- tileIds (Array): The array of 16 tile ids for the wall tile set.
b8.Tilemap.convertFromText(mapText)
Get a text map and convert it to an array of arrays.
An example text map might look like: ####### # 1 # # ### # # 2 2 # #######
The tilemap array will include the tile character code, foreground color, background color, collision flag, and additional data.
Parameters
- mapText (string): The text map to convert.
Returns
- (Array): The converted tilemap array.
b8.Tilemap.createEmptyTilemap(width, height)
Create an empty tilemap array of the specified size.
Parameters
- width (number): The width of the tilemap.
- height (number): The height of the tilemap.
Returns
- (Array): The empty tilemap array.
b8.Tilemap.createFromArray(grid, tilePattern [,defaultTilePattern])
Create a tilemap from an array of arrays. The tilePattern is an object that maps tile characters to tile properties.
Parameters
- grid (Array): The grid array to create the tilemap from.
- tilePattern (Object): The tile pattern object.
- [defaultTilePattern] (Object): The default tile pattern to use for empty tiles. (default: null)
Returns
- (Array): The created tilemap array.
b8.Tilemap.draw(tilemap [,tileStartCol] [,tileStartRow] [,width] [,height])
Draw a tilemap array to the screen.
Parameters
- tilemap (Array): The tilemap array to draw.
- [tileStartCol] (number): Tile column to start drawing from. (default: 0)
- [tileStartRow] (number): Tile row to start drawing from. (default: 0)
- [width] (number): The width of the tilemap to draw. (default: null)
- [height] (number): The height of the tilemap to draw. (default: null)
b8.Tilemap.getDefaultTile()
Get the default tile for a tilemap.
Returns
- (Array): The default tile.
b8.Tilemap.getWallTileSet(name)
Get a wall tile set by name.
Parameters
- name (string): The name of the wall tile set to get.
Returns
- (Array): The wall tile set array.
b8.Tilemap.load(data)
Load a tilemap array from a string.
Parameters
- data (string): The encoded string
Returns
- (Array): The tilemap array
b8.Tilemap.normaliseTile(tile)
Convert a tile definition to a tile array.
The tile definition can be an array or an object with properties. This function ensures that the tile array is in the correct format and all properties are set.
Parameters
- tile (Array|Object): The tile definition to convert. t.t = tile id, t.fg = foreground color, t.bg = background color, t.coll = collision flag, t.data = additional data.
Returns
- (Array): The converted tile array.
b8.Tilemap.resize(tilemap, width, height)
Resize a tilemap array to the specified width and height.
Parameters
- tilemap (Array): The tilemap array to resize.
- width (number): The new width of the tilemap.
- height (number): The new height of the tilemap.
Returns
- (Array): The resized tilemap array.
b8.Tilemap.save(tilemap)
Convert a tilemap array to a string.
This string can be loaded with b8.Tilemap.load.
Parameters
- tilemap (Array): The tilemap array to save.
Returns
- (string): The encoded string
b8.Tilemap.setTile(tilemap, col, row, tile)
Set a tile in the tilemap array at the specified coordinates. This function modifies the tilemap array in place.
Parameters
- tilemap (Array): The tilemap array to modify.
- col (number): The x coordinate of the tile to set.
- row (number): The y coordinate of the tile to set.
- tile (Array): The tile data to set at the specified coordinates.
b8.Tilemap.shift(tilemap, dx, dy)
Shift and wrap a tilemap array by the specified amount.
Parameters
- tilemap (Array): The tilemap array to shift.
- dx (number): The amount to shift the tilemap in the x direction.
- dy (number): The amount to shift the tilemap in the y direction.
b8.Tilemap.validateTilemap(mapText)
Check the validity of the encoded tilemap data.
The checks are simple, but reduces liklihood of invalid data being used.
Parameters
- mapText (string): The encoded tilemap data.
Returns
- (boolean): True if the tilemap is valid, false otherwise.
b8.Tilemap.wallTile(x, y, grid, name)
Select a wall tile from a predefined list based on the surrounding tiles. The grid is the 2D array of tile ids. The x and y are the coordinates of the tile to check.
Parameters
- x (number): The x coordinate of the tile.
- y (number): The y coordinate of the tile.
- grid (Array): The 2D array of tile ids.
- name (string): The name of the wall tile to select. Picked from the default lists of wall patterns.
Returns
- (number): The selected wall tile id.