Constructor
new Tilemap(twidth, theight, offsetxopt, offsetyopt)
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
twidth | Number | The width of the tiles | ||
theight | Number | The height of the tiles | ||
offsetx | Number | <optional> | 0 | X offset of the tiles |
offsety | Number | <optional> | 0 | Y offset of the tiles |
- Source
// ...
// 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
- Source
offset :gameify.Vector2d
The tile offset (coordinates of tile <0, 0>). Used to translate the map
- Source
parent
The parent screen (not used directly)
- Source
Methods
clear()
Clear the tilemap. Removes all placed tiles and cached images
- Source
downloadMapData()
Download a .tilemapdata.js file of the tilemap. This file can be imported as an es6 module
- Source
myMap.downloadMapData();import myMapData from './mymap.tilemapdata.js';
myMap.loadMapData(myMapData);draw(checkopt)
Draw the tilemap to the screen
| Name | Type | Attributes | Description |
|---|---|---|---|
check | function | <optional> | A function to check if the tile should be drawn (calls check(tile, x, y)) |
- Source
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.
| Name | Type | Description |
|---|---|---|
screen | gameify. | 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.
- Source
exportMapData() → {object}
Export this tilemap's map data and layout (load with loadMapData)
- Source
The map data as JSON
- Type:
- object
fromJSON(data, ref) → {gameify.Tilemap}
Creates a Tilemap from JSON data
| Name | Type | Description |
|---|---|---|
data | Object | | Serialized Tilemap data (from Tilemap.toJSON) |
ref | function | A function that returns a name for other objects, so they can be restored later |
- Source
- Type:
- gameify.
Tilemap
get(x, y) → {gameify.Tile}
Get the tile (if it exists) placed at a certain position
| Name | Type | Description |
|---|---|---|
x | Number | X coordinate of the tile |
y | Number | Y coordinate of the tile |
- Source
- Type:
- gameify.
Tile
getParent() → {gameify.Screen}
Get the screen this sprite draws
- Source
- Type:
- gameify.
Screen
getTilesNear(x, y, distance) → {Array.<gameify.Tile>}
Get all tiles within x tiles (square, not radius) of the given tile
| Name | Type | Description |
|---|---|---|
x | Number | X coordinate of the center tile |
y | Number | Y coordinate of the center tile |
distance | Number | radius of the square of tiles |
- Source
- Type:
- Array.<gameify.Tile>
getTileset() → {gameify.Tileset}
Get the tilemap's tileset
- Source
The tileset
- Type:
- gameify.
Tileset
listTiles() → {Array.<gameify.Tile>}
Get an array of all the tiles in the map
- Source
- Type:
- Array.<gameify.Tile>
loadMapData(data)
Load saved map data (export using exportMapData)
| Name | Type | Description |
|---|---|---|
data | Object | The map data to load |
- Source
mapToWorld(mapx, mapyopt) → {Object}
Convert map coordinates to world coordinates
| Name | Type | Attributes | Description |
|---|---|---|---|
mapx | Number | The map x coordinate | |
mapy | Number | <optional> | The map y coordinate |
- Source
{gameify.Vector2d} A vector representing the calculated position
- Type:
- Object
mapToWorld(position) → {gameify.Vector2d}
Convert map coordinates to world coordinates
| Name | Type | Description |
|---|---|---|
position | Object | | A vector OR an object containing both x any y coordinates |
- Source
A vector representing the calculated position
- Type:
- gameify.
Vector2d
onDraw(callback)
Set the draw function for this tilemap
| Name | Type | Description |
|---|---|---|
callback | function | The function to be called right before the tilemap is drawn |
- Source
place(originx, originy, destx, desty, rotationopt, widthopt, heightopt)
Place a tile on the tilemap
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
originx | Number | The x position of the tile on the tilesheet | ||
originy | Number | The y position of the tile on the tilesheet | ||
destx | Number | The x position to place the tile | ||
desty | Number | The y position to place the tile | ||
rotation | Number | <optional> | 0 | Tile rotation, in degrees |
width | Number | <optional> | 1 | Tile width, relative to single tile width |
height | Number | <optional> | 1 | Tile height, relative to single tile height |
- Source
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).
- Source
remove(x, y)
Remove a tile from the tilemap
| Name | Type | Description |
|---|---|---|
x | Number | The x coord of the tile to remove |
y | Number | The y coord of the tile to remove |
- Source
remove(tile)
Remove a tile from the tilemap
| Name | Type | Description |
|---|---|---|
tile | gameify. | The tile to remove |
- Source
remove(position)
Remove a tile from the tilemap
| Name | Type | Description |
|---|---|---|
position | gameify. | The position (map coordinate) of the tile to remove |
- Source
setContext()
Set the Canvas context to draw to. Should be called by a screen when the Tilemap is added to it.
- Source
setTileset(set)
What tileset to use. This tileset must include anything you want to use in this tilemap.
| Name | Type | Description |
|---|---|---|
set | gameify. | The tileset |
- Source
toJSON(keyopt, ref) → {Object}
Convert the Tilemap to JSON
| Name | Type | Attributes | Description |
|---|---|---|---|
key | string | <optional> | Key object is stored under (unused, here for consistency with e.g. Date.toJSON, etc.) |
ref | function | A function that returns a name for other objects, so they can be restored later |
- Source
- Type:
- Object
worldToMap(screenx, screenyopt) → {gameify.Vector2d}
Convert world coordinates to map coordinates
| Name | Type | Attributes | Description |
|---|---|---|---|
screenx | Number | The screen x coordinate | |
screeny | Number | <optional> | The screen y coordinate |
- Source
A vector representing the calculated position
- Type:
- gameify.
Vector2d
worldToMap(position) → {gameify.Vector2d}
Convert world coordinates to map coordinates
| Name | Type | Description |
|---|---|---|
position | Object | | A vector OR an object containing both x any y coordinates |
- Source
A vector representing the calculated position
- Type:
- gameify.
Vector2d
(static) mapToWorld()
Converts map coordinates to world coordinates. Use mapToWorld instead
- Deprecated
- Yes
- Source
(static) worldToMap()
Convert world coordinates to map coordinates. Use worldToMap instead
- Deprecated
- Yes
- Source