vector/ldtk: LDtk projects read + write as a TileMap (levels as groups, worlds, IntGrid / auto / tile / entity layers, fields, external levels through resolve)

This commit is contained in:
4DBug committed 2026-11-01 18:49:26 -06:00
1 parent 524179ec67
commit 1f74419ccf
11 files changed
+1557 -6

No files matched your search

+49 -2
View File
@@ -1,4 +1,4 @@
# Tile maps (Tiled TMX / TMJ)
# Tile maps (Tiled, LDtk, Ogmo)
[Tiled](https://www.mapeditor.org) maps load as a `TileMap`: the map grid, its tilesets, and a list of layers
(tile layers, object layers, image layers and groups) with Tiled's custom properties. The XML flavour (TMX) and the
@@ -194,14 +194,61 @@ local back = assert(F.load(tmx))
assert(back.layers[1].data[6] == 0x80000009)
```
## LDtk
[LDtk](https://ldtk.io) projects (`.ldtk`) load as a `TileMap` whose top layers are the **levels**: one `"group"`
layer per level (`level = true`, `offsetX` / `offsetY` = its world position in pixels, `pxWidth` / `pxHeight`, the
level fields as `properties`), holding the level's layers bottom first. Multi-world projects add one group per
world (`world = true`) around its levels. External levels (`.ldtkl`) load through `opts.resolve(relPath)`; without
it the level group has `unresolved = true` and no layers.
| LDtk layer | `TileLayer` |
|---|---|
| Tiles, AutoLayer | `type = "tiles"`, `tiles` (every tile: `x`, `y` pixels, `tile` id, `flip` bits, `srcX` / `srcY`, `alpha`), `data` (a gid grid of the top tile per cell), `tileset` |
| IntGrid | the same plus `intGrid` (one value per cell, 0 = empty) and `intGridValues` (`{ value, identifier, color }`) |
| Entities | `type = "objects"`: `class` = the entity identifier, `x` / `y` top-left (pixel position minus `pivot` x size), `width`, `height`, `pivot`, `tags`, `tile`, the fields as `properties` / `propertyTypes` (LDtk type names) |
Every layer also has `ldtkType` and `gridSize` (LDtk layers have their own cell size), and the raw LDtk tables
(definitions, iids, uids, rules) ride along in `.ldtk` fields so a save keeps them. Gids are the tileset's
`firstGid` plus the LDtk tile id, with LDtk's X / Y flips as Tiled's flip bits.
```luau
local map = assert(F.load(ldtkText))
assert(map.meta.source.format == "ldtk")
local level = map.layers[1]
assert(level.level and level.name == "Stamps" and level.pxWidth == 272)
local walls = level.layers[1]
assert(walls.ldtkType == "IntGrid" and walls.intGridValues[1].identifier == "walls")
assert(#walls.tiles == 118 and map.tilesets[1].imageSource == "atlas/Inca_front_by_Kronbits-extended.png")
local cx, cy = walls.tiles[1].x // walls.gridSize, walls.tiles[1].y // walls.gridSize
assert(walls.data[1 + cx + cy * walls.width] ~= 0)
```
Saving writes an LDtk project: maps read from LDtk keep their definitions and raw fields (changed `properties` get
new `realEditorValues`, so the editor sees them); other maps (a Tiled map, a new `TileMap`) get layer, tileset and
entity definitions made from their content, their loose layers forming one level. A tile layer writes its `tiles`
list when it has one, else its `data` grid; set `tiles = nil` after editing `data`. Levels are always embedded.
```luau
local map = F.Types.TileMap.new(2, 1, 16, 16)
table.insert(map.tilesets, { name = "t", firstGid = 1, tileWidth = 16, tileHeight = 16, columns = 2, count = 4,
imageSource = "t.png", imageWidth = 32, imageHeight = 32 })
table.insert(map.layers, { type = "tiles", name = "Floor", width = 2, height = 1, visible = true, opacity = 1,
properties = {}, data = { 1, 4 } })
local ldtk = assert(F.save(map, nil, { format = "ldtk" }))
local back = assert(F.load(ldtk))
assert(back.layers[1].layers[1].tiles[2].tile == 3)
```
## Formats
| Format | Read | Write | Notes |
|---|---|---|---|
| TMX / TSX | yes | yes | CSV, base64 (raw, zlib, gzip) and `<tile>` data; zstd data returns `UNSUPPORTED` |
| TMJ / TSJ | yes | yes | |
| LDtk (`.ldtk`) | yes | yes | levels as groups, worlds, IntGrid / auto / tile / entity layers, fields; `.ldtkl` through `resolve`; writes embedded levels |
Both are the `tiled` format in [FORMATS.md](../FORMATS.md). Wang sets and terrains are dropped.
TMX and TMJ are both the `tiled` format in [FORMATS.md](../FORMATS.md) (Wang sets and terrains are dropped).
## Gotchas