@@ -70,10 +70,10 @@ class GodotTilemapExporter {
70
70
// noinspection JSUnresolvedVariable
71
71
if ( layer . isTileLayer ) {
72
72
const layerData = this . getLayerData ( layer ) ;
73
- for ( let idx = 0 ; idx < layerData . length ; idx ++ ) {
73
+ for ( let idx = 0 ; idx < layerData . length ; idx ++ ) {
74
74
const ld = layerData [ idx ] ;
75
75
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 ;
77
77
this . mapLayerToTileset ( layer . name , ld . tilesetID ) ;
78
78
this . tileMapsString += this . getTileMapTemplate ( tileMapName , ld . tilesetID , ld . poolIntArrayString , ld . parent , layer . map . tileWidth , layer . map . tileHeight ) ;
79
79
}
@@ -107,18 +107,19 @@ class GodotTilemapExporter {
107
107
for ( let x = boundingRect . left ; x <= boundingRect . right ; ++ x ) {
108
108
109
109
// noinspection JSUnresolvedVariable,JSUnresolvedFunction
110
- let tileId = layer . cellAt ( x , y ) . tileId ;
110
+ let cell = layer . cellAt ( x , y ) ;
111
+ let tileId = cell . tileId ;
111
112
let tileGodotID = tileId ;
112
113
113
114
/** Check and don't export blank tiles **/
114
115
if ( tileId !== - 1 ) {
115
-
116
+
116
117
/**
117
118
* Find the tileset on the list, if not found, add
118
119
*/
119
120
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 ) ;
122
123
123
124
if ( ! tileset ) {
124
125
tileset = {
@@ -135,7 +136,7 @@ class GodotTilemapExporter {
135
136
}
136
137
137
138
const tilesetColumns = tileset . tilesetColumns ;
138
-
139
+
139
140
/** Handle Godot strange offset by rows in the tileset image **/
140
141
if ( tileId >= tilesetColumns ) {
141
142
let tileY = Math . floor ( tileId / tilesetColumns ) ;
@@ -146,19 +147,21 @@ class GodotTilemapExporter {
146
147
/**
147
148
* Godot coordinates use an offset of 65536
148
149
* Check the README.md: Godot Tilemap Encoding & Limits
149
- * * /
150
+ */
150
151
let yValue = y ;
151
152
let xValue = x ;
152
153
if ( xValue < 0 ) {
153
154
yValue = y + 1 ;
154
155
}
155
156
let firstParam = xValue + ( yValue * this . tileOffset ) ;
156
157
158
+
157
159
/**
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
160
163
*/
161
- let secondParam = 0 ;
164
+ let secondParam = this . getSecondParam ( cell ) ;
162
165
163
166
tileset . poolIntArrayString += firstParam + ", " + secondParam + ", " + tileGodotID + ", " ;
164
167
}
@@ -167,16 +170,16 @@ class GodotTilemapExporter {
167
170
168
171
// Remove trailing commas and blank
169
172
tilesetList . forEach ( i => {
170
- i . poolIntArrayString = i . poolIntArrayString . replace ( / , \s * $ / , "" ) ;
173
+ i . poolIntArrayString = i . poolIntArrayString . replace ( / , \s * $ / , "" ) ;
171
174
} ) ;
172
-
173
- for ( let idx = 0 ; idx < tilesetList . length ; idx ++ ) {
175
+
176
+ for ( let idx = 0 ; idx < tilesetList . length ; idx ++ ) {
174
177
const current = tilesetList [ idx ] ;
175
178
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
+ }
180
183
}
181
184
182
185
return tilesetList ;
@@ -186,13 +189,112 @@ class GodotTilemapExporter {
186
189
return this . tilesetsIndex . get ( tileset . name ) ;
187
190
}
188
191
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
+
189
291
/**
190
292
* Tileset should expose columns ... but didn't at the moment so we
191
293
* 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
193
295
* tile spacing (padding between individual tiles).
194
296
* @returns {number }
195
- ** /
297
+ */
196
298
getTilesetColumns ( tileset ) {
197
299
// noinspection JSUnresolvedVariable
198
300
const imageWidth = tileset . imageWidth + tileset . tileSpacing - tileset . margin
0 commit comments