tecs.tiled

Tiled TMX maps and TSX tilesets, loaded by the official Rust tiled crate.

Static atlas tiles draw through 16x16 TileChunks; animated and image-collection tiles use sprite entities. The loader retains tile metadata, object classes and properties, animations, nested layers and infinite-map chunks. Coordinates passed to getTile and setTile are zero-based tile coordinates, including negative coordinates in infinite maps. Tileset indices are one-based, and tile IDs are zero-based within their tileset.

const map = tecs.tiled.load("assets/maps/level.tmx")
tecs.tiled.spawn(world, map)
tecs.tiled.setTile(map, 1, 4, 3, {tileset = 1, id = 7})

registerObject binds an authored object class to game code. Spawned objects belong to the map through ChildOf; snapshots preserve their gameplay state. Tile visuals are derived from map data and rebuilt after a snapshot load.

Module contents

Types

TypeKindDescription
CelltypeOne nonempty tile at zero-based map coordinates.
FrametypeOne tile animation frame, with duration in seconds.
ImagetypeA file-backed image and its authored dimensions.
LayertypeA flattened layer, with inherited group offsets, tint and parallax applied.
MaptypeA mutable map.
ObjecttypeAn object authored in a map or a tile collision layer.
ObjectFactorytypeA callback that spawns a gameplay entity for an authored object class.
OptionstypeValues accepted by the Tilemap constructor.
TiletypeMetadata attached to a tileset's local tile ID.
TilemaptypeThe component value for one map instance.
TileReftypeA reference to one tile, including its Tiled flip flags.
TilesettypeA resolved tileset.
TileSourcetypeMetadata stored with a rendered tile.

Functions

FunctionKindDescription
getLayerfunctionFinds a flattened layer by name.
getTilefunctionReads a tile at zero-based tile coordinates.
installfunctionInstalls tile projection and animation in the world.
loadfunctionLoads a TMX map and resolves its external TSX files and templates.
registerObjectfunctionRegisters how a map object class creates its gameplay entity.
setTilefunctionChanges one tile and invalidates only that cell in live instances.
spawnfunctionSpawns a loaded map and installs its projection system.
tileToWorldfunctionConverts zero-based tile coordinates to their top-left local pixels.
worldToTilefunctionConverts local map pixels to zero-based tile coordinates.

Values

ValueKindDescription
TilemapvariableLoads a map as static TileChunks and animated sprite entities.
TileSourcevariableConstructs metadata identifying a tile's authored source.
TileVisualvariableMarks generated map visuals, which are replaced on snapshot restoration.

Types#

Celltype#

type Cell = {
    x: integer,
    y: integer,
    tileset: integer,
    id: integer,
    flipH: boolean,
    flipV: boolean,
    flipD: boolean
}

One nonempty tile at zero-based map coordinates.

Frametype#

type Frame = {
    id: integer,
    duration: number
}

One tile animation frame, with duration in seconds.

Imagetype#

type Image = {
    path: string,
    width: integer,
    height: integer,
    transparent: {integer}?
}

A file-backed image and its authored dimensions.

Layertype#

type Layer = {
    id: integer,
    name: string,
    kind: string,
    visible: boolean,
    x: number,
    y: number,
    parallaxX: number,
    parallaxY: number,
    opacity: number,
    tint: {number},
    properties: {[string]: any},
    cells: {Cell},
    objects: {Object},
    width: integer,
    height: integer,
    image: Image?,
    repeatX: boolean,
    repeatY: boolean
}

A flattened layer, with inherited group offsets, tint and parallax applied.

Maptype#

type Map = {
    source: string,
    parallaxX: number,
    parallaxY: number,
    width: integer,
    height: integer,
    tileWidth: integer,
    tileHeight: integer,
    infinite: boolean,
    properties: {[string]: any},
    tilesets: {Tileset},
    layers: {Layer},
    revision: integer,
    changes: {[string]: integer}
}

A mutable map. Use setTile to notify every live instance of an edit.

Objecttype#

type Object = {
    id: integer,
    name: string,
    class: string,
    x: number,
    y: number,
    width: number,
    height: number,
    rotation: number,
    visible: boolean,
    opacity: number,
    shape: string,
    points: {{number}},
    text: string,
    properties: {[string]: any},
    tile: {
        source: string,
        name: string,
        id: integer,
        flipH: boolean,
        flipV: boolean,
        flipD: boolean
    }?
}

An object authored in a map or a tile collision layer.

ObjectFactorytype#

type ObjectFactory = function(exclusive world: World, object: Object): integer

A callback that spawns a gameplay entity for an authored object class.

