UNPKG

kaplay-plugin-tiled

Version:
257 lines (256 loc) 6.3 kB
//#region src/utils.ts var e = 2147483648, t = 1073741824, n = 536870912, r = t | -1610612736; function i(e, t) { if (typeof t != "string") return t; let n = e.getAsset(t); if (!n) throw Error(`Tiled map asset "${t}" is not loaded.`); if (!n.data) throw Error(`Tiled map asset "${t}" is not ready yet.`); return n.data; } function a(e) { let t = {}; return e?.forEach((e) => { t[e.name] = e.value; }), t; } function o(e) { return e.type === "objectgroup"; } function s(e) { return e.type === "tilelayer"; } function c(e) { return { data: e.data, height: e.height, name: e.name, opacity: e.opacity ?? 1, type: "tilelayer", visible: e.visible ?? !0, width: e.width, x: e.x ?? 0, y: e.y ?? 0, zIndex: 0 }; } function l(e) { return { name: e.name, objects: e.objects, type: "objectgroup", visible: e.visible ?? !0, zIndex: 0 }; } function u(i) { return i === 0 ? null : { gid: (i & ~r) >>> 0, flip: { diagonal: (i & n) !== 0, horizontal: (i & e) !== 0, vertical: (i & t) !== 0 } }; } function d(e) { return e.diagonal ? e.horizontal && e.vertical ? { angle: 270, flipX: !0, flipY: !1 } : e.horizontal ? { angle: 270, flipX: !1, flipY: !1 } : e.vertical ? { angle: 90, flipX: !1, flipY: !1 } : { angle: 270, flipX: !1, flipY: !0 } : { angle: 0, flipX: e.horizontal, flipY: e.vertical }; } function f(e, t, n) { let r = n - t.tileset.firstGid, i = r % t.tileset.columns, a = Math.floor(r / t.tileset.columns), o = t.tileset.margin + i * (t.tileset.tileWidth + t.tileset.spacing), s = t.tileset.margin + a * (t.tileset.tileHeight + t.tileset.spacing); return e.quad(o / t.tileset.imageWidth, s / t.tileset.imageHeight, t.tileset.tileWidth / t.tileset.imageWidth, t.tileset.tileHeight / t.tileset.imageHeight); } function p(e) { return e.layerNames?.length ? new Set(e.layerNames) : null; } function m(e, t) { return e.tileset.tiles[t]; } function h(e, t) { return e ? Object.entries(e).every(([e, n]) => t[e] === n) : !0; } function g(e, t) { return e.gid !== void 0 && e.gid !== t.gid || e.tileId !== void 0 && e.tileId !== t.tileId || e.layer !== void 0 && e.layer !== t.layer ? !1 : h(e.properties, t.properties); } function _(e, t) { return e.layer !== void 0 && e.layer !== t.layer || e.name !== void 0 && e.name !== t.name || e.type !== void 0 && e.type !== t.type ? !1 : h(e.properties, t.properties); } function v(e, t, n, r) { return e.add([ e.pos(0, 0), e.z(t.zIndex), { draw: () => { t.data.forEach((i, a) => { let o = u(i); if (!o) return; let s = a % t.width, c = Math.floor(a / t.width), l = d(o.flip); e.drawSprite({ anchor: "center", angle: l.angle, flipX: l.flipX, flipY: l.flipY, opacity: t.opacity, pos: e.vec2((s + t.x) * n.tileWidth + n.tileset.tileWidth / 2, (c + t.y) * n.tileHeight + n.tileset.tileHeight / 2), quad: f(e, n, o.gid), sprite: r }); }); }, id: "tiled-layer" } ]); } function y(e, t, n, r) { let i = r.tiles; if (!i?.length) return []; let a = []; return t.data.forEach((r, o) => { let s = u(r); if (!s) return; let c = m(n, s.gid), l = o % t.width, d = Math.floor(o / t.width), f = { flip: s.flip, gid: s.gid, height: n.tileHeight, layer: t.name, tileHeight: n.tileHeight, tileWidth: n.tileWidth, tileX: l + t.x, tileY: d + t.y, width: n.tileWidth, x: (l + t.x) * n.tileWidth, y: (d + t.y) * n.tileHeight, properties: c?.properties ?? {}, tileId: c?.tileId ?? s.gid - n.tileset.firstGid }; i.forEach((n) => { g(n.match, f) && a.push(e.add([ e.pos(f.x, f.y), e.anchor("topleft"), e.z(t.zIndex), ...n.comps(f) ])); }); }), a; } function b(e, t, n) { let r = n.objects; if (!r?.length) return []; let i = []; return t.visible && t.objects.forEach((n) => { if (!n.visible) return; let o = { height: n.height, id: n.id, layer: t.name, name: n.name, width: n.width, x: n.x, y: n.y, point: n.point ?? !1, properties: a(n.properties), rotation: n.rotation, type: n.type }; r.forEach((n) => { _(n.match, o) && i.push(e.add([ e.pos(o.x, o.y), e.anchor("topleft"), e.z(t.zIndex), ...n.comps(o) ])); }); }), i; } function x(e) { if (e.orientation !== "orthogonal") throw Error(`Unsupported Tiled orientation "${e.orientation}". Expected "orthogonal".`); if (e.infinite) throw Error("Infinite Tiled maps are not supported."); if (e.tilesets.length !== 1) throw Error("Exactly one Tiled tileset is required."); let [t] = e.tilesets; if (t.columns <= 0) throw Error("Tiled tileset columns must be greater than 0."); let n = e.layers.map((e, t) => { if (s(e)) { if (e.data.length !== e.width * e.height) throw Error(`Layer "${e.name}" data length does not match its dimensions.`); return { ...c(e), zIndex: t }; } if (o(e)) return { ...l(e), zIndex: t }; throw Error("Unsupported Tiled layer type."); }); return { height: e.height, layers: n, orientation: "orthogonal", tileHeight: e.tileheight, tileWidth: e.tilewidth, tileset: { columns: t.columns, firstGid: t.firstgid, image: t.image, imageHeight: t.imageheight, imageWidth: t.imagewidth, lastGid: t.firstgid + t.tilecount - 1, margin: t.margin ?? 0, name: t.name, spacing: t.spacing ?? 0, tileCount: t.tilecount, tileHeight: t.tileheight, tiles: Object.fromEntries((t.tiles ?? []).map((e) => [e.id + t.firstgid, { gid: e.id + t.firstgid, properties: a(e.properties), tileId: e.id }])), tileWidth: t.tilewidth }, width: e.width }; } //#endregion //#region src/plugin.ts function S(e, t, n) { let r = x(i(e, t)), a = p(n); r.layers.forEach((t) => { if (t.visible && !(a && !a.has(t.name))) { if (t.type === "tilelayer") { t.data.forEach((e) => { let t = u(e); if (t && (t.gid < r.tileset.firstGid || t.gid > r.tileset.lastGid)) throw Error(`Tile gid ${String(e)} is outside the supported tileset range.`); }), v(e, t, r, n.sprite), y(e, t, r, n); return; } b(e, t, n); } }); } function C(e) { return { addTiledMap: (t, n) => { S(e, t, n); } }; } //#endregion export { C as tiledPlugin }; //# sourceMappingURL=plugin.mjs.map