@flare-engine/tilemap 0.2.1 → 0.3.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 -21
- package/NOTICE +2 -0
- package/README.md +22 -5
- package/dist/index.d.mts +194 -143
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +194 -143
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +442 -389
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +430 -356
- package/dist/index.mjs.map +1 -1
- package/package.json +19 -11
package/dist/index.mjs
CHANGED
|
@@ -1,380 +1,454 @@
|
|
|
1
|
-
|
|
1
|
+
import { AABB } from "@flare-engine/physics";
|
|
2
|
+
import { SpriteBatch } from "@flare-engine/render";
|
|
3
|
+
//#region src/parse-error.ts
|
|
4
|
+
/** Thrown by parseTiled/parseLDtk when a map uses an unsupported format or feature. */
|
|
5
|
+
var TilemapParseError = class extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "TilemapParseError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/tilemap-data.ts
|
|
13
|
+
/** Low 29 bits of a Tiled GID — Tiled stores flip state in the high 3 bits. */
|
|
14
|
+
const GID_FLIP_MASK = 536870911;
|
|
15
|
+
const FLIP_H = 2147483648;
|
|
16
|
+
const FLIP_V = 1073741824;
|
|
17
|
+
const FLIP_D = 536870912;
|
|
18
|
+
/** Strip the Tiled flip bits from a raw GID, returning the real global tile id. */
|
|
19
|
+
function stripGidFlags(gid) {
|
|
20
|
+
return gid & GID_FLIP_MASK;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Decode the Tiled flip bits encoded in the high bits of a raw GID.
|
|
24
|
+
* Note: `gid & 0x80000000` yields a negative Int32 when the bit is set;
|
|
25
|
+
* compare with `!== 0`, not `> 0`.
|
|
26
|
+
*/
|
|
27
|
+
function decodeGidFlags(gid) {
|
|
28
|
+
return {
|
|
29
|
+
horizontal: (gid & FLIP_H) !== 0,
|
|
30
|
+
vertical: (gid & FLIP_V) !== 0,
|
|
31
|
+
diagonal: (gid & FLIP_D) !== 0
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Create an empty TilemapData. */
|
|
2
35
|
function createTilemapData(width, height, tileWidth, tileHeight) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
36
|
+
return {
|
|
37
|
+
width,
|
|
38
|
+
height,
|
|
39
|
+
tileWidth,
|
|
40
|
+
tileHeight,
|
|
41
|
+
layers: [],
|
|
42
|
+
tilesets: [],
|
|
43
|
+
properties: {}
|
|
44
|
+
};
|
|
12
45
|
}
|
|
46
|
+
/** Get a tile GID at a grid position from a tile layer. Returns 0 if out of bounds. */
|
|
13
47
|
function getTileAt(layer, col, row) {
|
|
14
|
-
|
|
15
|
-
|
|
48
|
+
if (col < 0 || col >= layer.width || row < 0 || row >= layer.height) return 0;
|
|
49
|
+
return layer.tiles[row * layer.width + col] ?? 0;
|
|
16
50
|
}
|
|
51
|
+
/** Get the tileset that owns a given GID. Handles Tiled flip bits in the high bits. */
|
|
17
52
|
function getTilesetForGid(tilesets, gid) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
break;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return result;
|
|
53
|
+
const id = gid & GID_FLIP_MASK;
|
|
54
|
+
let result;
|
|
55
|
+
for (const ts of tilesets) if (ts.firstGid <= id) result = ts;
|
|
56
|
+
else break;
|
|
57
|
+
return result;
|
|
27
58
|
}
|
|
59
|
+
/** Compute the source rect in a tileset image for a given GID. Strips Tiled flip bits. */
|
|
28
60
|
function getTileSourceRect(tileset, gid) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
61
|
+
const localId = (gid & GID_FLIP_MASK) - tileset.firstGid;
|
|
62
|
+
const col = localId % tileset.columns;
|
|
63
|
+
const row = Math.floor(localId / tileset.columns);
|
|
64
|
+
return {
|
|
65
|
+
x: col * tileset.tileWidth,
|
|
66
|
+
y: row * tileset.tileHeight,
|
|
67
|
+
width: tileset.tileWidth,
|
|
68
|
+
height: tileset.tileHeight
|
|
69
|
+
};
|
|
38
70
|
}
|
|
39
|
-
|
|
40
|
-
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/ldtk-parser.ts
|
|
73
|
+
/**
|
|
74
|
+
* Extract every LDtk entity from a parsed map as a flat, spawn-ready list —
|
|
75
|
+
* the LDtk-entity → ECS spawn convention. Walks all object layers; tile layers are skipped.
|
|
76
|
+
*/
|
|
77
|
+
function parseLDtkEntities(map) {
|
|
78
|
+
const entities = [];
|
|
79
|
+
for (const layer of map.layers) {
|
|
80
|
+
if (layer.type !== "object") continue;
|
|
81
|
+
for (const obj of layer.objects) entities.push({
|
|
82
|
+
id: obj.type,
|
|
83
|
+
x: obj.x,
|
|
84
|
+
y: obj.y,
|
|
85
|
+
width: obj.width,
|
|
86
|
+
height: obj.height,
|
|
87
|
+
fields: obj.properties
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return entities;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Parse an LDtk JSON project into an array of TilemapData (one per level).
|
|
94
|
+
* Returns all levels with their tile and entity layers.
|
|
95
|
+
* Throws TilemapParseError for unsupported formats (externalLevels, invalid input).
|
|
96
|
+
*/
|
|
97
|
+
function parseLDtk(json, options = {}) {
|
|
98
|
+
if (!json || typeof json !== "object" || !Array.isArray(json.levels) || !json.defs || !Array.isArray(json.defs.tilesets)) throw new TilemapParseError("parseLDtk: input is not an LDtk project JSON (missing defs.tilesets or levels).");
|
|
99
|
+
const data = json;
|
|
100
|
+
if (data.externalLevels === true) throw new TilemapParseError("parseLDtk: projects with \"Save levels to separate files\" (externalLevels) are not supported. Disable it in LDtk ▸ Project settings and re-save.");
|
|
101
|
+
const isSolid = options.intGridSolid ?? ((v) => v > 0);
|
|
102
|
+
const tilesetDefs = /* @__PURE__ */ new Map();
|
|
103
|
+
for (const ts of data.defs.tilesets) tilesetDefs.set(ts.uid, ts);
|
|
104
|
+
const results = [];
|
|
105
|
+
for (const level of data.levels) {
|
|
106
|
+
const gridSize = data.defaultGridSize;
|
|
107
|
+
const mapWidth = Math.ceil(level.pxWid / gridSize);
|
|
108
|
+
const mapHeight = Math.ceil(level.pxHei / gridSize);
|
|
109
|
+
const map = createTilemapData(mapWidth, mapHeight, gridSize, gridSize);
|
|
110
|
+
if (level.fieldInstances) for (const field of level.fieldInstances) map.properties[field.__identifier] = field.__value;
|
|
111
|
+
const firstGidByUid = /* @__PURE__ */ new Map();
|
|
112
|
+
let nextFirstGid = 1;
|
|
113
|
+
if (level.layerInstances) {
|
|
114
|
+
const usedTileUids = /* @__PURE__ */ new Set();
|
|
115
|
+
for (const layerInst of level.layerInstances) if ((layerInst.__type === "Tiles" || layerInst.__type === "AutoLayer") && layerInst.__tilesetDefUid !== void 0) usedTileUids.add(layerInst.__tilesetDefUid);
|
|
116
|
+
const sortedUids = Array.from(usedTileUids).sort((a, b) => a - b);
|
|
117
|
+
for (const uid of sortedUids) {
|
|
118
|
+
const def = tilesetDefs.get(uid);
|
|
119
|
+
if (!def) continue;
|
|
120
|
+
const columns = def.__cWid;
|
|
121
|
+
const tileCount = def.__cWid * def.__cHei;
|
|
122
|
+
firstGidByUid.set(uid, nextFirstGid);
|
|
123
|
+
const ref = {
|
|
124
|
+
firstGid: nextFirstGid,
|
|
125
|
+
name: def.identifier,
|
|
126
|
+
tileWidth: def.tileGridSize,
|
|
127
|
+
tileHeight: def.tileGridSize,
|
|
128
|
+
columns,
|
|
129
|
+
tileCount,
|
|
130
|
+
image: def.relPath,
|
|
131
|
+
imageWidth: def.pxWid,
|
|
132
|
+
imageHeight: def.pxHei,
|
|
133
|
+
tileProperties: /* @__PURE__ */ new Map(),
|
|
134
|
+
solidTiles: /* @__PURE__ */ new Set()
|
|
135
|
+
};
|
|
136
|
+
map.tilesets.push(ref);
|
|
137
|
+
nextFirstGid += tileCount;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (level.layerInstances) for (let i = level.layerInstances.length - 1; i >= 0; i--) {
|
|
141
|
+
const layerInst = level.layerInstances[i];
|
|
142
|
+
if (layerInst.__type === "IntGrid") {
|
|
143
|
+
const csv = layerInst.intGridCsv;
|
|
144
|
+
const cellCount = layerInst.__cWid * layerInst.__cHei;
|
|
145
|
+
const tiles = new Array(cellCount).fill(0);
|
|
146
|
+
const intFirstGid = nextFirstGid;
|
|
147
|
+
let maxLocal = 0;
|
|
148
|
+
let hasNonZero = false;
|
|
149
|
+
const solidSet = /* @__PURE__ */ new Set();
|
|
150
|
+
if (csv) for (let j = 0; j < csv.length; j++) {
|
|
151
|
+
const v = csv[j] ?? 0;
|
|
152
|
+
if (v > 0) {
|
|
153
|
+
hasNonZero = true;
|
|
154
|
+
const localId = v - 1;
|
|
155
|
+
if (localId > maxLocal) maxLocal = localId;
|
|
156
|
+
tiles[j] = intFirstGid + localId;
|
|
157
|
+
if (isSolid(v)) solidSet.add(localId);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (hasNonZero) {
|
|
161
|
+
const tileCount = maxLocal + 1;
|
|
162
|
+
map.tilesets.push({
|
|
163
|
+
firstGid: intFirstGid,
|
|
164
|
+
name: layerInst.__identifier,
|
|
165
|
+
tileWidth: layerInst.__gridSize,
|
|
166
|
+
tileHeight: layerInst.__gridSize,
|
|
167
|
+
columns: tileCount,
|
|
168
|
+
tileCount,
|
|
169
|
+
image: "",
|
|
170
|
+
imageWidth: 0,
|
|
171
|
+
imageHeight: 0,
|
|
172
|
+
tileProperties: /* @__PURE__ */ new Map(),
|
|
173
|
+
solidTiles: solidSet
|
|
174
|
+
});
|
|
175
|
+
nextFirstGid += tileCount;
|
|
176
|
+
}
|
|
177
|
+
map.layers.push({
|
|
178
|
+
type: "tile",
|
|
179
|
+
name: layerInst.__identifier,
|
|
180
|
+
width: layerInst.__cWid,
|
|
181
|
+
height: layerInst.__cHei,
|
|
182
|
+
tiles,
|
|
183
|
+
visible: layerInst.visible,
|
|
184
|
+
opacity: layerInst.__opacity,
|
|
185
|
+
offsetX: 0,
|
|
186
|
+
offsetY: 0,
|
|
187
|
+
properties: {}
|
|
188
|
+
});
|
|
189
|
+
} else if (layerInst.__type === "Tiles" || layerInst.__type === "AutoLayer") {
|
|
190
|
+
const firstGid = firstGidByUid.get(layerInst.__tilesetDefUid ?? -1) ?? 1;
|
|
191
|
+
map.layers.push(parseTilesLayer(layerInst, mapWidth, mapHeight, gridSize, firstGid));
|
|
192
|
+
} else if (layerInst.__type === "Entities") map.layers.push(parseEntitiesLayer(layerInst));
|
|
193
|
+
}
|
|
194
|
+
results.push(map);
|
|
195
|
+
}
|
|
196
|
+
return results;
|
|
197
|
+
}
|
|
198
|
+
function parseTilesLayer(layer, mapWidth, mapHeight, gridSize, firstGid) {
|
|
199
|
+
const tiles = new Array(mapWidth * mapHeight).fill(0);
|
|
200
|
+
const tileInstances = layer.gridTiles ?? layer.autoLayerTiles ?? [];
|
|
201
|
+
for (const ti of tileInstances) {
|
|
202
|
+
const col = Math.floor(ti.px[0] / gridSize);
|
|
203
|
+
const row = Math.floor(ti.px[1] / gridSize);
|
|
204
|
+
if (col >= 0 && col < mapWidth && row >= 0 && row < mapHeight) tiles[row * mapWidth + col] = firstGid + ti.t;
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
type: "tile",
|
|
208
|
+
name: layer.__identifier,
|
|
209
|
+
width: mapWidth,
|
|
210
|
+
height: mapHeight,
|
|
211
|
+
tiles,
|
|
212
|
+
visible: layer.visible,
|
|
213
|
+
opacity: layer.__opacity,
|
|
214
|
+
offsetX: 0,
|
|
215
|
+
offsetY: 0,
|
|
216
|
+
properties: {}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function parseEntitiesLayer(layer) {
|
|
220
|
+
const objects = (layer.entityInstances ?? []).map((ent, idx) => {
|
|
221
|
+
const props = {};
|
|
222
|
+
for (const field of ent.fieldInstances) props[field.__identifier] = field.__value;
|
|
223
|
+
return {
|
|
224
|
+
id: idx + 1,
|
|
225
|
+
name: ent.__identifier,
|
|
226
|
+
type: ent.__identifier,
|
|
227
|
+
x: ent.px[0],
|
|
228
|
+
y: ent.px[1],
|
|
229
|
+
width: ent.width,
|
|
230
|
+
height: ent.height,
|
|
231
|
+
rotation: 0,
|
|
232
|
+
properties: props
|
|
233
|
+
};
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
type: "object",
|
|
237
|
+
name: layer.__identifier,
|
|
238
|
+
objects,
|
|
239
|
+
visible: layer.visible,
|
|
240
|
+
opacity: layer.__opacity,
|
|
241
|
+
properties: {}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region src/tiled-parser.ts
|
|
246
|
+
/** Convert Tiled properties array to a Record. */
|
|
41
247
|
function parseProperties(props) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
return result;
|
|
248
|
+
if (!props || props.length === 0) return {};
|
|
249
|
+
const result = {};
|
|
250
|
+
for (const p of props) result[p.name] = p.value;
|
|
251
|
+
return result;
|
|
48
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Parse a Tiled .tmj (JSON) map into TilemapData.
|
|
255
|
+
* Supports orthogonal maps with CSV tile layers and object layers.
|
|
256
|
+
* Throws TilemapParseError for unsupported formats (infinite, non-orthogonal,
|
|
257
|
+
* external tilesets, base64/compressed/chunked layer data).
|
|
258
|
+
*/
|
|
49
259
|
function parseTiled(json) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
map.tilesets.sort((a, b) => a.firstGid - b.firstGid);
|
|
84
|
-
for (const layer of data.layers) {
|
|
85
|
-
map.layers.push(parseLayer(layer));
|
|
86
|
-
}
|
|
87
|
-
return map;
|
|
260
|
+
const data = json;
|
|
261
|
+
if (data.infinite === true) throw new TilemapParseError("parseTiled: infinite maps are not supported. In Tiled: Map ▸ Map Properties ▸ set \"Infinite\" to false, then re-export.");
|
|
262
|
+
if (data.orientation && data.orientation !== "orthogonal") throw new TilemapParseError(`parseTiled: orientation "${data.orientation}" is not supported — only orthogonal maps are supported.`);
|
|
263
|
+
const map = createTilemapData(data.width, data.height, data.tilewidth, data.tileheight);
|
|
264
|
+
map.properties = parseProperties(data.properties);
|
|
265
|
+
for (const ts of data.tilesets) {
|
|
266
|
+
if (ts.source !== void 0) throw new TilemapParseError(`parseTiled: external tileset "${ts.source}" is not supported. Embed tilesets on export (Tiled ▸ Map ▸ "Embed tilesets") or run it through \`flare tiled-to-flare\` (which embeds tilesets by default).`);
|
|
267
|
+
const tileProperties = /* @__PURE__ */ new Map();
|
|
268
|
+
const solidTiles = /* @__PURE__ */ new Set();
|
|
269
|
+
if (ts.tiles) for (const tile of ts.tiles) {
|
|
270
|
+
const props = parseProperties(tile.properties);
|
|
271
|
+
if (Object.keys(props).length > 0) tileProperties.set(tile.id, props);
|
|
272
|
+
if ((tile.type ?? tile.class ?? "") === "solid" || props.solid === true || props.collision === true) solidTiles.add(tile.id);
|
|
273
|
+
}
|
|
274
|
+
const ref = {
|
|
275
|
+
firstGid: ts.firstgid,
|
|
276
|
+
name: ts.name,
|
|
277
|
+
tileWidth: ts.tilewidth,
|
|
278
|
+
tileHeight: ts.tileheight,
|
|
279
|
+
columns: ts.columns,
|
|
280
|
+
tileCount: ts.tilecount,
|
|
281
|
+
image: ts.image,
|
|
282
|
+
imageWidth: ts.imagewidth,
|
|
283
|
+
imageHeight: ts.imageheight,
|
|
284
|
+
tileProperties,
|
|
285
|
+
solidTiles
|
|
286
|
+
};
|
|
287
|
+
map.tilesets.push(ref);
|
|
288
|
+
}
|
|
289
|
+
map.tilesets.sort((a, b) => a.firstGid - b.firstGid);
|
|
290
|
+
for (const layer of data.layers) map.layers.push(parseLayer(layer));
|
|
291
|
+
return map;
|
|
88
292
|
}
|
|
89
293
|
function parseLayer(layer) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
294
|
+
if (layer.type === "tilelayer") return parseTileLayer(layer);
|
|
295
|
+
if (layer.type === "objectgroup") return parseObjectLayer(layer);
|
|
296
|
+
const l = layer;
|
|
297
|
+
throw new TilemapParseError(`parseTiled: layer "${l.name ?? ""}" has unsupported type "${l.type}". Only tilelayer and objectgroup are supported — flatten group layers and remove image layers before export.`);
|
|
94
298
|
}
|
|
95
299
|
function parseTileLayer(layer) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
300
|
+
if (layer.encoding !== void 0 && layer.encoding !== "csv" || layer.compression !== void 0 || layer.chunks !== void 0) throw new TilemapParseError("parseTiled: base64/compressed/chunked (infinite) layer data is not supported. In Tiled export options set Tile Layer Format to \"CSV\".");
|
|
301
|
+
if (typeof layer.data === "string") throw new TilemapParseError("parseTiled: base64/compressed/chunked (infinite) layer data is not supported. In Tiled export options set Tile Layer Format to \"CSV\".");
|
|
302
|
+
return {
|
|
303
|
+
type: "tile",
|
|
304
|
+
name: layer.name,
|
|
305
|
+
width: layer.width,
|
|
306
|
+
height: layer.height,
|
|
307
|
+
tiles: layer.data.slice(),
|
|
308
|
+
visible: layer.visible,
|
|
309
|
+
opacity: layer.opacity,
|
|
310
|
+
offsetX: layer.offsetx ?? 0,
|
|
311
|
+
offsetY: layer.offsety ?? 0,
|
|
312
|
+
properties: parseProperties(layer.properties)
|
|
313
|
+
};
|
|
108
314
|
}
|
|
109
315
|
function parseObjectLayer(layer) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// src/ldtk-parser.ts
|
|
132
|
-
function parseLDtk(json) {
|
|
133
|
-
const data = json;
|
|
134
|
-
const results = [];
|
|
135
|
-
const tilesetDefs = /* @__PURE__ */ new Map();
|
|
136
|
-
for (const ts of data.defs.tilesets) {
|
|
137
|
-
tilesetDefs.set(ts.uid, ts);
|
|
138
|
-
}
|
|
139
|
-
for (const level of data.levels) {
|
|
140
|
-
const gridSize = data.defaultGridSize;
|
|
141
|
-
const mapWidth = Math.ceil(level.pxWid / gridSize);
|
|
142
|
-
const mapHeight = Math.ceil(level.pxHei / gridSize);
|
|
143
|
-
const map = createTilemapData(mapWidth, mapHeight, gridSize, gridSize);
|
|
144
|
-
if (level.fieldInstances) {
|
|
145
|
-
for (const field of level.fieldInstances) {
|
|
146
|
-
map.properties[field.__identifier] = field.__value;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
const usedTilesets = /* @__PURE__ */ new Set();
|
|
150
|
-
if (level.layerInstances) {
|
|
151
|
-
for (let i = level.layerInstances.length - 1; i >= 0; i--) {
|
|
152
|
-
const layerInst = level.layerInstances[i];
|
|
153
|
-
const layer = parseLayerInstance(layerInst, mapWidth, mapHeight, gridSize);
|
|
154
|
-
if (layer) {
|
|
155
|
-
map.layers.push(layer);
|
|
156
|
-
}
|
|
157
|
-
if (layerInst.__tilesetDefUid !== void 0) {
|
|
158
|
-
usedTilesets.add(layerInst.__tilesetDefUid);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
let firstGid = 1;
|
|
163
|
-
for (const uid of usedTilesets) {
|
|
164
|
-
const def = tilesetDefs.get(uid);
|
|
165
|
-
if (!def) continue;
|
|
166
|
-
const columns = def.__cWid;
|
|
167
|
-
const tileCount = def.__cWid * def.__cHei;
|
|
168
|
-
const ref = {
|
|
169
|
-
firstGid,
|
|
170
|
-
name: def.identifier,
|
|
171
|
-
tileWidth: def.tileGridSize,
|
|
172
|
-
tileHeight: def.tileGridSize,
|
|
173
|
-
columns,
|
|
174
|
-
tileCount,
|
|
175
|
-
image: def.relPath,
|
|
176
|
-
imageWidth: def.pxWid,
|
|
177
|
-
imageHeight: def.pxHei,
|
|
178
|
-
tileProperties: /* @__PURE__ */ new Map(),
|
|
179
|
-
solidTiles: /* @__PURE__ */ new Set()
|
|
180
|
-
};
|
|
181
|
-
map.tilesets.push(ref);
|
|
182
|
-
firstGid += tileCount;
|
|
183
|
-
}
|
|
184
|
-
results.push(map);
|
|
185
|
-
}
|
|
186
|
-
return results;
|
|
316
|
+
const objects = layer.objects.map((obj) => ({
|
|
317
|
+
id: obj.id,
|
|
318
|
+
name: obj.name,
|
|
319
|
+
type: obj.type ?? obj.class ?? "",
|
|
320
|
+
x: obj.x,
|
|
321
|
+
y: obj.y,
|
|
322
|
+
width: obj.width,
|
|
323
|
+
height: obj.height,
|
|
324
|
+
rotation: obj.rotation,
|
|
325
|
+
properties: parseProperties(obj.properties)
|
|
326
|
+
}));
|
|
327
|
+
return {
|
|
328
|
+
type: "object",
|
|
329
|
+
name: layer.name,
|
|
330
|
+
objects,
|
|
331
|
+
visible: layer.visible,
|
|
332
|
+
opacity: layer.opacity,
|
|
333
|
+
properties: parseProperties(layer.properties)
|
|
334
|
+
};
|
|
187
335
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return null;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
function parseIntGridLayer(layer, mapWidth, mapHeight) {
|
|
202
|
-
const tiles = layer.intGridCsv ? layer.intGridCsv.slice() : new Array(mapWidth * mapHeight).fill(0);
|
|
203
|
-
return {
|
|
204
|
-
type: "tile",
|
|
205
|
-
name: layer.__identifier,
|
|
206
|
-
width: layer.__cWid,
|
|
207
|
-
height: layer.__cHei,
|
|
208
|
-
tiles,
|
|
209
|
-
visible: layer.visible,
|
|
210
|
-
opacity: layer.__opacity,
|
|
211
|
-
offsetX: 0,
|
|
212
|
-
offsetY: 0,
|
|
213
|
-
properties: {}
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
function parseTilesLayer(layer, mapWidth, mapHeight, gridSize) {
|
|
217
|
-
const tiles = new Array(mapWidth * mapHeight).fill(0);
|
|
218
|
-
const tileInstances = layer.gridTiles ?? layer.autoLayerTiles ?? [];
|
|
219
|
-
for (const ti of tileInstances) {
|
|
220
|
-
const col = Math.floor(ti.px[0] / gridSize);
|
|
221
|
-
const row = Math.floor(ti.px[1] / gridSize);
|
|
222
|
-
if (col >= 0 && col < mapWidth && row >= 0 && row < mapHeight) {
|
|
223
|
-
tiles[row * mapWidth + col] = ti.t + 1;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return {
|
|
227
|
-
type: "tile",
|
|
228
|
-
name: layer.__identifier,
|
|
229
|
-
width: mapWidth,
|
|
230
|
-
height: mapHeight,
|
|
231
|
-
tiles,
|
|
232
|
-
visible: layer.visible,
|
|
233
|
-
opacity: layer.__opacity,
|
|
234
|
-
offsetX: 0,
|
|
235
|
-
offsetY: 0,
|
|
236
|
-
properties: {}
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
function parseEntitiesLayer(layer) {
|
|
240
|
-
const objects = (layer.entityInstances ?? []).map((ent, idx) => {
|
|
241
|
-
const props = {};
|
|
242
|
-
for (const field of ent.fieldInstances) {
|
|
243
|
-
props[field.__identifier] = field.__value;
|
|
244
|
-
}
|
|
245
|
-
return {
|
|
246
|
-
id: idx + 1,
|
|
247
|
-
name: ent.__identifier,
|
|
248
|
-
type: ent.__identifier,
|
|
249
|
-
x: ent.px[0],
|
|
250
|
-
y: ent.px[1],
|
|
251
|
-
width: ent.width,
|
|
252
|
-
height: ent.height,
|
|
253
|
-
rotation: 0,
|
|
254
|
-
properties: props
|
|
255
|
-
};
|
|
256
|
-
});
|
|
257
|
-
return {
|
|
258
|
-
type: "object",
|
|
259
|
-
name: layer.__identifier,
|
|
260
|
-
objects,
|
|
261
|
-
visible: layer.visible,
|
|
262
|
-
opacity: layer.__opacity,
|
|
263
|
-
properties: {}
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// src/tilemap-renderer.ts
|
|
268
|
-
import { SpriteBatch } from "@flare-engine/render";
|
|
269
|
-
var TilemapRenderer = class {
|
|
270
|
-
/** The tilemap data to render. */
|
|
271
|
-
map;
|
|
272
|
-
/** SpriteBatch used for batched tile rendering. */
|
|
273
|
-
batch;
|
|
274
|
-
/** Map from tileset name → SkImage. Set via setTilesetImage(). */
|
|
275
|
-
_images = /* @__PURE__ */ new Map();
|
|
276
|
-
/** Reusable bounds rect — avoids per-frame allocation from camera.getBounds(). */
|
|
277
|
-
_boundsBuf = { x: 0, y: 0, width: 0, height: 0 };
|
|
278
|
-
constructor(map) {
|
|
279
|
-
this.map = map;
|
|
280
|
-
this.batch = new SpriteBatch();
|
|
281
|
-
}
|
|
282
|
-
/** Associate a loaded image with a tileset by name. */
|
|
283
|
-
setTilesetImage(tilesetName, image) {
|
|
284
|
-
this._images.set(tilesetName, image);
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Queue visible tile layers into a caller-owned `batch` using the camera for
|
|
288
|
-
* culling. Does NOT flush — the caller owns the single flush so tiles z-order
|
|
289
|
-
* under sprites. Zero-alloc: reuses the internal bounds buffer.
|
|
290
|
-
*/
|
|
291
|
-
drawInto(camera, batch) {
|
|
292
|
-
const bounds = camera.getBoundsInto(this._boundsBuf);
|
|
293
|
-
const tw = this.map.tileWidth;
|
|
294
|
-
const th = this.map.tileHeight;
|
|
295
|
-
for (const layer of this.map.layers) {
|
|
296
|
-
if (layer.type !== "tile" || !layer.visible) continue;
|
|
297
|
-
this._drawTileLayer(layer, bounds.x, bounds.y, bounds.width, bounds.height, tw, th, batch);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Draw visible tile layers using the camera for culling.
|
|
302
|
-
* Call `flush` on a callback to issue the actual draw calls.
|
|
303
|
-
* Delegates to `drawInto` so culling and queuing logic live in one place.
|
|
304
|
-
*/
|
|
305
|
-
draw(camera, flush) {
|
|
306
|
-
this.drawInto(camera, this.batch);
|
|
307
|
-
flush(this.batch);
|
|
308
|
-
}
|
|
309
|
-
_drawTileLayer(layer, viewX, viewY, viewW, viewH, tw, th, batch) {
|
|
310
|
-
const offsetX = layer.offsetX;
|
|
311
|
-
const offsetY = layer.offsetY;
|
|
312
|
-
const startCol = Math.max(0, Math.floor((viewX - offsetX) / tw));
|
|
313
|
-
const startRow = Math.max(0, Math.floor((viewY - offsetY) / th));
|
|
314
|
-
const endCol = Math.min(layer.width - 1, Math.floor((viewX - offsetX + viewW) / tw));
|
|
315
|
-
const endRow = Math.min(layer.height - 1, Math.floor((viewY - offsetY + viewH) / th));
|
|
316
|
-
for (let row = startRow; row <= endRow; row++) {
|
|
317
|
-
for (let col = startCol; col <= endCol; col++) {
|
|
318
|
-
const gid = getTileAt(layer, col, row);
|
|
319
|
-
if (gid === 0) continue;
|
|
320
|
-
const tileset = getTilesetForGid(this.map.tilesets, gid);
|
|
321
|
-
if (!tileset) continue;
|
|
322
|
-
const image = this._images.get(tileset.name);
|
|
323
|
-
if (!image) continue;
|
|
324
|
-
const localId = gid - tileset.firstGid;
|
|
325
|
-
const srcCol = localId % tileset.columns;
|
|
326
|
-
const srcRow = (localId - srcCol) / tileset.columns;
|
|
327
|
-
const srcX = srcCol * tileset.tileWidth;
|
|
328
|
-
const srcY = srcRow * tileset.tileHeight;
|
|
329
|
-
const dstX = col * tw + offsetX;
|
|
330
|
-
const dstY = row * th + offsetY;
|
|
331
|
-
batch.drawRaw(image, srcX, srcY, tileset.tileWidth, tileset.tileHeight, dstX, dstY, tw, th);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
// src/tilemap-collider.ts
|
|
338
|
-
import { AABB } from "@flare-engine/physics";
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/tilemap-collider.ts
|
|
338
|
+
/**
|
|
339
|
+
* Extract collision shapes from solid tiles in a tilemap.
|
|
340
|
+
* A tile is considered solid if its tileset marks the local tile ID as solid.
|
|
341
|
+
*
|
|
342
|
+
* @param map The parsed tilemap data.
|
|
343
|
+
* @param layerName Optional: only extract from this layer. If omitted, extracts from all tile layers.
|
|
344
|
+
* @returns Array of TileCollider bodies.
|
|
345
|
+
*/
|
|
339
346
|
function extractColliders(map, layerName) {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
347
|
+
const colliders = [];
|
|
348
|
+
const tw = map.tileWidth;
|
|
349
|
+
const th = map.tileHeight;
|
|
350
|
+
for (const layer of map.layers) {
|
|
351
|
+
if (layer.type !== "tile") continue;
|
|
352
|
+
if (layerName !== void 0 && layer.name !== layerName) continue;
|
|
353
|
+
extractFromLayer(layer, map.tilesets, tw, th, colliders);
|
|
354
|
+
}
|
|
355
|
+
return colliders;
|
|
349
356
|
}
|
|
350
357
|
function extractFromLayer(layer, tilesets, tw, th, out) {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
368
|
-
}
|
|
358
|
+
for (let row = 0; row < layer.height; row++) for (let col = 0; col < layer.width; col++) {
|
|
359
|
+
const gid = layer.tiles[row * layer.width + col] ?? 0;
|
|
360
|
+
if (gid === 0) continue;
|
|
361
|
+
const tileset = getTilesetForGid(tilesets, gid);
|
|
362
|
+
if (!tileset) continue;
|
|
363
|
+
const localId = (gid & GID_FLIP_MASK) - tileset.firstGid;
|
|
364
|
+
if (!tileset.solidTiles.has(localId)) continue;
|
|
365
|
+
out.push({
|
|
366
|
+
x: col * tw + tw * .5 + layer.offsetX,
|
|
367
|
+
y: row * th + th * .5 + layer.offsetY,
|
|
368
|
+
shape: AABB(tw, th),
|
|
369
|
+
col,
|
|
370
|
+
row,
|
|
371
|
+
gid: gid & GID_FLIP_MASK
|
|
372
|
+
});
|
|
373
|
+
}
|
|
369
374
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
375
|
+
//#endregion
|
|
376
|
+
//#region src/tilemap-renderer.ts
|
|
377
|
+
/**
|
|
378
|
+
* TilemapRenderer — draws a tilemap with viewport culling.
|
|
379
|
+
* Only tiles visible to the camera are submitted to the SpriteBatch.
|
|
380
|
+
*/
|
|
381
|
+
var TilemapRenderer = class {
|
|
382
|
+
/** The tilemap data to render. */
|
|
383
|
+
map;
|
|
384
|
+
/** SpriteBatch used for batched tile rendering. */
|
|
385
|
+
batch;
|
|
386
|
+
/** Map from tileset name → SkImage. Set via setTilesetImage(). */
|
|
387
|
+
_images = /* @__PURE__ */ new Map();
|
|
388
|
+
/** Reusable bounds rect — avoids per-frame allocation from camera.getBounds(). */
|
|
389
|
+
_boundsBuf = {
|
|
390
|
+
x: 0,
|
|
391
|
+
y: 0,
|
|
392
|
+
width: 0,
|
|
393
|
+
height: 0
|
|
394
|
+
};
|
|
395
|
+
constructor(map) {
|
|
396
|
+
this.map = map;
|
|
397
|
+
this.batch = new SpriteBatch();
|
|
398
|
+
}
|
|
399
|
+
/** Associate a loaded image with a tileset by name. */
|
|
400
|
+
setTilesetImage(tilesetName, image) {
|
|
401
|
+
this._images.set(tilesetName, image);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Queue visible tile layers into a caller-owned `batch` using the camera for
|
|
405
|
+
* culling. Does NOT flush — the caller owns the single flush so tiles z-order
|
|
406
|
+
* under sprites. Zero-alloc: reuses the internal bounds buffer.
|
|
407
|
+
*/
|
|
408
|
+
drawInto(camera, batch) {
|
|
409
|
+
const bounds = camera.getBoundsInto(this._boundsBuf);
|
|
410
|
+
const tw = this.map.tileWidth;
|
|
411
|
+
const th = this.map.tileHeight;
|
|
412
|
+
for (const layer of this.map.layers) {
|
|
413
|
+
if (layer.type !== "tile" || !layer.visible) continue;
|
|
414
|
+
this._drawTileLayer(layer, bounds.x, bounds.y, bounds.width, bounds.height, tw, th, batch);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Draw visible tile layers using the camera for culling.
|
|
419
|
+
* Call `flush` on a callback to issue the actual draw calls.
|
|
420
|
+
* Delegates to `drawInto` so culling and queuing logic live in one place.
|
|
421
|
+
*/
|
|
422
|
+
draw(camera, flush) {
|
|
423
|
+
this.drawInto(camera, this.batch);
|
|
424
|
+
flush(this.batch);
|
|
425
|
+
}
|
|
426
|
+
_drawTileLayer(layer, viewX, viewY, viewW, viewH, tw, th, batch) {
|
|
427
|
+
const offsetX = layer.offsetX;
|
|
428
|
+
const offsetY = layer.offsetY;
|
|
429
|
+
const startCol = Math.max(0, Math.floor((viewX - offsetX) / tw));
|
|
430
|
+
const startRow = Math.max(0, Math.floor((viewY - offsetY) / th));
|
|
431
|
+
const endCol = Math.min(layer.width - 1, Math.floor((viewX - offsetX + viewW) / tw));
|
|
432
|
+
const endRow = Math.min(layer.height - 1, Math.floor((viewY - offsetY + viewH) / th));
|
|
433
|
+
for (let row = startRow; row <= endRow; row++) for (let col = startCol; col <= endCol; col++) {
|
|
434
|
+
const gid = getTileAt(layer, col, row);
|
|
435
|
+
if (gid === 0) continue;
|
|
436
|
+
const tileset = getTilesetForGid(this.map.tilesets, gid);
|
|
437
|
+
if (!tileset) continue;
|
|
438
|
+
const image = this._images.get(tileset.name);
|
|
439
|
+
if (!image) continue;
|
|
440
|
+
const localId = (gid & GID_FLIP_MASK) - tileset.firstGid;
|
|
441
|
+
const srcCol = localId % tileset.columns;
|
|
442
|
+
const srcRow = (localId - srcCol) / tileset.columns;
|
|
443
|
+
const srcX = srcCol * tileset.tileWidth;
|
|
444
|
+
const srcY = srcRow * tileset.tileHeight;
|
|
445
|
+
const dstX = col * tw + offsetX;
|
|
446
|
+
const dstY = row * th + offsetY;
|
|
447
|
+
batch.drawRaw(image, srcX, srcY, tileset.tileWidth, tileset.tileHeight, dstX, dstY, tw, th);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
379
450
|
};
|
|
451
|
+
//#endregion
|
|
452
|
+
export { GID_FLIP_MASK, TilemapParseError, TilemapRenderer, createTilemapData, decodeGidFlags, extractColliders, getTileAt, getTileSourceRect, getTilesetForGid, parseLDtk, parseLDtkEntities, parseTiled, stripGidFlags };
|
|
453
|
+
|
|
380
454
|
//# sourceMappingURL=index.mjs.map
|