@@ -10,6 +10,7 @@ class GodotTilemapExporter {
10
10
this . tileOffset = 65536 ;
11
11
this . tileMapsString = "" ;
12
12
this . tilesetsString = "" ;
13
+ this . extResourceId = 0 ;
13
14
14
15
/**
15
16
* Tiled doesn't have tileset ID so we create a map
@@ -48,11 +49,11 @@ class GodotTilemapExporter {
48
49
for ( let index = 0 ; index < this . map . tilesets . length ; ++ index ) {
49
50
// noinspection JSUnresolvedVariable
50
51
const tileset = this . map . tilesets [ index ] ;
51
- const tilesetID = index + 1 ;
52
- this . tilesetsIndex . set ( tileset . name , tilesetID ) ;
52
+ this . extResourceId = index + 1 ;
53
+ this . tilesetsIndex . set ( tileset . name , this . extResourceId ) ;
53
54
// noinspection JSUnresolvedVariable
54
55
let tilesetPath = tileset . asset . fileName . replace ( this . projectRoot , "" ) . replace ( '.tsx' , '.tres' ) ;
55
- this . tilesetsString += this . getTilesetResourceTemplate ( tilesetID , tilesetPath ) ;
56
+ this . tilesetsString += this . getTilesetResourceTemplate ( this . extResourceId , tilesetPath , "TileSet" ) ;
56
57
}
57
58
58
59
}
@@ -78,6 +79,39 @@ class GodotTilemapExporter {
78
79
this . tileMapsString += this . getTileMapTemplate ( tileMapName , ld . tilesetID , ld . poolIntArrayString , ld . parent , layer . map . tileWidth , layer . map . tileHeight ) ;
79
80
}
80
81
}
82
+ } else if ( layer . isObjectLayer ) {
83
+ this . tileMapsString += `
84
+
85
+ [node name="${ layer . name } " type="Node2D" parent="."]` ;
86
+ for ( const object of layer . objects ) {
87
+ if ( object . tile ) {
88
+ let tilesetsIndexKey = object . tile . tileset . name + "_Image" ;
89
+ let textureResourceId = 0 ;
90
+ if ( ! this . tilesetsIndex . get ( tilesetsIndexKey ) ) {
91
+ this . extResourceId = this . extResourceId + 1 ;
92
+ textureResourceId = this . extResourceId ;
93
+ this . tilesetsIndex . set ( tilesetsIndexKey , this . extResourceId ) ;
94
+ let tilesetPath = object . tile . tileset . image . replace ( this . projectRoot , "" ) ;
95
+ this . tilesetsString += this . getTilesetResourceTemplate ( this . extResourceId , tilesetPath , "Texture" ) ;
96
+ } else {
97
+ textureResourceId = this . tilesetsIndex . get ( tilesetsIndexKey ) ;
98
+ }
99
+
100
+ let tileOffset = this . getTileOffset ( object . tile . tileset , object . tile . id ) ;
101
+
102
+ // Account for anchoring in Godot (corner vs. middle):
103
+ let objectPositionX = object . x + ( object . tile . width / 2 ) ;
104
+ let objectPositionY = object . y - ( object . tile . height / 2 ) ;
105
+
106
+ this . tileMapsString += `
107
+
108
+ [node name="${ object . name } " type="Sprite" parent="${ layer . name } "]
109
+ position = Vector2( ${ objectPositionX } , ${ objectPositionY } )
110
+ texture = ExtResource( ${ textureResourceId } )
111
+ region_enabled = true
112
+ region_rect = Rect2( ${ tileOffset . x } , ${ tileOffset . y } , ${ object . tile . width } , ${ object . tile . height } )` ;
113
+ }
114
+ }
81
115
}
82
116
}
83
117
}
@@ -305,6 +339,26 @@ class GodotTilemapExporter {
305
339
return Math . floor ( calculatedColumnCount ) ;
306
340
}
307
341
342
+ /**
343
+ * Calculate the X and Y offset (in pixels) for the specified tile
344
+ * ID within the specified tileset image.
345
+ *
346
+ * @param {Tileset } tileset - The full Tileset object
347
+ * @param {int } tileId - Id for the tile to extract offset for
348
+ * @returns {object } - An object with pixel offset in the format {x: int, y: int}
349
+ */
350
+ getTileOffset ( tileset , tileId ) {
351
+ let columnCount = this . getTilesetColumns ( tileset ) ;
352
+ let row = Math . floor ( tileId / columnCount ) ;
353
+ let col = tileId % columnCount ;
354
+ let xOffset = tileset . margin + ( tileset . tileSpacing * col ) ;
355
+ let yOffset = tileset . margin + ( tileset . tileSpacing * row ) ;
356
+ return {
357
+ x : ( col * tileset . tileWidth ) + xOffset ,
358
+ y : ( row * tileset . tileHeight ) + yOffset
359
+ } ;
360
+ }
361
+
308
362
/**
309
363
* Template for a scene
310
364
* @returns {string }
@@ -322,8 +376,10 @@ ${this.tileMapsString}
322
376
* Template for a tileset resource
323
377
* @returns {string }
324
378
*/
325
- getTilesetResourceTemplate ( id , path ) {
326
- return `[ext_resource path="res://${ path } " type="TileSet" id=${ id } ]
379
+ getTilesetResourceTemplate ( id , path , type ) {
380
+ // Strip leading slashes to prevent invalid triple slashes in Godot res:// path:
381
+ path = path . replace ( / ^ \/ + / , '' ) ;
382
+ return `[ext_resource path="res://${ path } " type="${ type } " id=${ id } ]
327
383
` ;
328
384
}
329
385
0 commit comments