Optionstype#

type Options = {
    path: string?,
    map: Map?,
    collision: boolean?
}

Values accepted by the Tilemap constructor.

Tiletype#

type Tile = {
    id: integer,
    class: string,
    properties: {[string]: any},
    image: Image?,
    collision: {Object},
    animation: {Frame},
    rect: {integer}?
}

Metadata attached to a tileset's local tile ID.

Tilemaptype#

type Tilemap = TilemapValue

The component value for one map instance.

TileReftype#

type TileRef = {
    tileset: integer,
    id: integer,
    flipH: boolean?,
    flipV: boolean?,
    flipD: boolean?
}

A reference to one tile, including its Tiled flip flags.

Tilesettype#

type Tileset = {
    name: string,
    source: string,
    alignment: string,
    tileWidth: integer,
    tileHeight: integer,
    columns: integer,
    count: integer,
    spacing: integer,
    margin: integer,
    x: integer,
    y: integer,
    image: Image?,
    tiles: {Tile},
    properties: {[string]: any}
}

A resolved tileset. Paths already include the TSX file's directory.

TileSourcetype#

type TileSource = TileSourceValue

Metadata stored with a rendered tile.

Functions#

getLayerfunction#

function getLayer(map: Map, name: string): Layer?, integer?

Finds a flattened layer by name.

Arguments

NameTypeDescription
mapMap

the loaded map

namestring

the authored layer name

Returns

TypeDescription
Layer?

the layer and its one-based index, or nil

integer?

getTilefunction#

function getTile(map: Map, layer: integer, x: integer, y: integer): Cell?

Reads a tile at zero-based tile coordinates.

Arguments

NameTypeDescription
mapMap

the loaded map

layerinteger

the one-based flattened layer index

xinteger

the tile column

yinteger

the tile row

Returns

TypeDescription
Cell?

the tile reference, or nil for an empty cell

installfunction#

function install(exclusive world: World): nil

Installs tile projection and animation in the world.

Arguments

NameTypeDescription
exclusive worldWorld

the world receiving maps

Returns

TypeDescription
nil

loadfunction#

function load(path: string): Map

Loads a TMX map and resolves its external TSX files and templates.

Arguments

NameTypeDescription
pathstring

the TMX file path

Returns

TypeDescription
Map

the editable map

Raises

TypeCondition
string

when the library cannot load the map or its orientation is unsupported

registerObjectfunction#

function registerObject(class: string, factory: ObjectFactory): nil

Registers how a map object class creates its gameplay entity.

Arguments

NameTypeDescription
classstring

the Tiled class name

factoryObjectFactory

a callback returning a newly spawned entity

Returns

TypeDescription
nil

setTilefunction#

function setTile(map: Map, layer: integer, x: integer, y: integer, tile: TileRef?): nil

Changes one tile and invalidates only that cell in live instances.

Arguments

NameTypeDescription
mapMap

the editable map

layerinteger

the one-based flattened layer index

xinteger

the zero-based tile column

yinteger

the zero-based tile row

tileTileRef?

the replacement, or nil to clear the cell

Returns

TypeDescription
nil

Raises

TypeCondition
string

when the layer, coordinate, tileset or tile ID is invalid

spawnfunction#

function spawn(exclusive world: World, map: Map, options: Options?): integer

Spawns a loaded map and installs its projection system.

Arguments

NameTypeDescription
exclusive worldWorld

the target world

mapMap

the loaded map

optionsOptions?

optional collision settings

Returns

TypeDescription
integer

the parent map entity

tileToWorldfunction#

function tileToWorld(map: Map, x: integer, y: integer): number, number

Converts zero-based tile coordinates to their top-left local pixels.

Arguments

NameTypeDescription
mapMap

the map whose grid is used

xinteger

the tile column

yinteger

the tile row

Returns

TypeDescription
number

local horizontal and vertical pixels

number

worldToTilefunction#

function worldToTile(map: Map, x: number, y: number): integer, integer

Converts local map pixels to zero-based tile coordinates.

Arguments

NameTypeDescription
mapMap

the map whose grid is used

xnumber

local horizontal pixels

ynumber

local vertical pixels

Returns

TypeDescription
integer

the tile column and row

integer

Values#

Tilemapvariable#

const Tilemap: ComponentDefinition<TilemapValue>

Loads a map as static TileChunks and animated sprite entities.

TileSourcevariable#

const TileSource: ComponentDefinition<TileSourceValue>

Constructs metadata identifying a tile's authored source.

TileVisualvariable#

Marks generated map visuals, which are replaced on snapshot restoration.