Skip to content

TileLayer ​

Batched, culled tile grids.

ts
import { TileLayer, TilemapViewport, TileQuery, GidInfo } from "willow";

TileLayer ​

ts
class TileLayer extends Node

Extends Node.

A batched, culled grid of tiles, the efficient way to build large 2D worlds (vs. one Sprite per cell). Backed by willow's tile renderer: one draw batch per layer, GID flip flags, animated tiles, and viewport culling. Construct with a config object:

tks const tiles: Texture = Texture.load("tiles.png"); const regions: Array<TextureRegion> = TextureRegion.grid(tiles, 16, 16); const ground: TileLayer = new TileLayer({ tileWidth: 16, tileHeight: 16, columns: 64, rows: 64, tileset: tiles, regions: regions, data: world.groundData, });

A GID of 0 is an empty cell; GID N maps to regions[N, 1]. The top three bits carry the horizontal / vertical / diagonal flip flags, use the encodeGid / decodeGid statics rather than packing bits by hand. Add a bare TileLayer to the scene to render its whole grid; add it under a TilemapViewport to cull to the camera-visible region.

TileLayer: constructors ​

ts
new TileLayer(config: any)

TileLayer: properties ​

MemberDescription
readonly columns: intGrid width in tiles.
readonly rows: intGrid height in tiles.
readonly tileWidth: floatTile width in pixels.
readonly tileHeight: floatTile height in pixels.

TileLayer: methods ​

MemberDescription
setTile(col: int, row: int, gid: int): voidSets the GID at (col, row). Out-of-range coordinates or a negative GID raise a typed VM error.
getTile(col: int, row: int): intReturns the GID at (col, row) (with any active animation frame applied). Out-of-range coordinates raise a typed VM error.
setData(data: Array<int>): voidReplaces the whole tile grid. data.length must equal columns * rows; otherwise a typed VM error is raised.
setAnimation(gid: int, frames: Array<int>, fps: float): voidAnimates every cell whose base GID equals gid, cycling through frames (base GIDs) at fps frames per second. fps must be > 0.
query(): TileQueryReturns a read-only TileQuery interface over this tile layer.

TileLayer: static methods ​

MemberDescription
static encodeGid(index: int, flipH?: boolean, flipV?: boolean, flipD?: boolean): intPacks a tile index plus flip flags into a GID. Flags default to false. Inverse of decodeGid.
static decodeGid(gid: int): GidInfoUnpacks a GID into its { index, flipH, flipV, flipD }.

TilemapViewport ​

ts
class TilemapViewport extends Node

Extends Node.

A container that clips its child TileLayers to the camera-visible region each frame, only tiles inside the camera frustum (plus a padding margin) are meshed, so a huge map stays cheap. Culls against the engine camera; the optional camera config field is accepted for API symmetry. new TilemapViewport({ padding: 96 }).

TilemapViewport: constructors ​

ts
new TilemapViewport(config?: any)

TilemapViewport: properties ​

MemberDescription
padding: floatExtra cull margin in world pixels beyond the camera frustum.

TileQuery ​

ts
class TileQuery

Read-only view of tilemap data for collision, pathfinding or world queries. Exposes grid dimensions, tile sizes, coordinate conversions and tile ID queries.

TileQuery: constructors ​

ts
new TileQuery(layer: TileLayer)

TileQuery: properties ​

MemberDescription
readonly columns: intGrid width in columns.
readonly rows: intGrid height in rows.
readonly tileWidth: floatTile width in pixels.
readonly tileHeight: floatTile height in pixels.
readonly cols: intGrid width in columns.
readonly tileW: floatTile width in pixels.
readonly tileH: floatTile height in pixels.

TileQuery: methods ​

MemberDescription
tileAt(col: int, row: int): intReturns the tile GID at (col, row) (with active animation), or 0 if out of bounds.
rawAt(col: int, row: int): intReturns the raw (unanimated) tile GID at (col, row), or 0 if out of bounds.
worldToTile(worldX: float, worldY: float): anyConverts world pixel coordinates to tile cell coordinates { col, row }.
tileToWorld(col: int, row: int): Vec2Converts tile coordinates (col, row) to world pixel coordinates { x, y }.
tileAtWorld(worldX: float, worldY: float): intReturns the tile GID at world coordinates (worldX, worldY), or 0 if out of bounds.

GidInfo ​

ts
class GidInfo

Decoded GID returned by TileLayer.decodeGid: the base tile index plus the three Tiled flip flags.

GidInfo: properties ​

MemberDescription
index: intBase tile index (flip bits stripped).
flipH: booleanHorizontal-flip flag.
flipV: booleanVertical-flip flag.
flipD: booleanAnti-diagonal-flip flag.