Skip to content

Tile

const tile = require("@vectarine/tile")
tile.createGeneratedTilemap(
tilemapGenerator: (layer: number, x: number, y: number) -> { number },
): GeneratedTilemap

Create a tilemap resource from a function. The tilemap created is instantly loaded. The tilemapGenerator is a function that generates the part of the tilemap requested. For performance reasons, it should return a 256 list of numbers representing a 16x16 part of the tilemap (of the form result[1 + x + y * 16]). You can assume that x and y will always be multiples of 16 (but they can be negative). The tilemap is infinite in size. The tilemapGenerator is called as needed and its result is cached.

You can use this to merge tilemaps together by calling getTile on multiple tilemaps in the generator. You can proceduraly generate tilemaps using the noise function.

export type TilesetResource = ...

A tileset is a mapping from names to rectangular pieces of an image named tiles. You can draw a tile directly or using drawImagePart. Drawing multiple tiles from the same tileset is more efficient than drawing multiple images.

TilesetResource:getTileSize(): vec.Vec2

Get the size of a tile in pixels

TilesetResource:getTileRatio(): vec.Vec2

Get the number of tiles that fit in the full image, along each axis (the image size divided by the tile size). For example, if you have 8 tiles per row and 4 per column, this will return V2(8, 4).

TilesetResource:getTile(tileId: number): {

Get the position of a tile in the image. The position is returned as a ratio of the full image.

TilesetResource:getTiles(): { number }

Return a list of tile ids defined in the tileset provided.

export type TilemapResource = ...

A tilemap is a 3d grid of integers which can be used to represent levels. Tilemaps can be imported from .tmx files, generated on the fly or a combination of both. Tilemaps represents their data efficiently and can be infinite in size.

TilemapResource:getTile(layer: number, x: number, y: number): number

Returns the id of the tile at the location specified.

Returns 0 if the tilemap is not defined at the provided location, the layer does not exist or if the layer is not a tile layer.

TilemapResource:getObjectLayer(layer: number): ObjectLayer
TilemapResource:getTilemapPart(
layer: number,
lowx: number,
lowy: number,
highx: number,
highy: number,
accessFunction: (tileId: number, x: number, y: number) -> (),
)

A high-performance way to draw or access a part of a tilemap.

Call accessFunction for all tiles with coordinates (x,y) such that: lowx <= x <= highx and lowy <= y <= highy

If the layer does not exist or the coordinates are outside of where the tilemap is defined, the accessFunction might never be called.

export type GeneratedTilemap = ...

A generated tilemap is a tilemap that is generated on the fly using a function rather than loaded from a file. A generated tilemap is not a resource.

GeneratedTilemap:invalidate(layer: number, x: number, y: number)

Invalidate the cache of a part of the tilemap.

The next time this part is needed, the tilemapGenerator will be called again to generate it.

Because of how caching works, invalidating a location will also invalidate a 16x16 chunk which contains it. Invalidating a location that is already invalidated has no effect.

GeneratedTilemap:getTile(layer: number, x: number, y: number): number

Returns the id of the tile at the location specified.

Returns 0 if the tilemap is not defined at the provided location, the layer does not exist or if the layer is not a tile layer.

GeneratedTilemap:getTilemapPart(
layer: number,
lowx: number,
lowy: number,
highx: number,
highy: number,
accessFunction: (tileId: number, x: number, y: number) -> (),
)

A high-performance way to draw or access a part of a tilemap.

Call accessFunction for all tiles with coordinates (x,y) such that: lowx <= x <= highx and lowy <= y <= highy

If the layer does not exist or the coordinates are outside of where the tilemap is defined, the accessFunction might never be called.

export type Tilemap = ...
type ObjectLayer = ...