gameify. Tilemap

Class representing a Tilemap of rectangular tiles

Constructor

new Tilemap(twidth, theight, offsetxopt, offsetyopt)

Parameters:
NameTypeAttributesDefaultDescription
twidthNumber

The width of the tiles

theightNumber

The height of the tiles

offsetxNumber<optional>
0

X offset of the tiles

offsetyNumber<optional>
0

Y offset of the tiles

Example
// ...
// make a new tileset with 8x8 pixels
let forestTileset = new gameify.Tileset("images/forest.png", 8, 8);
// make a new tilemap with 16x16 tiles and no offset
// anything that's not 16x16 will be scaled to match
let forestMap = new gameify.Tilemap(16, 16, 0, 0);
forsetMap.setTileset(forestTileset);

// make sure to add it to the screen
myScreen.add(tileset);

forestScene.onDraw(() => {
    // ...
    // Draw the tilemap
    forsetMap.draw();
});

Members

context

The Canvas context to draw to

offset :gameify.Vector2d

The tile offset (coordinates of tile <0, 0>). Used to translate the map

parent

The parent screen (not used directly)

Methods

clear()

Clear the tilemap. Removes all placed tiles and cached images

downloadMapData()

Download a .tilemapdata.js file of the tilemap. This file can be imported as an es6 module

Examples
myMap.downloadMapData();
import myMapData from './mymap.tilemapdata.js';
myMap.loadMapData(myMapData);

draw(checkopt)

Draw the tilemap to the screen

Parameters:
NameTypeAttributesDescription
checkfunction<optional>

A function to check if the tile should be drawn (calls check(tile, x, y))

enableMapBuilder(screen)

Deprecated. Use the engine editor to build your tilemaps. This editor is no longer maintained.
Enable the map builder tool. This allows you to easily edit tilesets.
Controls are: Click to place, Right-click to delete, Middle-click to pick, Scroll and Ctrl+Scroll to switch tile, Shift+Scroll to rotate the tile.
Once you're finished, call tilemap.exportMapData() to export the map.

Parameters:
NameTypeDescription
screengameify.Screen

The screen to show the map builder on. For best results, use the one you've already added it to.

Deprecated
  • Use the engine editor to build and export your tilemaps. This editor is no longer maintained.

exportMapData() → {object}

Export this tilemap's map data and layout (load with loadMapData)

Returns:

The map data as JSON

Type: 
object

fromJSON(data, ref) → {gameify.Tilemap}

Creates a Tilemap from JSON data

Parameters:
NameTypeDescription
dataObject | Array

Serialized Tilemap data (from Tilemap.toJSON)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
gameify.Tilemap

get(x, y) → {gameify.Tile}

Get the tile (if it exists) placed at a certain position

Parameters:
NameTypeDescription
xNumber

X coordinate of the tile

yNumber

Y coordinate of the tile

Returns:
Type: 
gameify.Tile

getParent() → {gameify.Screen}

Get the screen this sprite draws

Returns:
Type: 
gameify.Screen

getTilesNear(x, y, distance) → {Array.<gameify.Tile>}

Get all tiles within x tiles (square, not radius) of the given tile

Parameters:
NameTypeDescription
xNumber

X coordinate of the center tile

yNumber

Y coordinate of the center tile

distanceNumber

radius of the square of tiles

Returns:
Type: 
Array.<gameify.Tile>

getTileset() → {gameify.Tileset}

Get the tilemap's tileset

Returns:

The tileset

Type: 
gameify.Tileset

listTiles() → {Array.<gameify.Tile>}

Get an array of all the tiles in the map

Returns:
Type: 
Array.<gameify.Tile>

loadMapData(data)

Load saved map data (export using exportMapData)

Parameters:
NameTypeDescription
dataObject

The map data to load

mapToWorld(mapx, mapyopt) → {Object}

Convert map coordinates to world coordinates

Parameters:
NameTypeAttributesDescription
mapxNumber

The map x coordinate

mapyNumber<optional>

The map y coordinate

Returns:

{gameify.Vector2d} A vector representing the calculated position

Type: 
Object

mapToWorld(position) → {gameify.Vector2d}

Convert map coordinates to world coordinates

Parameters:
NameTypeDescription
positionObject | gameify.Vector2d

A vector OR an object containing both x any y coordinates

Returns:

A vector representing the calculated position

Type: 
gameify.Vector2d

onDraw(callback)

Set the draw function for this tilemap

Parameters:
NameTypeDescription
callbackfunction

The function to be called right before the tilemap is drawn

place(originx, originy, destx, desty, rotationopt, widthopt, heightopt)

Place a tile on the tilemap

Parameters:
NameTypeAttributesDefaultDescription
originxNumber

The x position of the tile on the tilesheet

originyNumber

The y position of the tile on the tilesheet

destxNumber

The x position to place the tile

destyNumber

The y position to place the tile

rotationNumber<optional>
0

Tile rotation, in degrees

widthNumber<optional>
1

Tile width, relative to single tile width

heightNumber<optional>
1

Tile height, relative to single tile height

refreshCachedImages()

Clear cached images (use after updating a tileset). Clears all placed tiles, and re-adds them from the current tileset. This operation can be slow and is intended for infrequent changes (so don't run it every frame).

remove(x, y)

Remove a tile from the tilemap

Parameters:
NameTypeDescription
xNumber

The x coord of the tile to remove

yNumber

The y coord of the tile to remove

remove(tile)

Remove a tile from the tilemap

Parameters:
NameTypeDescription
tilegameify.Tile

The tile to remove

remove(position)

Remove a tile from the tilemap

Parameters:
NameTypeDescription
positiongameify.Vector2d

The position (map coordinate) of the tile to remove

setContext()

Set the Canvas context to draw to. Should be called by a screen when the Tilemap is added to it.

setTileset(set)

What tileset to use. This tileset must include anything you want to use in this tilemap.

Parameters:
NameTypeDescription
setgameify.Tileset

The tileset

toJSON(keyopt, ref) → {Object}

Convert the Tilemap to JSON

Parameters:
NameTypeAttributesDescription
keystring<optional>

Key object is stored under (unused, here for consistency with e.g. Date.toJSON, etc.)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
Object

worldToMap(screenx, screenyopt) → {gameify.Vector2d}

Convert world coordinates to map coordinates

Parameters:
NameTypeAttributesDescription
screenxNumber

The screen x coordinate

screenyNumber<optional>

The screen y coordinate

Returns:

A vector representing the calculated position

Type: 
gameify.Vector2d

worldToMap(position) → {gameify.Vector2d}

Convert world coordinates to map coordinates

Parameters:
NameTypeDescription
positionObject | gameify.Vector2d

A vector OR an object containing both x any y coordinates

Returns:

A vector representing the calculated position

Type: 
gameify.Vector2d

(static) mapToWorld()

Converts map coordinates to world coordinates. Use mapToWorld instead

Deprecated
  • Yes

(static) worldToMap()

Convert world coordinates to map coordinates. Use worldToMap instead

Deprecated
  • Yes