merge ws/engines

This commit is contained in:
4DBug committed 2026-11-01 20:14:34 -06:00
commit 8bd15840d8
66 files changed
+31751 -1331

No files matched your search

+82 -1
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
@@ -212,6 +212,83 @@ local maps = assert(F.format("gamemaps").maps(F.Bytes.reader(mapheadBytes), { ga
assert(#maps == 1 and maps[1].slot == 1)
```
## 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)
```
## Ogmo Editor 3
[Ogmo Editor 3](https://ogmo-editor-3.github.io) keeps a project (`.ogmo`: layer definitions, entity templates,
tilesets with their images embedded) and one JSON file per level. A level loads as a `TileMap`; pass its project as
`opts.project` (bytes or decoded JSON) to get the tilesets (gids), the map grid, entity template sizes and value
types. Without it tile layers keep `id + 1` as their gids. A project alone loads with `meta.project = true`, its
tilesets and one empty layer per definition.
| Ogmo layer | `TileLayer` (`ogmoType`) |
|---|---|
| tile (ids or coords, 1D or 2D) | `"tiles"` (`"tile"`): `data` gids, `tileset`, the tile flags as Tiled flip bits |
| grid | `"tiles"` (`"grid"`): `grid` (the cell strings, `"0"` empty), `data` (their numbers), `legend` |
| entity | `"objects"` (`"entity"`): `x` / `y` top-left (position minus `originX` / `originY`), `width`, `height`, `rotation` in degrees, `flippedX` / `flippedY`, `nodes` (`{ { x, y } }`), values as `properties` |
| decal | `"objects"` (`"decal"`): `texture`, `x` / `y`, `scaleX` / `scaleY`, `rotation` in degrees, `originX` / `originY` |
Ogmo's first layer is drawn on top, so the layers are reversed (bottom first, as everywhere in `TileMap`). Layers keep
their cell size (`cellWidth`, `cellHeight`) and save layout (`exportMode`, `arrayMode`). Tileset images stay bytes
(`tileset.imageData`, a PNG) next to their `imageSource` path.
```luau
local map = assert(F.load(ogmoLevelText, { format = "ogmo", project = ogmoProjectText }))
assert(map.pxWidth == 640 and map.tileWidth == 8) -- the project's grid
local walls = map.layers[1]
assert(walls.ogmoType == "tile" and walls.tileset == "tiles" and walls.width == 40)
local things = map.layers[2]
assert(things.objects[1].class == "player" and things.objects[1].x == 128)
local level = assert(F.save(map, nil, { format = "ogmo" })) -- a level
local project = assert(F.save(map, nil, { format = "ogmo", kind = "project" })) -- a project for any map
assert(F.detect(project) == "ogmo")
```
## Formats
| Format | Read | Write | Notes |
@@ -221,6 +298,10 @@ assert(#maps == 1 and maps[1].slot == 1)
| Wolfenstein 3D GAMEMAPS + MAPHEAD | yes | | the `gamemaps` format: raw tile values, no tileset |
TMX and TMJ are the `tiled` format in [FORMATS.md](../FORMATS.md). Wang sets and terrains are dropped.
| LDtk (`.ldtk`) | yes | yes | levels as groups, worlds, IntGrid / auto / tile / entity layers, fields; `.ldtkl` through `resolve`; writes embedded levels |
| Ogmo Editor 3 (`.ogmo`, level `.json`) | yes | yes | tile / grid / entity / decal layers, values; the project through `opts.project` |
TMX and TMJ are both the `tiled` format in [FORMATS.md](../FORMATS.md) (Wang sets and terrains are dropped).
## Gotchas