Skip to content

Commit 8abadcb

Browse files
committed
Add support for tile rotation and flipping
1 parent c856bd6 commit 8abadcb

File tree

1 file changed

+122
-20
lines changed

1 file changed

+122
-20
lines changed

export_to_godot_tilemap.js

+122-20
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ class GodotTilemapExporter {
7070
// noinspection JSUnresolvedVariable
7171
if (layer.isTileLayer) {
7272
const layerData = this.getLayerData(layer);
73-
for(let idx = 0; idx < layerData.length; idx++) {
73+
for (let idx = 0; idx < layerData.length; idx++) {
7474
const ld = layerData[idx];
7575
if (!ld.isEmpty) {
76-
const tileMapName = idx === 0 ? layer.name || "TileMap " + i : ld.tileset.name || "TileMap " + i +"_" + idx;
76+
const tileMapName = idx === 0 ? layer.name || "TileMap " + i : ld.tileset.name || "TileMap " + i + "_" + idx;
7777
this.mapLayerToTileset(layer.name, ld.tilesetID);
7878
this.tileMapsString += this.getTileMapTemplate(tileMapName, ld.tilesetID, ld.poolIntArrayString, ld.parent, layer.map.tileWidth, layer.map.tileHeight);
7979
}
@@ -107,18 +107,19 @@ class GodotTilemapExporter {
107107
for (let x = boundingRect.left; x <= boundingRect.right; ++x) {
108108

109109
// noinspection JSUnresolvedVariable,JSUnresolvedFunction
110-
let tileId = layer.cellAt(x, y).tileId;
110+
let cell = layer.cellAt(x, y);
111+
let tileId = cell.tileId;
111112
let tileGodotID = tileId;
112113

113114
/** Check and don't export blank tiles **/
114115
if (tileId !== -1) {
115-
116+
116117
/**
117118
* Find the tileset on the list, if not found, add
118119
*/
119120
const tile = layer.tileAt(x, y);
120-
121-
let tileset = tilesetList.find( item => item.tileset === tile.tileset);
121+
122+
let tileset = tilesetList.find(item => item.tileset === tile.tileset);
122123

123124
if (!tileset) {
124125
tileset = {
@@ -135,7 +136,7 @@ class GodotTilemapExporter {
135136
}
136137

137138
const tilesetColumns = tileset.tilesetColumns;
138-
139+
139140
/** Handle Godot strange offset by rows in the tileset image **/
140141
if (tileId >= tilesetColumns) {
141142
let tileY = Math.floor(tileId / tilesetColumns);
@@ -146,19 +147,21 @@ class GodotTilemapExporter {
146147
/**
147148
* Godot coordinates use an offset of 65536
148149
* Check the README.md: Godot Tilemap Encoding & Limits
149-
* */
150+
*/
150151
let yValue = y;
151152
let xValue = x;
152153
if (xValue < 0) {
153154
yValue = y + 1;
154155
}
155156
let firstParam = xValue + (yValue * this.tileOffset);
156157

158+
157159
/**
158-
This is texture image form the tileset in godot
159-
Tiled doesn't support more than one image in tileset
160+
* This is texture image form the tileset in godot
161+
* Tiled doesn't support more than one image in tileset
162+
* Also this is used to encode the rotation of a tile... as it seems. :P
160163
*/
161-
let secondParam = 0;
164+
let secondParam = this.getSecondParam(cell);
162165

163166
tileset.poolIntArrayString += firstParam + ", " + secondParam + ", " + tileGodotID + ", ";
164167
}
@@ -167,16 +170,16 @@ class GodotTilemapExporter {
167170

168171
// Remove trailing commas and blank
169172
tilesetList.forEach(i => {
170-
i.poolIntArrayString = i.poolIntArrayString.replace(/,\s*$/, "");
173+
i.poolIntArrayString = i.poolIntArrayString.replace(/,\s*$/, "");
171174
});
172-
173-
for(let idx = 0; idx < tilesetList.length; idx++) {
175+
176+
for (let idx = 0; idx < tilesetList.length; idx++) {
174177
const current = tilesetList[idx];
175178
if (current.tileset !== null && current.poolIntArrayString !== "") {
176-
current.tilesetID = this.getTilesetIDByTileset(current.tileset);
177-
} else {
178-
console.warn(`Error: The layer ${layer.name} is empty and has been skipped!`);
179-
}
179+
current.tilesetID = this.getTilesetIDByTileset(current.tileset);
180+
} else {
181+
console.warn(`Error: The layer ${layer.name} is empty and has been skipped!`);
182+
}
180183
}
181184

182185
return tilesetList;
@@ -186,13 +189,112 @@ class GodotTilemapExporter {
186189
return this.tilesetsIndex.get(tileset.name);
187190
}
188191

192+
getSecondParam(cell) {
193+
/**
194+
* no rotation or flips
195+
* cell.cell.flippedHorizontally is false and
196+
* cell.cell.flippedVertically is false
197+
* cell.cell.flippedAntiDiagonally is false
198+
*/
199+
let secondParam = 0;
200+
201+
202+
/**
203+
* rotated 1x left or
204+
* rotated 3x right
205+
*/
206+
if (
207+
cell.flippedHorizontally === false &&
208+
cell.flippedVertically === true &&
209+
cell.flippedAntiDiagonally === true
210+
) {
211+
secondParam = -1073741824;
212+
}
213+
214+
/**
215+
* rotated 2x left or 2x right or
216+
* vertical and horizontal flip
217+
*/
218+
if (
219+
cell.flippedHorizontally === true &&
220+
cell.flippedVertically === true &&
221+
cell.flippedAntiDiagonally === false
222+
) {
223+
secondParam = 1610612736;
224+
}
225+
226+
/**
227+
* rotated 3x left or
228+
* rotated 1x right
229+
*/
230+
if (
231+
cell.flippedHorizontally === true &&
232+
cell.flippedVertically === false &&
233+
cell.flippedAntiDiagonally === true
234+
) {
235+
secondParam = -1610612736;
236+
}
237+
238+
/**
239+
* flipped horizontal or
240+
* flipped vertical and 2x times rotated left/right
241+
*/
242+
if (
243+
cell.flippedHorizontally === true &&
244+
cell.flippedVertically === false &&
245+
cell.flippedAntiDiagonally === false
246+
) {
247+
secondParam = 536870912;
248+
}
249+
250+
/**
251+
* flipped horizontal and 1x rotated left or
252+
* flipped vertical and 1x time rotated right
253+
*/
254+
if (
255+
cell.flippedHorizontally === false &&
256+
cell.flippedVertically === false &&
257+
cell.flippedAntiDiagonally === true
258+
) {
259+
secondParam = -2147483648;
260+
}
261+
262+
/**
263+
* flipped horizontal and 2x times rotated left/right or
264+
* flipped vertically
265+
*/
266+
if (
267+
cell.flippedHorizontally === false &&
268+
cell.flippedVertically === true &&
269+
cell.flippedAntiDiagonally === false
270+
) {
271+
secondParam = 1073741824;
272+
}
273+
274+
/**
275+
* flipped horizontal and 3x rotated left or
276+
* flipped vertically and 1x rotated left or
277+
* flipped horizontal and 1x rotated right or
278+
* flipped vertically and 3x rotated right
279+
*/
280+
if (
281+
cell.flippedHorizontally === true &&
282+
cell.flippedVertically === true &&
283+
cell.flippedAntiDiagonally === true
284+
) {
285+
secondParam = -536870912;
286+
}
287+
288+
return secondParam;
289+
}
290+
189291
/**
190292
* Tileset should expose columns ... but didn't at the moment so we
191293
* calculate them base on the image width and tileWidth.
192-
* Takes into account margin (extra space around the image edges) and
294+
* Takes into account margin (extra space around the image edges) and
193295
* tile spacing (padding between individual tiles).
194296
* @returns {number}
195-
**/
297+
*/
196298
getTilesetColumns(tileset) {
197299
// noinspection JSUnresolvedVariable
198300
const imageWidth = tileset.imageWidth + tileset.tileSpacing - tileset.margin

0 commit comments

Comments
 (0)