TileLayer
Batched, culled tile grids.
ts
import { TileLayer, TilemapViewport, TileQuery, GidInfo } from "willow";TileLayer
ts
class TileLayer extends NodeExtends 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
| Member | Description |
|---|---|
readonly columns: int | Grid width in tiles. |
readonly rows: int | Grid height in tiles. |
readonly tileWidth: float | Tile width in pixels. |
readonly tileHeight: float | Tile height in pixels. |
TileLayer: methods
| Member | Description |
|---|---|
setTile(col: int, row: int, gid: int): void | Sets the GID at (col, row). Out-of-range coordinates or a negative GID raise a typed VM error. |
getTile(col: int, row: int): int | Returns the GID at (col, row) (with any active animation frame applied). Out-of-range coordinates raise a typed VM error. |
setData(data: Array<int>): void | Replaces 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): void | Animates every cell whose base GID equals gid, cycling through frames (base GIDs) at fps frames per second. fps must be > 0. |
query(): TileQuery | Returns a read-only TileQuery interface over this tile layer. |
TileLayer: static methods
| Member | Description |
|---|---|
static encodeGid(index: int, flipH?: boolean, flipV?: boolean, flipD?: boolean): int | Packs a tile index plus flip flags into a GID. Flags default to false. Inverse of decodeGid. |
static decodeGid(gid: int): GidInfo | Unpacks a GID into its { index, flipH, flipV, flipD }. |
TilemapViewport
ts
class TilemapViewport extends NodeExtends 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
| Member | Description |
|---|---|
padding: float | Extra cull margin in world pixels beyond the camera frustum. |
TileQuery
ts
class TileQueryRead-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
| Member | Description |
|---|---|
readonly columns: int | Grid width in columns. |
readonly rows: int | Grid height in rows. |
readonly tileWidth: float | Tile width in pixels. |
readonly tileHeight: float | Tile height in pixels. |
readonly cols: int | Grid width in columns. |
readonly tileW: float | Tile width in pixels. |
readonly tileH: float | Tile height in pixels. |
TileQuery: methods
| Member | Description |
|---|---|
tileAt(col: int, row: int): int | Returns the tile GID at (col, row) (with active animation), or 0 if out of bounds. |
rawAt(col: int, row: int): int | Returns the raw (unanimated) tile GID at (col, row), or 0 if out of bounds. |
worldToTile(worldX: float, worldY: float): any | Converts world pixel coordinates to tile cell coordinates { col, row }. |
tileToWorld(col: int, row: int): Vec2 | Converts tile coordinates (col, row) to world pixel coordinates { x, y }. |
tileAtWorld(worldX: float, worldY: float): int | Returns the tile GID at world coordinates (worldX, worldY), or 0 if out of bounds. |
GidInfo
ts
class GidInfoDecoded GID returned by TileLayer.decodeGid: the base tile index plus the three Tiled flip flags.
GidInfo: properties
| Member | Description |
|---|---|
index: int | Base tile index (flip bits stripped). |
flipH: boolean | Horizontal-flip flag. |
flipV: boolean | Vertical-flip flag. |
flipD: boolean | Anti-diagonal-flip flag. |