@margoengine/tilemap 0.0.0-stage → 1.0.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.
@@ -0,0 +1,3 @@
1
+ import { type Obj } from './import.js';
2
+ /** Convert standard Tiled XML maps, tilesets and templates to the JSON field model before semantic import. */
3
+ export declare function tiledXML(input: string | Uint8Array): Obj;
@@ -0,0 +1,86 @@
1
+ import { type Properties, type TileMapData } from './index.js';
2
+ export { MapImportError, type MapImportErrorCode } from './import.js';
3
+ /** Options of `parseTiled`. */
4
+ export interface TiledOptions {
5
+ /** Load a tileset or template. Paths are relative to baseUrl, including references inside external files. */
6
+ readonly resolve?: (source: string, signal?: AbortSignal) => Promise<string | Uint8Array | object>;
7
+ /** Map document path or URL, used to resolve relative resources. Default empty. */
8
+ readonly baseUrl?: string;
9
+ /** Called for unknown layer types. Supported Tiled features are never skipped. */
10
+ readonly onWarning?: (message: string) => void;
11
+ readonly signal?: AbortSignal;
12
+ }
13
+ /**
14
+ * Read a Tiled map saved as JSON (`.tmj`) into `TileMapData`. Takes the text, UTF-8 bytes or the parsed object;
15
+ * orthogonal maps only. Throws `MapImportError` (coded, the message names the fix) for malformed, truncated, oversized
16
+ * or unsupported input.
17
+ */
18
+ export declare function parseTiled(input: string | Uint8Array | object, options?: TiledOptions): Promise<TileMapData>;
19
+ /** The parts of the `@margoengine/loaders` `LoadedTMX` that `fromTMX` reads. A `LoadedTMX` fits as it is. */
20
+ export interface TMXMapLike {
21
+ readonly width: number;
22
+ readonly height: number;
23
+ readonly tilewidth: number;
24
+ readonly tileheight: number;
25
+ readonly tilesets: readonly {
26
+ readonly name?: string;
27
+ readonly tilewidth: number;
28
+ readonly tileheight: number;
29
+ readonly spacing: number;
30
+ readonly margin: number;
31
+ readonly columns: number;
32
+ readonly tilecount: number;
33
+ readonly image?: {
34
+ readonly path: string;
35
+ readonly width: number;
36
+ readonly height: number;
37
+ };
38
+ readonly properties: Properties;
39
+ }[];
40
+ readonly tilesetRefs: readonly {
41
+ readonly firstgid: number;
42
+ readonly tileset: number;
43
+ }[];
44
+ readonly layers: readonly ({
45
+ readonly type: 'tile';
46
+ readonly name: string;
47
+ readonly id?: number;
48
+ readonly visible: boolean;
49
+ readonly opacity: number;
50
+ readonly offsetx: number;
51
+ readonly offsety: number;
52
+ readonly properties: Properties;
53
+ readonly x: number;
54
+ readonly y: number;
55
+ readonly width: number;
56
+ readonly height: number;
57
+ readonly gids: Uint32Array;
58
+ readonly flips: Uint8Array;
59
+ } | {
60
+ readonly type: 'object';
61
+ readonly name: string;
62
+ readonly id?: number;
63
+ readonly visible: boolean;
64
+ readonly opacity: number;
65
+ readonly offsetx: number;
66
+ readonly offsety: number;
67
+ readonly properties: Properties;
68
+ readonly objects: readonly {
69
+ readonly id?: number;
70
+ readonly name: string;
71
+ readonly type: string;
72
+ readonly x: number;
73
+ readonly y: number;
74
+ readonly width: number;
75
+ readonly height: number;
76
+ readonly rotation: number;
77
+ readonly visible: boolean;
78
+ readonly gid?: number;
79
+ readonly flips?: number;
80
+ readonly properties: Properties;
81
+ }[];
82
+ })[];
83
+ readonly properties: Properties;
84
+ }
85
+ /** Turn a map from the loaders' `parseTMX` into `TileMapData`, so TMX and Tiled JSON share one format and one parser. */
86
+ export declare function fromTMX(map: TMXMapLike): TileMapData;
package/dist/tiled.js ADDED
@@ -0,0 +1,541 @@
1
+ import { i as e, n as t, t as n, v as r } from "./src-CJvCfCkX.js";
2
+ import { a as i, c as a, i as o, l as s, n as c, o as l, r as u, s as d, t as f, u as p } from "./import-DsL40A38.js";
3
+ //#region src/tiled.ts
4
+ var m = "Tiled", h = (e, t) => {
5
+ throw new c(m, e, t);
6
+ };
7
+ function g(e, t) {
8
+ if (/^[a-z][\w+.-]*:/i.test(t)) return t;
9
+ if (/^[a-z][\w+.-]*:/i.test(e)) return new URL(t, e).href;
10
+ if (t.startsWith("/")) return t;
11
+ let n = [];
12
+ for (let r of (e.slice(0, e.lastIndexOf("/") + 1) + t).split("/")) r && r !== "." && (r === ".." && n.length && n[n.length - 1] !== ".." ? n.pop() : n.push(r));
13
+ return n.join("/");
14
+ }
15
+ function _(e, t) {
16
+ if (!e) return t;
17
+ if (!t) return e;
18
+ let n = r(e), i = r(t);
19
+ if (!n || !i) return h("syntax", "invalid layer tint; use Tiled hexadecimal colours.");
20
+ let a = n.map((e, t) => Math.round(e * i[t] * 255).toString(16).padStart(2, "0"));
21
+ return "#" + a[3] + a[0] + a[1] + a[2];
22
+ }
23
+ async function v(e) {
24
+ let t = e instanceof Uint8Array ? new TextDecoder().decode(e) : e;
25
+ if (typeof t == "string" && t.trimStart().startsWith("<") || e instanceof Uint8Array && (e[0] === 255 && e[1] === 254 || e[0] === 254 && e[1] === 255)) try {
26
+ return (await import("./tiled-xml-Dp4Ez81m.js")).tiledXML(e);
27
+ } catch (e) {
28
+ if (e instanceof c) throw e;
29
+ return h("syntax", `invalid Tiled XML (${e instanceof Error ? e.message : String(e)}); re-save the file.`);
30
+ }
31
+ return d(m, e);
32
+ }
33
+ async function y(e, t) {
34
+ if (p(m, t.signal), !t.resolve) return h("missing", `resource "${e}" is external; pass options.resolve to load it by path.`);
35
+ let n = t.signal, r;
36
+ try {
37
+ let i = t.resolve(e, n), a = n ? await Promise.race([i, new Promise((e, t) => {
38
+ r = () => t(new c(m, "aborted", "the import was aborted by its signal.")), n.addEventListener("abort", r, { once: !0 }), n.aborted && r();
39
+ })]) : await i;
40
+ return p(m, n), await v(a);
41
+ } finally {
42
+ r && n.removeEventListener("abort", r);
43
+ }
44
+ }
45
+ function b(e) {
46
+ let t = s(e, "halign") ?? "left", n = s(e, "valign") ?? "top";
47
+ return ![
48
+ "left",
49
+ "center",
50
+ "right",
51
+ "justify"
52
+ ].includes(t) || ![
53
+ "top",
54
+ "center",
55
+ "bottom"
56
+ ].includes(n) || !(l(e, "pixelsize", 16) > 0) ? h("syntax", "text style is invalid; use a positive font size and Tiled alignments.") : {
57
+ text: s(e, "text") ?? "",
58
+ fontFamily: s(e, "fontfamily") ?? "sans-serif",
59
+ pixelSize: l(e, "pixelsize", 16),
60
+ color: s(e, "color") ?? "#000000",
61
+ bold: e.bold === !0,
62
+ italic: e.italic === !0,
63
+ underline: e.underline === !0,
64
+ strikeout: e.strikeout === !0,
65
+ kerning: e.kerning !== !1,
66
+ wrap: e.wrap === !0,
67
+ horizontalAlign: t,
68
+ verticalAlign: n
69
+ };
70
+ }
71
+ function x(r) {
72
+ return i(r, "wangsets").filter(o).map((a) => {
73
+ let c = i(a, "colors").filter(o).map((e) => ({
74
+ name: s(e, "name") ?? "",
75
+ color: s(e, "color") ?? "#000000",
76
+ tile: l(e, "tile", -1),
77
+ probability: l(e, "probability", 1),
78
+ properties: S(e.properties)
79
+ })), u = i(a, "wangtiles").filter(o).map((a) => {
80
+ let s = i(a, "wangid");
81
+ if (s.length !== 8 || s.some((e) => typeof e != "number" || !Number.isInteger(e) || e < 0 || e > c.length)) return h("syntax", "a Wang tile needs eight defined colour ids; re-save the tileset.");
82
+ let u = l(a, "tileid"), d = l(i(r, "tiles").filter(o).find((e) => l(e, "id") === u) ?? {}, "probability", 1);
83
+ return !Number.isInteger(u) || u < 0 || d < 0 ? h("syntax", "a Wang tile has an invalid id or probability; re-save the tileset.") : {
84
+ tile: u,
85
+ colors: s,
86
+ weight: d,
87
+ flags: (a.hflip ? t : 0) | (a.vflip ? e : 0) | (a.dflip ? n : 0)
88
+ };
89
+ }), d = s(a, "type") ?? (u.some((e) => e.colors.some((e, t) => t % 2 == 0 && e)) ? "mixed" : "corner");
90
+ return [
91
+ "edge",
92
+ "corner",
93
+ "mixed"
94
+ ].includes(d) ? {
95
+ mode: "wang",
96
+ name: s(a, "name") ?? "",
97
+ type: d,
98
+ colors: c,
99
+ tiles: u,
100
+ properties: S(a.properties)
101
+ } : h("syntax", "unknown Wang set type; use edge, corner or mixed.");
102
+ });
103
+ }
104
+ function S(e) {
105
+ let t = {};
106
+ if (Array.isArray(e)) for (let n of e) o(n) && typeof n.name == "string" && a(t, n.name, n.value);
107
+ else if (o(e)) for (let n of Object.keys(e)) a(t, n, e[n]);
108
+ return Object.keys(t).length ? t : void 0;
109
+ }
110
+ var C = (e) => e.map((e) => o(e) ? [l(e, "x"), l(e, "y")] : [0, 0]);
111
+ function w(e) {
112
+ let t = i(e, "polygon");
113
+ if (t.length) return {
114
+ kind: "polygon",
115
+ x: 0,
116
+ y: 0,
117
+ points: C(t)
118
+ };
119
+ let n = i(e, "polyline");
120
+ if (n.length) return {
121
+ kind: "polyline",
122
+ x: 0,
123
+ y: 0,
124
+ points: C(n)
125
+ };
126
+ if (e.point === !0) return {
127
+ kind: "point",
128
+ x: 0,
129
+ y: 0
130
+ };
131
+ let r = {
132
+ x: 0,
133
+ y: 0,
134
+ width: l(e, "width"),
135
+ height: l(e, "height")
136
+ };
137
+ return e.ellipse === !0 ? {
138
+ kind: "ellipse",
139
+ ...r
140
+ } : {
141
+ kind: "rect",
142
+ ...r
143
+ };
144
+ }
145
+ function T(e) {
146
+ let t = {
147
+ ...w(e),
148
+ x: l(e, "x"),
149
+ y: l(e, "y")
150
+ }, n = l(e, "rotation");
151
+ return n && t.kind !== "point" ? {
152
+ ...t,
153
+ rotation: n
154
+ } : t;
155
+ }
156
+ function E(e, t = !1) {
157
+ let n = typeof e.gid == "number" ? u(e.gid, t) : void 0, r = s(e, "class") || s(e, "type"), i = l(e, "rotation"), a = s(e, "name"), c = S(e.properties);
158
+ return {
159
+ id: l(e, "id"),
160
+ ...a ? { name: a } : {},
161
+ ...r ? { type: r } : {},
162
+ x: l(e, "x"),
163
+ y: l(e, "y"),
164
+ ...typeof e.width == "number" ? { width: l(e, "width") } : {},
165
+ ...typeof e.height == "number" ? { height: l(e, "height") } : {},
166
+ ...o(e.text) ? { text: b(e.text) } : {},
167
+ ...i ? { rotation: i } : {},
168
+ ...e.visible === !1 ? { visible: !1 } : {},
169
+ ...n === void 0 ? { shape: w(e) } : { gid: n },
170
+ ...c ? { properties: c } : {}
171
+ };
172
+ }
173
+ function D(e) {
174
+ let t = s(e, "class") || s(e, "type"), n = S(e.properties), r = i(e, "animation").flatMap((e) => o(e) ? [{
175
+ tileId: l(e, "tileid"),
176
+ duration: l(e, "duration")
177
+ }] : []), a = o(e.objectgroup) ? i(e.objectgroup, "objects").filter(o) : [], c = s(e, "image");
178
+ return {
179
+ id: l(e, "id"),
180
+ ...t ? { type: t } : {},
181
+ ...n ? { properties: n } : {},
182
+ ...r.length ? { animation: r } : {},
183
+ ...a.length ? { collision: a.map(T) } : {},
184
+ ...c ? { image: {
185
+ source: c,
186
+ width: l(e, "imagewidth"),
187
+ height: l(e, "imageheight")
188
+ } } : {}
189
+ };
190
+ }
191
+ async function O(e, t) {
192
+ let n = e, r = s(e, "source");
193
+ r !== void 0 && (n = await y(g(t.baseUrl ?? "", r), t));
194
+ let a = r === void 0 ? t.baseUrl ?? "" : g(t.baseUrl ?? "", r);
195
+ if (!(l(n, "tilewidth") > 0 && l(n, "tileheight") > 0)) return h("syntax", "tileset tilewidth and tileheight must be positive; re-save the tileset.");
196
+ let c = i(n, "tiles").filter(o).map((e) => D({
197
+ ...e,
198
+ ...typeof e.image == "string" ? { image: g(a, e.image) } : {}
199
+ })), u = s(n, "image"), d = l(n, "spacing"), f = l(n, "margin"), p = o(n.tileoffset) ? [l(n.tileoffset, "x"), l(n.tileoffset, "y")] : void 0, m = S(n.properties);
200
+ return {
201
+ name: s(n, "name") ?? "",
202
+ firstGid: l(e, "firstgid", 1),
203
+ tileWidth: l(n, "tilewidth"),
204
+ tileHeight: l(n, "tileheight"),
205
+ tileCount: Math.max(l(n, "tilecount", c.length), ...c.map((e) => e.id + 1)),
206
+ columns: l(n, "columns"),
207
+ ...typeof n.objectalignment == "string" ? { objectAlignment: n.objectalignment } : {},
208
+ ...d ? { spacing: d } : {},
209
+ ...f ? { margin: f } : {},
210
+ ...p && (p[0] || p[1]) ? { tileOffset: p } : {},
211
+ ...u ? { image: {
212
+ source: g(a, u),
213
+ width: l(n, "imagewidth"),
214
+ height: l(n, "imageheight")
215
+ } } : {},
216
+ ...c.length ? { tiles: c } : {},
217
+ ...Array.isArray(n.wangsets) ? { wangSets: x(n) } : {},
218
+ ...m ? { properties: m } : {}
219
+ };
220
+ }
221
+ async function k(e, t, n) {
222
+ if (typeof DecompressionStream > "u") return h("unsupported", "this runtime has no DecompressionStream; save the layers uncompressed or as CSV.");
223
+ let r = new Blob([e]).stream().pipeThrough(new DecompressionStream(t)).getReader(), i = [], a = 0;
224
+ try {
225
+ for (;;) {
226
+ let { done: e, value: t } = await r.read();
227
+ if (e) break;
228
+ if (a += t.length, a > n) return h("limit", `a compressed layer expands past its ${n} byte size; the file is damaged.`);
229
+ i.push(t);
230
+ }
231
+ } catch (e) {
232
+ if (e instanceof c) throw e;
233
+ return h("syntax", `the compressed layer data cannot be inflated (${t}); re-save the map.`);
234
+ }
235
+ let o = new Uint8Array(a), s = 0;
236
+ for (let e of i) o.set(e, s), s += e.length;
237
+ return o;
238
+ }
239
+ async function A(e, t, n, r, i = !1) {
240
+ let a = s(e, "name") ?? "", o = new Uint32Array(n);
241
+ if (Array.isArray(t)) {
242
+ if (t.length !== n) return h("truncated", `layer "${a}" has ${t.length} tiles for ${n} cells; re-save the map.`);
243
+ for (let e = 0; e < n; e++) {
244
+ let n = t[e];
245
+ if (typeof n != "number" || !Number.isInteger(n) || n < 0 || n > 4294967295) return h("syntax", `layer "${a}" tile ${e} is ${String(n)}, not an unsigned 32 bit integer.`);
246
+ o[e] = u(n, i);
247
+ }
248
+ return o;
249
+ }
250
+ if (typeof t != "string") return h("syntax", `layer "${a}" has no tile data; expected an array or a base64 string.`);
251
+ if (e.encoding !== "base64") return h("unsupported", `layer "${a}" is encoded as "${String(e.encoding)}"; use CSV or base64.`);
252
+ let c;
253
+ try {
254
+ c = Uint8Array.from(atob(t), (e) => e.charCodeAt(0));
255
+ } catch {
256
+ return h("syntax", `layer "${a}" holds invalid base64; re-save the map.`);
257
+ }
258
+ let l = s(e, "compression") ?? "";
259
+ if (l === "zlib") c = await k(c, "deflate", n * 4);
260
+ else if (l === "gzip") c = await k(c, "gzip", n * 4);
261
+ else if (l) return h("unsupported", `layer "${a}" uses ${l} compression; save it as zlib, gzip or uncompressed.`);
262
+ if (p(m, r), c.length !== n * 4) return h(c.length < n * 4 ? "truncated" : "syntax", `layer "${a}" decodes to ${c.length} bytes, expected ${n * 4}; re-save the map.`);
263
+ let d = new DataView(c.buffer, c.byteOffset, c.byteLength);
264
+ for (let e = 0; e < n; e++) o[e] = u(d.getUint32(e * 4, !0), i);
265
+ return o;
266
+ }
267
+ async function j(e, t = {}) {
268
+ p(m, t.signal);
269
+ let n = await v(e), r = t.onWarning ?? (() => {});
270
+ if (!Array.isArray(n.layers) || !Array.isArray(n.tilesets)) return h("syntax", "this is not a Tiled map: \"layers\" and \"tilesets\" are missing; pass the .tmj / .json file contents.");
271
+ let a = s(n, "orientation") ?? "orthogonal";
272
+ if (a !== "orthogonal" && a !== "isometric" && a !== "staggered" && a !== "hexagonal") return h("unsupported", `the map is ${a}; orthogonal, isometric, staggered and hexagonal maps are read, change the orientation in Tiled.`);
273
+ let c = a === "hexagonal";
274
+ if (a !== "orthogonal" && n.infinite === !0) return h("unsupported", `the map is an infinite ${a} map; untick Infinite in Tiled's map properties.`);
275
+ let d = s(n, "staggeraxis"), b = s(n, "staggerindex"), x = l(n, "hexsidelength");
276
+ if ((d ?? "y") !== "x" && (d ?? "y") !== "y") return h("syntax", `staggeraxis is "${d}"; Tiled writes x or y.`);
277
+ if ((b ?? "odd") !== "odd" && (b ?? "odd") !== "even") return h("syntax", `staggerindex is "${b}"; Tiled writes odd or even.`);
278
+ let C = a === "orthogonal" ? {} : a === "isometric" ? { orientation: a } : {
279
+ orientation: a,
280
+ staggerAxis: d ?? "y",
281
+ staggerIndex: b ?? "odd",
282
+ ...c ? { hexSideLength: Math.max(0, x) } : {}
283
+ }, w = l(n, "tilewidth"), T = l(n, "tileheight");
284
+ if (!(w > 0 && T > 0)) return h("syntax", "tilewidth and tileheight must be positive numbers.");
285
+ let D = l(n, "width"), k = l(n, "height");
286
+ if (D * k > 16777216 && n.infinite !== !0) return h("limit", `the map is ${D} x ${k} tiles; the limit is ${f} cells per layer.`);
287
+ let j = [], M = /* @__PURE__ */ new Map();
288
+ for (let e of n.tilesets) if (p(m, t.signal), o(e)) {
289
+ let n = await O(e, t);
290
+ j.push(n), typeof e.source == "string" && M.set(g(t.baseUrl ?? "", e.source), n);
291
+ }
292
+ let N = /* @__PURE__ */ new Map(), P = async (e, n = t.baseUrl ?? "", r = []) => {
293
+ if (typeof e.template != "string") return e;
294
+ let i = g(n, e.template);
295
+ if (r.includes(i) || r.length >= 32) return h("limit", `template cycle or nesting past 32 at "${i}"; detach the cyclic reference.`);
296
+ let a = N.get(i);
297
+ a || (a = y(i, t), N.set(i, a));
298
+ let s = await a;
299
+ if (!o(s.object)) return h("syntax", `template "${i}" needs an object; re-save it in Tiled.`);
300
+ let d = await P(s.object, i, [...r, i]);
301
+ if (e.gid === void 0 && typeof s.object.gid == "number" && o(s.tileset)) {
302
+ let e = s.tileset;
303
+ if (typeof e.source != "string") return h("syntax", `template "${i}" needs an external tileset reference.`);
304
+ let n = g(i, e.source), r = M.get(n);
305
+ if (!r) {
306
+ let i = Math.max(1, ...j.map((e) => e.firstGid + e.tileCount));
307
+ r = await O({
308
+ ...e,
309
+ source: n,
310
+ firstgid: i
311
+ }, {
312
+ ...t,
313
+ baseUrl: ""
314
+ }), j.push(r), M.set(n, r);
315
+ }
316
+ let a = u(s.object.gid, c), o = (a & 268435455) - l(e, "firstgid", 1);
317
+ if (o < 0 || o >= r.tileCount) return h("syntax", `template "${i}" tile id is outside its tileset; re-save the template.`);
318
+ d = {
319
+ ...d,
320
+ gid: (a & 4026531840 | r.firstGid + o) >>> 0
321
+ };
322
+ }
323
+ let f = {
324
+ ...d,
325
+ ...e,
326
+ template: void 0,
327
+ properties: {
328
+ ...S(d.properties),
329
+ ...S(e.properties)
330
+ },
331
+ ...o(d.text) || o(e.text) ? { text: {
332
+ ...o(d.text) ? d.text : {},
333
+ ...o(e.text) ? e.text : {}
334
+ } } : {}
335
+ };
336
+ return e.type !== void 0 && e.class === void 0 && delete f.class, f;
337
+ }, F = [], I = D, L = k, R = async (e, a) => {
338
+ for (let u of e) {
339
+ if (p(m, t.signal), !o(u)) continue;
340
+ let e = {
341
+ ox: a.ox + l(u, "offsetx"),
342
+ oy: a.oy + l(u, "offsety"),
343
+ opacity: a.opacity * l(u, "opacity", 1),
344
+ visible: a.visible && u.visible !== !1,
345
+ px: a.px * l(u, "parallaxx", 1),
346
+ py: a.py * l(u, "parallaxy", 1),
347
+ tint: _(a.tint, s(u, "tintcolor"))
348
+ }, d = s(u, "type");
349
+ if (d === "group") {
350
+ await R(i(u, "layers"), e);
351
+ continue;
352
+ }
353
+ if (d !== "tilelayer" && d !== "objectgroup" && d !== "imagelayer") {
354
+ r(`layer "${s(u, "name") ?? ""}" is a ${d ?? "layer of unknown type"}; skipped`);
355
+ continue;
356
+ }
357
+ let v = e.tint, y = S(u.properties), b = {
358
+ name: s(u, "name") ?? "",
359
+ ...typeof u.id == "number" ? { id: u.id } : {},
360
+ ...e.opacity === 1 ? {} : { opacity: e.opacity },
361
+ ...e.visible ? {} : { visible: !1 },
362
+ ...e.px !== 1 || e.py !== 1 ? { parallax: [e.px, e.py] } : {},
363
+ ...v ? { tint: v } : {},
364
+ ...y ? { properties: y } : {}
365
+ }, x = (t, n) => e.ox + t || e.oy + n ? { offset: [e.ox + t, e.oy + n] } : {};
366
+ if (d === "imagelayer") {
367
+ let e = s(u, "image");
368
+ if (!e) return h("missing", `image layer "${b.name}" needs its image path.`);
369
+ F.push({
370
+ type: "image",
371
+ ...b,
372
+ ...x(l(u, "x"), l(u, "y")),
373
+ image: {
374
+ source: g(t.baseUrl ?? "", e),
375
+ ...l(u, "imagewidth") > 0 ? { width: l(u, "imagewidth") } : {},
376
+ ...l(u, "imageheight") > 0 ? { height: l(u, "imageheight") } : {}
377
+ },
378
+ ...u.repeatx === !0 ? { repeatX: !0 } : {},
379
+ ...u.repeaty === !0 ? { repeatY: !0 } : {}
380
+ });
381
+ continue;
382
+ }
383
+ if (d === "objectgroup") {
384
+ let e = [];
385
+ for (let t of i(u, "objects").filter(o)) e.push(E(await P(t), c));
386
+ F.push({
387
+ type: "object",
388
+ ...b,
389
+ ...x(0, 0),
390
+ ...u.draworder === "index" ? { drawOrder: "index" } : {},
391
+ objects: e
392
+ });
393
+ continue;
394
+ }
395
+ if (n.infinite === !0 && Array.isArray(u.chunks)) {
396
+ let e = u.chunks.filter(o);
397
+ if (!e.length) {
398
+ F.push({
399
+ type: "tile",
400
+ ...b,
401
+ ...x(0, 0),
402
+ width: 0,
403
+ height: 0,
404
+ data: /* @__PURE__ */ new Uint32Array()
405
+ });
406
+ continue;
407
+ }
408
+ let n = Infinity, r = Infinity, i = -Infinity, a = -Infinity;
409
+ for (let t of e) n = Math.min(n, l(t, "x")), r = Math.min(r, l(t, "y")), i = Math.max(i, l(t, "x") + l(t, "width")), a = Math.max(a, l(t, "y") + l(t, "height"));
410
+ let s = i - n, c = a - r;
411
+ if (s * c > 16777216) return h("limit", `layer "${b.name}" spans ${s} x ${c} tiles; the limit is ${f} cells. Split the map or use fewer chunks.`);
412
+ let d = new Uint32Array(s * c);
413
+ for (let i of e) {
414
+ let e = l(i, "width"), a = l(i, "height"), o = await A(u, i.data, e * a, t.signal);
415
+ for (let t = 0; t < a; t++) d.set(o.subarray(t * e, t * e + e), (l(i, "y") - r + t) * s + l(i, "x") - n);
416
+ }
417
+ I = Math.max(I, i), L = Math.max(L, a), F.push({
418
+ type: "tile",
419
+ ...b,
420
+ ...x(n * w, r * T),
421
+ width: s,
422
+ height: c,
423
+ data: d
424
+ });
425
+ continue;
426
+ }
427
+ let C = l(u, "width", D), O = l(u, "height", k);
428
+ if (C * O > 16777216) return h("limit", `layer "${b.name}" is ${C} x ${O} tiles; the limit is ${f} cells.`);
429
+ F.push({
430
+ type: "tile",
431
+ ...b,
432
+ ...x(0, 0),
433
+ width: C,
434
+ height: O,
435
+ data: await A(u, u.data, C * O, t.signal, c)
436
+ });
437
+ }
438
+ };
439
+ await R(n.layers, {
440
+ ox: 0,
441
+ oy: 0,
442
+ opacity: 1,
443
+ visible: !0,
444
+ px: 1,
445
+ py: 1
446
+ });
447
+ let z = s(n, "backgroundcolor"), B = S(n.properties);
448
+ return {
449
+ width: I,
450
+ height: L,
451
+ tileWidth: w,
452
+ tileHeight: T,
453
+ ...C,
454
+ ...typeof n.renderorder == "string" ? { renderOrder: n.renderorder } : {},
455
+ tilesets: j,
456
+ layers: F,
457
+ ...z ? { backgroundColor: z } : {},
458
+ ...B ? { properties: B } : {}
459
+ };
460
+ }
461
+ var M = (r) => (r & 1 ? t : 0) | (r & 2 ? e : 0) | (r & 4 ? n : 0), N = (e) => Object.keys(e).length ? e : void 0;
462
+ function P(e) {
463
+ let t = e.tilesetRefs.map((t) => {
464
+ let n = e.tilesets[t.tileset], r = N(n.properties);
465
+ return {
466
+ name: n.name ?? "",
467
+ firstGid: t.firstgid,
468
+ tileWidth: n.tilewidth,
469
+ tileHeight: n.tileheight,
470
+ tileCount: n.tilecount,
471
+ columns: n.columns,
472
+ ...n.spacing ? { spacing: n.spacing } : {},
473
+ ...n.margin ? { margin: n.margin } : {},
474
+ ...n.image ? { image: {
475
+ source: n.image.path,
476
+ width: n.image.width,
477
+ height: n.image.height
478
+ } } : {},
479
+ ...r ? { properties: r } : {}
480
+ };
481
+ }), n = e.layers.map((t) => {
482
+ let n = N(t.properties), r = {
483
+ name: t.name,
484
+ ...t.id === void 0 ? {} : { id: t.id },
485
+ ...t.opacity === 1 ? {} : { opacity: t.opacity },
486
+ ...t.visible ? {} : { visible: !1 },
487
+ ...n ? { properties: n } : {}
488
+ };
489
+ if (t.type === "object") return {
490
+ type: "object",
491
+ ...r,
492
+ ...t.offsetx || t.offsety ? { offset: [t.offsetx, t.offsety] } : {},
493
+ objects: t.objects.map((e) => {
494
+ let t = N(e.properties);
495
+ return {
496
+ id: e.id ?? 0,
497
+ ...e.name ? { name: e.name } : {},
498
+ ...e.type ? { type: e.type } : {},
499
+ x: e.x,
500
+ y: e.y,
501
+ ...e.rotation ? { rotation: e.rotation } : {},
502
+ ...e.visible ? {} : { visible: !1 },
503
+ ...e.gid === void 0 ? { shape: e.width && e.height ? {
504
+ kind: "rect",
505
+ x: 0,
506
+ y: 0,
507
+ width: e.width,
508
+ height: e.height
509
+ } : {
510
+ kind: "point",
511
+ x: 0,
512
+ y: 0
513
+ } } : { gid: (e.gid | M(e.flips ?? 0)) >>> 0 },
514
+ ...t ? { properties: t } : {}
515
+ };
516
+ })
517
+ };
518
+ let i = new Uint32Array(t.gids.length);
519
+ for (let e = 0; e < i.length; e++) i[e] = (t.gids[e] | M(t.flips[e])) >>> 0;
520
+ let a = t.offsetx + t.x * e.tilewidth, o = t.offsety + t.y * e.tileheight;
521
+ return {
522
+ type: "tile",
523
+ ...r,
524
+ ...a || o ? { offset: [a, o] } : {},
525
+ width: t.width,
526
+ height: t.height,
527
+ data: i
528
+ };
529
+ }), r = N(e.properties);
530
+ return {
531
+ width: e.width,
532
+ height: e.height,
533
+ tileWidth: e.tilewidth,
534
+ tileHeight: e.tileheight,
535
+ tilesets: t,
536
+ layers: n,
537
+ ...r ? { properties: r } : {}
538
+ };
539
+ }
540
+ //#endregion
541
+ export { c as MapImportError, P as fromTMX, j as parseTiled };