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
| Type | Kind | Description |
|---|---|---|
Cell | type | One nonempty tile at zero-based map coordinates. |
Frame | type | One tile animation frame, with duration in seconds. |
Image | type | A file-backed image and its authored dimensions. |
Layer | type | A flattened layer, with inherited group offsets, tint and parallax applied. |
Map | type | A mutable map. |
Object | type | An object authored in a map or a tile collision layer. |
ObjectFactory | type | A callback that spawns a gameplay entity for an authored object class. |
Options | type | Values accepted by the Tilemap constructor. |
Tile | type | Metadata attached to a tileset's local tile ID. |
Tilemap | type | The component value for one map instance. |
TileRef | type | A reference to one tile, including its Tiled flip flags. |
Tileset | type | A resolved tileset. |
TileSource | type | Metadata stored with a rendered tile. |
Functions
| Function | Kind | Description |
|---|---|---|
getLayer | function | Finds a flattened layer by name. |
getTile | function | Reads a tile at zero-based tile coordinates. |
install | function | Installs tile projection and animation in the world. |
load | function | Loads a TMX map and resolves its external TSX files and templates. |
registerObject | function | Registers how a map object class creates its gameplay entity. |
setTile | function | Changes one tile and invalidates only that cell in live instances. |
spawn | function | Spawns a loaded map and installs its projection system. |
tileToWorld | function | Converts zero-based tile coordinates to their top-left local pixels. |
worldToTile | function | Converts local map pixels to zero-based tile coordinates. |
Values
| Value | Kind | Description |
|---|---|---|
Tilemap | variable | Loads a map as static TileChunks and animated sprite entities. |
TileSource | variable | Constructs metadata identifying a tile's authored source. |
TileVisual | variable | Marks 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#
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 = TilemapValueThe 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 = TileSourceValueMetadata stored with a rendered tile.
Functions#
getLayerfunction#
Finds a flattened layer by name.
Arguments
| Name | Type | Description |
|---|---|---|
map | Map | the loaded map |
name | string | the authored layer name |
Returns
| Type | Description |
|---|---|
Layer? | the layer and its one-based index, or nil |
integer? |
getTilefunction#
Reads a tile at zero-based tile coordinates.
Arguments
| Name | Type | Description |
|---|---|---|
map | Map | the loaded map |
layer | integer | the one-based flattened layer index |
x | integer | the tile column |
y | integer | the tile row |
Returns
| Type | Description |
|---|---|
Cell? | the tile reference, or nil for an empty cell |
installfunction#
function install(exclusive world: World): nilInstalls tile projection and animation in the world.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive world | World | the world receiving maps |
Returns
| Type | Description |
|---|---|
nil |
loadfunction#
function load(path: string): MapLoads a TMX map and resolves its external TSX files and templates.
Arguments
| Name | Type | Description |
|---|---|---|
path | string | the TMX file path |
Returns
| Type | Description |
|---|---|
Map | the editable map |
Raises
when the library cannot load the map or its orientation is unsupported
registerObjectfunction#
function registerObject(class: string, factory: ObjectFactory): nilRegisters how a map object class creates its gameplay entity.
Arguments
| Name | Type | Description |
|---|---|---|
class | string | the Tiled class name |
factory | ObjectFactory | a callback returning a newly spawned entity |
Returns
| Type | Description |
|---|---|
nil |
setTilefunction#
Changes one tile and invalidates only that cell in live instances.
Arguments
| Name | Type | Description |
|---|---|---|
map | Map | the editable map |
layer | integer | the one-based flattened layer index |
x | integer | the zero-based tile column |
y | integer | the zero-based tile row |
tile | TileRef? | the replacement, or nil to clear the cell |
Returns
| Type | Description |
|---|---|
nil |
Raises
when the layer, coordinate, tileset or tile ID is invalid
spawnfunction#
Spawns a loaded map and installs its projection system.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive world | World | the target world |
map | Map | the loaded map |
options | Options? | optional collision settings |
Returns
| Type | Description |
|---|---|
integer | the parent map entity |
tileToWorldfunction#
function tileToWorld(map: Map, x: integer, y: integer): number, numberConverts zero-based tile coordinates to their top-left local pixels.
Arguments
| Name | Type | Description |
|---|---|---|
map | Map | the map whose grid is used |
x | integer | the tile column |
y | integer | the tile row |
Returns
| Type | Description |
|---|---|
number | local horizontal and vertical pixels |
number |
worldToTilefunction#
function worldToTile(map: Map, x: number, y: number): integer, integerConverts local map pixels to zero-based tile coordinates.
Arguments
| Name | Type | Description |
|---|---|---|
map | Map | the map whose grid is used |
x | number | local horizontal pixels |
y | number | local vertical pixels |
Returns
| Type | Description |
|---|---|
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#
const TileVisualMarks generated map visuals, which are replaced on snapshot restoration.