@margoengine/tilemap 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +294 -3
- package/dist/autotile.d.ts +105 -0
- package/dist/autotile.js +116 -0
- package/dist/chunks.d.ts +45 -0
- package/dist/fov.d.ts +73 -0
- package/dist/fov.js +107 -0
- package/dist/hex.d.ts +21 -0
- package/dist/hex.js +101 -0
- package/dist/import-DsL40A38.js +33 -0
- package/dist/import.d.ts +29 -0
- package/dist/index.d.ts +240 -0
- package/dist/index.js +1 -0
- package/dist/iso-C2Hwh5Gr.js +300 -0
- package/dist/iso.d.ts +114 -0
- package/dist/iso.js +3 -0
- package/dist/layout-CLx3KdGC.js +124 -0
- package/dist/layout.d.ts +37 -0
- package/dist/ldtk.d.ts +72 -0
- package/dist/ldtk.js +187 -0
- package/dist/object-quads.d.ts +31 -0
- package/dist/objects-BK2mewd1.js +444 -0
- package/dist/objects.d.ts +88 -0
- package/dist/objects.js +1 -0
- package/dist/path.d.ts +68 -0
- package/dist/path.js +178 -0
- package/dist/plane.d.ts +37 -0
- package/dist/plane.js +76 -0
- package/dist/renderer.d.ts +177 -0
- package/dist/src-CJvCfCkX.js +602 -0
- package/dist/tiled-xml-Dp4Ez81m.js +89 -0
- package/dist/tiled-xml.d.ts +3 -0
- package/dist/tiled.d.ts +86 -0
- package/dist/tiled.js +541 -0
- package/package.json +90 -6
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
//#region src/layout.ts
|
|
2
|
+
var e = (e) => {
|
|
3
|
+
let t = e.orientation ?? "orthogonal", n = e.tileWidth, r = e.tileHeight;
|
|
4
|
+
if (!(n > 0 && r > 0)) throw RangeError(`tile size must be positive, got ${n} x ${r}.`);
|
|
5
|
+
if (t === "orthogonal" || t === "isometric") return {
|
|
6
|
+
kind: t,
|
|
7
|
+
tw: n,
|
|
8
|
+
th: r,
|
|
9
|
+
rows: e.height,
|
|
10
|
+
axisY: !0,
|
|
11
|
+
odd: !0,
|
|
12
|
+
side: 0
|
|
13
|
+
};
|
|
14
|
+
if (t !== "staggered" && t !== "hexagonal") throw RangeError(`orientation "${t}" is not a layout; use orthogonal, isometric, staggered or hexagonal.`);
|
|
15
|
+
let i = (e.staggerAxis ?? "y") === "y", a = t === "hexagonal" ? e.hexSideLength ?? 0 : 0;
|
|
16
|
+
if (!(a >= 0) || a > (i ? r : n)) throw RangeError(`hexSideLength must be 0 to the tile ${i ? "height" : "width"}, got ${e.hexSideLength}.`);
|
|
17
|
+
return {
|
|
18
|
+
kind: "stagger",
|
|
19
|
+
tw: n,
|
|
20
|
+
th: r,
|
|
21
|
+
rows: e.height,
|
|
22
|
+
axisY: i,
|
|
23
|
+
odd: (e.staggerIndex ?? "odd") === "odd",
|
|
24
|
+
side: a
|
|
25
|
+
};
|
|
26
|
+
}, t = (e, t) => (t & 1) == 1 === e.odd;
|
|
27
|
+
function n(n, r, i) {
|
|
28
|
+
let a = e(n), o, s;
|
|
29
|
+
return a.kind === "orthogonal" ? (o = r * a.tw, s = i * a.th) : a.kind === "isometric" ? (o = (r - i + a.rows - 1) * a.tw / 2, s = (r + i) * a.th / 2) : a.axisY ? (o = r * a.tw + (t(a, i) ? a.tw / 2 : 0), s = i * (a.th + a.side) / 2) : (o = r * (a.tw + a.side) / 2, s = i * a.th + (t(a, r) ? a.th / 2 : 0)), {
|
|
30
|
+
x: o,
|
|
31
|
+
y: s,
|
|
32
|
+
width: a.tw,
|
|
33
|
+
height: a.th
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function r(e, t, r) {
|
|
37
|
+
let i = n(e, t, r);
|
|
38
|
+
return [i.x + i.width / 2, i.y + i.height / 2];
|
|
39
|
+
}
|
|
40
|
+
function i(t, r, i) {
|
|
41
|
+
let a = e(t), { x: o, y: s, width: c, height: l } = n(t, r, i);
|
|
42
|
+
if (a.kind === "orthogonal") return [
|
|
43
|
+
[o, s],
|
|
44
|
+
[o + c, s],
|
|
45
|
+
[o + c, s + l],
|
|
46
|
+
[o, s + l]
|
|
47
|
+
];
|
|
48
|
+
let u = a.side;
|
|
49
|
+
return u ? a.axisY ? [
|
|
50
|
+
[o + c / 2, s],
|
|
51
|
+
[o + c, s + (l - u) / 2],
|
|
52
|
+
[o + c, s + (l + u) / 2],
|
|
53
|
+
[o + c / 2, s + l],
|
|
54
|
+
[o, s + (l + u) / 2],
|
|
55
|
+
[o, s + (l - u) / 2]
|
|
56
|
+
] : [
|
|
57
|
+
[o + (c - u) / 2, s],
|
|
58
|
+
[o + (c + u) / 2, s],
|
|
59
|
+
[o + c, s + l / 2],
|
|
60
|
+
[o + (c + u) / 2, s + l],
|
|
61
|
+
[o + (c - u) / 2, s + l],
|
|
62
|
+
[o, s + l / 2]
|
|
63
|
+
] : [
|
|
64
|
+
[o + c / 2, s],
|
|
65
|
+
[o + c, s + l / 2],
|
|
66
|
+
[o + c / 2, s + l],
|
|
67
|
+
[o, s + l / 2]
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
var a = (e, t, n) => {
|
|
71
|
+
for (let r = 0; r < e.length; r++) {
|
|
72
|
+
let [i, a] = e[r], [o, s] = e[(r + 1) % e.length];
|
|
73
|
+
if ((o - i) * (n - a) - (s - a) * (t - i) < 0) return !1;
|
|
74
|
+
}
|
|
75
|
+
return !0;
|
|
76
|
+
};
|
|
77
|
+
function o(n, o, s) {
|
|
78
|
+
let c = e(n);
|
|
79
|
+
if (c.kind === "orthogonal") return [Math.floor(o / c.tw), Math.floor(s / c.th)];
|
|
80
|
+
if (c.kind === "isometric") {
|
|
81
|
+
let e = (o - c.rows * c.tw / 2) / c.tw, t = s / c.th;
|
|
82
|
+
return [Math.floor(t + e), Math.floor(t - e)];
|
|
83
|
+
}
|
|
84
|
+
let l = c.axisY ? s / ((c.th + c.side) / 2) : o / ((c.tw + c.side) / 2), u = Math.floor(l), d, f = Infinity;
|
|
85
|
+
for (let e = u - 1; e <= u + 1; e++) {
|
|
86
|
+
let l = t(c, e) ? .5 : 0, u = Math.floor((c.axisY ? o / c.tw : s / c.th) - l);
|
|
87
|
+
for (let t = u - 1; t <= u + 1; t++) {
|
|
88
|
+
let [l, u] = c.axisY ? [t, e] : [e, t];
|
|
89
|
+
if (a(i(n, l, u), o, s)) return [l, u];
|
|
90
|
+
let [p, m] = r(n, l, u), h = ((o - p) / c.tw) ** 2 + ((s - m) / c.th) ** 2;
|
|
91
|
+
h < f && (f = h, d = [l, u]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return d;
|
|
95
|
+
}
|
|
96
|
+
function s(n, r, i) {
|
|
97
|
+
let a = e(n);
|
|
98
|
+
if (a.kind === "orthogonal" || a.kind === "isometric") return [
|
|
99
|
+
[r, i - 1],
|
|
100
|
+
[r + 1, i],
|
|
101
|
+
[r, i + 1],
|
|
102
|
+
[r - 1, i]
|
|
103
|
+
];
|
|
104
|
+
let o = a.side > 0;
|
|
105
|
+
if (a.axisY) {
|
|
106
|
+
let e = t(a, i) ? 0 : -1, n = [[r + e + 1, i - 1]];
|
|
107
|
+
return o && n.push([r + 1, i]), n.push([r + e + 1, i + 1], [r + e, i + 1]), o && n.push([r - 1, i]), n.push([r + e, i - 1]), n;
|
|
108
|
+
}
|
|
109
|
+
let s = t(a, r) ? 0 : -1, c = [];
|
|
110
|
+
return o && c.push([r, i - 1]), c.push([r + 1, i + s], [r + 1, i + s + 1]), o && c.push([r, i + 1]), c.push([r - 1, i + s + 1], [r - 1, i + s]), c;
|
|
111
|
+
}
|
|
112
|
+
function c(n) {
|
|
113
|
+
let r = e(n), i = n.width, a = n.height;
|
|
114
|
+
return !i || !a ? [0, 0] : r.kind === "orthogonal" ? [i * r.tw, a * r.th] : r.kind === "isometric" ? [(i + a) * r.tw / 2, (i + a) * r.th / 2] : r.axisY ? [i * r.tw + (a > 1 || t(r, 0) ? r.tw / 2 : 0), (a - 1) * (r.th + r.side) / 2 + r.th] : [(i - 1) * (r.tw + r.side) / 2 + r.tw, a * r.th + (i > 1 || t(r, 0) ? r.th / 2 : 0)];
|
|
115
|
+
}
|
|
116
|
+
function l(n, r = n.width, i = n.height) {
|
|
117
|
+
let a = e(n), o = new Uint32Array(r * i), s = 0;
|
|
118
|
+
if (a.kind === "isometric") for (let e = 0; e < r + i - 1; e++) for (let t = Math.min(e, i - 1); t >= 0 && e - t < r; t--) o[s++] = t * r + e - t;
|
|
119
|
+
else if (a.kind === "stagger" && !a.axisY) for (let e = 0; e < i; e++) for (let n of [!1, !0]) for (let i = 0; i < r; i++) t(a, i) === n && (o[s++] = e * r + i);
|
|
120
|
+
else for (let e = 0; e < o.length; e++) o[e] = e;
|
|
121
|
+
return o;
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { c as a, i, r as n, l as o, s as r, o as s, n as t };
|
package/dist/layout.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { TileMapData } from './index.js';
|
|
2
|
+
/** The grid fields of a map, as {@link TileMapData} carries them. */
|
|
3
|
+
export type MapGrid = Pick<TileMapData, 'width' | 'height' | 'tileWidth' | 'tileHeight' | 'orientation' | 'staggerAxis' | 'staggerIndex' | 'hexSideLength'>;
|
|
4
|
+
/** A cell's place: its bounding box's top left in map pixels, and its size. */
|
|
5
|
+
export interface CellBox {
|
|
6
|
+
readonly x: number;
|
|
7
|
+
readonly y: number;
|
|
8
|
+
readonly width: number;
|
|
9
|
+
readonly height: number;
|
|
10
|
+
}
|
|
11
|
+
/** Where cell (`x`, `y`) sits: the top left of its bounding box in map pixels, and its size (the map's tile size). */
|
|
12
|
+
export declare function cellBox(map: MapGrid, x: number, y: number): CellBox;
|
|
13
|
+
/** The centre of cell (`x`, `y`) in map pixels. */
|
|
14
|
+
export declare function cellCenter(map: MapGrid, x: number, y: number): [number, number];
|
|
15
|
+
/**
|
|
16
|
+
* The outline of cell (`x`, `y`) in map pixels, clockwise from the top: 4 corners for orthogonal, isometric and
|
|
17
|
+
* staggered cells, 6 for hexagonal ones with a side length above 0.
|
|
18
|
+
*/
|
|
19
|
+
export declare function cellPolygon(map: MapGrid, x: number, y: number): [number, number][];
|
|
20
|
+
/**
|
|
21
|
+
* The cell under map pixel (`px`, `py`), which may lie outside the map. A point on a shared edge goes to the cell
|
|
22
|
+
* found first: the lower row, then the lower column.
|
|
23
|
+
*/
|
|
24
|
+
export declare function pixelToCell(map: MapGrid, px: number, py: number): [number, number];
|
|
25
|
+
/**
|
|
26
|
+
* The cells that share an edge with (`x`, `y`), clockwise from the top or top right: 4 on orthogonal, isometric and
|
|
27
|
+
* staggered maps, 6 on hexagonal ones. Cells outside the map are included; filter them with the map size.
|
|
28
|
+
*/
|
|
29
|
+
export declare function cellNeighbors(map: MapGrid, x: number, y: number): [number, number][];
|
|
30
|
+
/** The map's size in pixels: the box around every cell. */
|
|
31
|
+
export declare function mapPixelSize(map: MapGrid): [number, number];
|
|
32
|
+
/**
|
|
33
|
+
* Every cell of a `width` x `height` layer in painter's order, back to front: the order that lets tall tiles cover the
|
|
34
|
+
* cells behind them. Rows top to bottom on orthogonal and y-staggered maps, diagonals (x + y) on isometric ones, and on
|
|
35
|
+
* x-staggered maps each row's raised columns before its lowered ones. Returns cell indices `y * width + x`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function paintOrder(map: MapGrid, width?: number, height?: number): Uint32Array;
|
package/dist/ldtk.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type TileMapData } from './index.js';
|
|
2
|
+
import { type Properties } from './import.js';
|
|
3
|
+
export { MapImportError, type MapImportErrorCode } from './import.js';
|
|
4
|
+
/** Options of {@link parseLDtk}. */
|
|
5
|
+
export interface ParseLDtkOptions {
|
|
6
|
+
/** Called for each thing the import drops or moves. Default ignores them. */
|
|
7
|
+
onWarning?: (message: string) => void;
|
|
8
|
+
/** Stops the import between levels. */
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
}
|
|
11
|
+
/** A placed LDtk entity. */
|
|
12
|
+
export interface LDtkEntity {
|
|
13
|
+
/** The entity's identifier in the project. */
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly iid?: string;
|
|
16
|
+
/** The layer it was placed on. */
|
|
17
|
+
readonly layer: string;
|
|
18
|
+
/** Pixel position in the level: the entity's pivot point. */
|
|
19
|
+
readonly x: number;
|
|
20
|
+
readonly y: number;
|
|
21
|
+
/** Pixel position in the world. */
|
|
22
|
+
readonly worldX: number;
|
|
23
|
+
readonly worldY: number;
|
|
24
|
+
readonly width: number;
|
|
25
|
+
readonly height: number;
|
|
26
|
+
/** Pivot, 0 to 1 from the top left. Default [0, 0]. */
|
|
27
|
+
readonly pivot: readonly [number, number];
|
|
28
|
+
readonly tags: readonly string[];
|
|
29
|
+
/** Field values by identifier. */
|
|
30
|
+
readonly fields: Properties;
|
|
31
|
+
}
|
|
32
|
+
/** The value grid of an IntGrid layer, row by row from the top left; 0 is empty. */
|
|
33
|
+
export interface LDtkIntGrid {
|
|
34
|
+
readonly width: number;
|
|
35
|
+
readonly height: number;
|
|
36
|
+
/** Pixels per cell. */
|
|
37
|
+
readonly gridSize: number;
|
|
38
|
+
readonly values: Int32Array;
|
|
39
|
+
}
|
|
40
|
+
/** One level. */
|
|
41
|
+
export interface LDtkLevel {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly iid?: string;
|
|
44
|
+
/** The world this level belongs to, when the project has several. */
|
|
45
|
+
readonly world?: string;
|
|
46
|
+
/** Pixel position in the world. */
|
|
47
|
+
readonly worldX: number;
|
|
48
|
+
readonly worldY: number;
|
|
49
|
+
readonly worldDepth: number;
|
|
50
|
+
/** Tile layers and one object layer per entity layer, back to front. */
|
|
51
|
+
readonly map: TileMapData;
|
|
52
|
+
readonly entities: readonly LDtkEntity[];
|
|
53
|
+
/** IntGrid layers by identifier. */
|
|
54
|
+
readonly intGrids: {
|
|
55
|
+
readonly [layer: string]: LDtkIntGrid;
|
|
56
|
+
};
|
|
57
|
+
/** Level field values by identifier. */
|
|
58
|
+
readonly fields: Properties;
|
|
59
|
+
}
|
|
60
|
+
/** A parsed LDtk project. */
|
|
61
|
+
export interface LDtkProject {
|
|
62
|
+
/** The format version the file was saved with. */
|
|
63
|
+
readonly version?: string;
|
|
64
|
+
readonly levels: readonly LDtkLevel[];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Reads an LDtk project saved as JSON (`.ldtk`): text, UTF-8 bytes or the parsed object. Each level becomes a
|
|
68
|
+
* {@link TileMapData} plus its entities, IntGrid values and fields. All worlds of a multi-world project are read.
|
|
69
|
+
* @throws MapImportError with a `code` (`syntax`, `truncated`, `missing`, `unsupported`, `limit`, `aborted`) and a message
|
|
70
|
+
* that names the fix. Levels saved to separate `.ldtkl` files are refused.
|
|
71
|
+
*/
|
|
72
|
+
export declare function parseLDtk(input: string | Uint8Array | object, options?: ParseLDtkOptions): LDtkProject;
|
package/dist/ldtk.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { i as e, n as t } from "./src-CJvCfCkX.js";
|
|
2
|
+
import { a as n, c as r, i, l as a, n as o, o as s, s as c, t as l, u } from "./import-DsL40A38.js";
|
|
3
|
+
//#region src/ldtk.ts
|
|
4
|
+
var d = "LDtk", f = (e, t) => {
|
|
5
|
+
throw new o(d, e, t);
|
|
6
|
+
};
|
|
7
|
+
function p(e) {
|
|
8
|
+
let t = {};
|
|
9
|
+
for (let n of e) i(n) && typeof n.__identifier == "string" && r(t, n.__identifier, n.__value ?? null);
|
|
10
|
+
return t;
|
|
11
|
+
}
|
|
12
|
+
function m(e) {
|
|
13
|
+
let t = [], r = /* @__PURE__ */ new Map(), o = 1;
|
|
14
|
+
for (let c of n(e, "tilesets")) {
|
|
15
|
+
if (!i(c)) continue;
|
|
16
|
+
let e = s(c, "tileGridSize"), l = s(c, "__cWid"), u = s(c, "__cHei"), d = a(c, "identifier") ?? `tileset${s(c, "uid")}`, p = a(c, "relPath");
|
|
17
|
+
(!(e > 0) || !(l > 0) || !(u > 0)) && f("missing", `tileset "${d}" has no tileGridSize, __cWid or __cHei; save the project again with LDtk.`);
|
|
18
|
+
let m = /* @__PURE__ */ new Map(), h = (e) => {
|
|
19
|
+
let t = m.get(e);
|
|
20
|
+
return t || m.set(e, t = { id: e }), t;
|
|
21
|
+
};
|
|
22
|
+
for (let e of n(c, "customData")) i(e) && (h(s(e, "tileId")).properties = { customData: a(e, "data") ?? "" });
|
|
23
|
+
for (let e of n(c, "enumTags")) if (i(e)) for (let t of n(e, "tileIds")) typeof t == "number" && (h(t).type = a(e, "enumValueId"));
|
|
24
|
+
let g = {
|
|
25
|
+
name: d,
|
|
26
|
+
firstGid: o,
|
|
27
|
+
tileWidth: e,
|
|
28
|
+
tileHeight: e,
|
|
29
|
+
tileCount: l * u,
|
|
30
|
+
columns: l,
|
|
31
|
+
spacing: s(c, "spacing"),
|
|
32
|
+
margin: s(c, "padding"),
|
|
33
|
+
...p ? { image: {
|
|
34
|
+
source: p,
|
|
35
|
+
width: s(c, "pxWid"),
|
|
36
|
+
height: s(c, "pxHei")
|
|
37
|
+
} } : {},
|
|
38
|
+
...m.size ? { tiles: [...m.values()].sort((e, t) => e.id - t.id) } : {}
|
|
39
|
+
};
|
|
40
|
+
t.push(g), r.set(s(c, "uid"), g), o += g.tileCount;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
sets: t,
|
|
44
|
+
byUid: r
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
var h = (e, t) => t ? h(t, e % t) : e;
|
|
48
|
+
function g(n, r, a, o, c, l, u, d) {
|
|
49
|
+
let p = [], m = 0, h = 0;
|
|
50
|
+
for (let n of r) {
|
|
51
|
+
(!i(n) || !Array.isArray(n.px)) && f("syntax", `layer "${o}" has a tile with no px; save it again.`);
|
|
52
|
+
let r = n, d = s(r, "t", -1), g = s(r, "f");
|
|
53
|
+
(d < 0 || d >= a.tileCount) && f("syntax", `layer "${o}" uses tile ${d}, outside tileset "${a.name}" (0..${a.tileCount - 1}).`);
|
|
54
|
+
let [_, v] = r.px, y = _ / c, b = (v + a.tileHeight) / c - 1, x = Math.round(y), S = Math.round(b);
|
|
55
|
+
if ((x !== y || S !== b) && m++, s(r, "a", 1) !== 1 && h++, x < 0 || S < 0 || x >= l || S >= u) {
|
|
56
|
+
m++;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
let C = S * l + x, w = a.firstGid + d + (g & 1 ? t : 0) + (g & 2 ? e : 0) >>> 0, T = 0;
|
|
60
|
+
for (; T < p.length && p[T][C];) T++;
|
|
61
|
+
T === p.length && p.push(new Uint32Array(l * u)), p[T][C] = w;
|
|
62
|
+
}
|
|
63
|
+
m && d(`layer "${o}": ${m} tiles off the grid moved to the nearest cell or dropped past the edge.`), h && d(`layer "${o}": ${h} tiles have their own alpha, which a tile layer cannot hold; drawn opaque.`);
|
|
64
|
+
let g = {
|
|
65
|
+
opacity: s(n, "__opacity", 1),
|
|
66
|
+
visible: n.visible !== !1,
|
|
67
|
+
offset: [s(n, "__pxTotalOffsetX"), s(n, "__pxTotalOffsetY")]
|
|
68
|
+
};
|
|
69
|
+
return p.map((e, t) => ({
|
|
70
|
+
...g,
|
|
71
|
+
type: "tile",
|
|
72
|
+
name: t ? `${o}#${t + 1}` : o,
|
|
73
|
+
width: l,
|
|
74
|
+
height: u,
|
|
75
|
+
data: e
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
function _(e, t, r, o) {
|
|
79
|
+
let c = [];
|
|
80
|
+
for (let l of n(e, "entityInstances")) {
|
|
81
|
+
if (!i(l)) continue;
|
|
82
|
+
let e = Array.isArray(l.px) ? l.px : [0, 0], u = Array.isArray(l.__pivot) ? l.__pivot : [0, 0], d = s(l, "width"), f = s(l, "height"), m = a(l, "__identifier") ?? "entity", h = p(n(l, "fieldInstances"));
|
|
83
|
+
o.push({
|
|
84
|
+
name: m,
|
|
85
|
+
iid: a(l, "iid"),
|
|
86
|
+
layer: t,
|
|
87
|
+
x: e[0],
|
|
88
|
+
y: e[1],
|
|
89
|
+
worldX: s(l, "__worldX"),
|
|
90
|
+
worldY: s(l, "__worldY"),
|
|
91
|
+
width: d,
|
|
92
|
+
height: f,
|
|
93
|
+
pivot: u,
|
|
94
|
+
tags: n(l, "__tags").filter((e) => typeof e == "string"),
|
|
95
|
+
fields: h
|
|
96
|
+
}), c.push({
|
|
97
|
+
id: r + c.length,
|
|
98
|
+
name: m,
|
|
99
|
+
type: m,
|
|
100
|
+
x: e[0] - u[0] * d,
|
|
101
|
+
y: e[1] - u[1] * f,
|
|
102
|
+
shape: {
|
|
103
|
+
kind: "rect",
|
|
104
|
+
x: 0,
|
|
105
|
+
y: 0,
|
|
106
|
+
width: d,
|
|
107
|
+
height: f
|
|
108
|
+
},
|
|
109
|
+
properties: h
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
type: "object",
|
|
114
|
+
name: t,
|
|
115
|
+
objects: c,
|
|
116
|
+
opacity: s(e, "__opacity", 1),
|
|
117
|
+
visible: e.visible !== !1,
|
|
118
|
+
offset: [s(e, "__pxTotalOffsetX"), s(e, "__pxTotalOffsetY")]
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function v(e, t, o, c, u) {
|
|
122
|
+
let d = a(e, "identifier") ?? "level", m = s(e, "pxWid"), v = s(e, "pxHei");
|
|
123
|
+
Array.isArray(e.layerInstances) || f("unsupported", `level "${d}" is saved in its own .ldtkl file; turn off "Save levels to separate files" in LDtk, or merge its layerInstances into the level first.`);
|
|
124
|
+
let y = e.layerInstances.filter(i).reverse(), b = 0;
|
|
125
|
+
for (let e of y) n(e, "gridTiles").length + n(e, "autoLayerTiles").length && (b = h(b, s(e, "__gridSize")));
|
|
126
|
+
if (!b) for (let e of y) b = h(b, s(e, "__gridSize"));
|
|
127
|
+
b ||= 16;
|
|
128
|
+
let x = Math.ceil(m / b), S = Math.ceil(v / b);
|
|
129
|
+
x * S > 16777216 && f("limit", `level "${d}" has ${x} x ${S} cells, past the limit of ${l}; split the level.`);
|
|
130
|
+
let C = [], w = [], T = {};
|
|
131
|
+
for (let e of y) {
|
|
132
|
+
let t = a(e, "__identifier") ?? "layer", i = a(e, "__type");
|
|
133
|
+
if (i === "Entities") {
|
|
134
|
+
C.push(_(e, t, w.length + 1, w));
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (i === "IntGrid") {
|
|
138
|
+
let i = s(e, "__cWid"), a = s(e, "__cHei"), o = n(e, "intGridCsv");
|
|
139
|
+
i * a > 16777216 && f("limit", `layer "${t}" has ${i} x ${a} cells, past the limit of ${l}; split the level.`), o.length !== i * a && f("truncated", `layer "${t}" has ${o.length} intGridCsv values for ${i} x ${a} cells; save the project again.`), r(T, t, {
|
|
140
|
+
width: i,
|
|
141
|
+
height: a,
|
|
142
|
+
gridSize: s(e, "__gridSize"),
|
|
143
|
+
values: Int32Array.from(o)
|
|
144
|
+
});
|
|
145
|
+
} else i !== "Tiles" && i !== "AutoLayer" && f("unsupported", `layer "${t}" has type "${i}"; this importer reads IntGrid, Entities, Tiles and AutoLayer.`);
|
|
146
|
+
let c = [...n(e, "autoLayerTiles"), ...n(e, "gridTiles")];
|
|
147
|
+
if (!c.length) continue;
|
|
148
|
+
let d = s(e, "overrideTilesetUid", s(e, "__tilesetDefUid", -1)), p = o.get(d);
|
|
149
|
+
if (!p) return f("missing", `layer "${t}" uses tileset ${d}, which defs.tilesets does not list; save the project again.`);
|
|
150
|
+
C.push(...g(e, c, p, t, b, x, S, u));
|
|
151
|
+
}
|
|
152
|
+
let E = a(e, "__bgColor");
|
|
153
|
+
return {
|
|
154
|
+
name: d,
|
|
155
|
+
iid: a(e, "iid"),
|
|
156
|
+
world: c,
|
|
157
|
+
worldX: s(e, "worldX"),
|
|
158
|
+
worldY: s(e, "worldY"),
|
|
159
|
+
worldDepth: s(e, "worldDepth"),
|
|
160
|
+
map: {
|
|
161
|
+
width: x,
|
|
162
|
+
height: S,
|
|
163
|
+
tileWidth: b,
|
|
164
|
+
tileHeight: b,
|
|
165
|
+
tilesets: t,
|
|
166
|
+
layers: C,
|
|
167
|
+
...E ? { backgroundColor: E } : {}
|
|
168
|
+
},
|
|
169
|
+
entities: w,
|
|
170
|
+
intGrids: T,
|
|
171
|
+
fields: p(n(e, "fieldInstances"))
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function y(e, t = {}) {
|
|
175
|
+
let { signal: r, onWarning: o } = t, s = (e) => o?.(e);
|
|
176
|
+
u(d, r);
|
|
177
|
+
let l = c(d, e);
|
|
178
|
+
(!i(l.defs) || !Array.isArray(l.levels) && !Array.isArray(l.worlds)) && f("syntax", "expected \"defs\" and \"levels\" at the top level; pass the .ldtk project file itself.");
|
|
179
|
+
let { sets: p, byUid: h } = m(l.defs), g = [], _ = n(l, "worlds").filter(i), y = _.length ? _.map((e) => [a(e, "identifier"), n(e, "levels")]) : [[void 0, n(l, "levels")]];
|
|
180
|
+
for (let [e, t] of y) for (let n of t) u(d, r), i(n) && g.push(v(n, p, h, e, s));
|
|
181
|
+
return {
|
|
182
|
+
version: a(l, "jsonVersion"),
|
|
183
|
+
levels: g
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
187
|
+
export { o as MapImportError, y as parseLDtk };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type TileMapData } from './index.js';
|
|
2
|
+
/** One exact-texel quad in map painter order. Axes include scale, rotation and hexagonal turns. */
|
|
3
|
+
export interface TileQuad {
|
|
4
|
+
readonly source: string;
|
|
5
|
+
readonly image: boolean;
|
|
6
|
+
readonly layer: string;
|
|
7
|
+
readonly origin: readonly [number, number];
|
|
8
|
+
readonly axisX: readonly [number, number];
|
|
9
|
+
readonly axisY: readonly [number, number];
|
|
10
|
+
readonly texel: readonly [number, number];
|
|
11
|
+
readonly size: readonly [number, number];
|
|
12
|
+
/** Texture flips: horizontal 1, vertical 2, diagonal 4. */
|
|
13
|
+
readonly flags: number;
|
|
14
|
+
readonly tint: readonly number[];
|
|
15
|
+
}
|
|
16
|
+
/** Settings for building ordered map quads. No view builds the whole map. */
|
|
17
|
+
export interface TileQuadOptions {
|
|
18
|
+
readonly time?: number;
|
|
19
|
+
readonly mapLayers?: readonly string[];
|
|
20
|
+
/** Bounds in map pixels before each layer's offset and parallax. */
|
|
21
|
+
readonly view?: readonly [number, number, number, number];
|
|
22
|
+
readonly camera?: {
|
|
23
|
+
readonly x: number;
|
|
24
|
+
readonly y: number;
|
|
25
|
+
};
|
|
26
|
+
readonly parallax?: boolean;
|
|
27
|
+
/** Dimensions of image sources whose metadata omits its size. */
|
|
28
|
+
readonly imageSize?: (source: string) => readonly [number, number] | undefined;
|
|
29
|
+
}
|
|
30
|
+
/** Build tile cells, tile objects and image layers together, preserving map, cell and object order. */
|
|
31
|
+
export declare function buildMapQuads(map: TileMapData, options?: TileQuadOptions): TileQuad[];
|