Skip to content

Commit c856bd6

Browse files
authored
Merge pull request #11 from devharts/develop
Take into account tile spacing (padding between tiles)
2 parents 26c825a + 149923c commit c856bd6

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

export_to_godot_tilemap.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,19 @@ class GodotTilemapExporter {
188188

189189
/**
190190
* Tileset should expose columns ... but didn't at the moment so we
191-
* calculate them base on the image width and tileWidth
192-
* return {number}
191+
* calculate them base on the image width and tileWidth.
192+
* Takes into account margin (extra space around the image edges) and
193+
* tile spacing (padding between individual tiles).
194+
* @returns {number}
193195
**/
194196
getTilesetColumns(tileset) {
195197
// noinspection JSUnresolvedVariable
196-
return Math.floor(tileset.imageWidth / tileset.tileWidth);
198+
const imageWidth = tileset.imageWidth + tileset.tileSpacing - tileset.margin
199+
const tileWidth = tileset.tileWidth + tileset.tileSpacing
200+
const calculatedColumnCount = imageWidth / tileWidth
201+
// Tiled ignores "partial" tiles (extra unaccounted for pixels in the image),
202+
// so we need to return as Math.floor to avoid throwing off the tile indices.
203+
return Math.floor(calculatedColumnCount);
197204
}
198205

199206
/**

export_to_godot_tileset.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ ${this.shapesResources}[resource]
117117
0/texture = ExtResource( 1 )
118118
0/tex_offset = Vector2( 0, 0 )
119119
0/modulate = Color( 1, 1, 1, 1 )
120-
0/region = Rect2( 0, 0, ${this.tileset.imageWidth}, ${this.tileset.imageHeight} )
120+
0/region = Rect2( ${this.tileset.margin}, ${this.tileset.margin}, ${this.tileset.imageWidth}, ${this.tileset.imageHeight} )
121121
0/tile_mode = 2
122122
0/autotile/icon_coordinate = Vector2( 0, 0 )
123123
0/autotile/tile_size = Vector2( ${this.tileset.tileWidth}, ${this.tileset.tileHeight} )
124-
0/autotile/spacing = 0
124+
0/autotile/spacing = ${this.tileset.tileSpacing}
125125
0/autotile/occluder_map = [ ]
126126
0/autotile/navpoly_map = [ ]
127127
0/autotile/priority_map = [ ]

0 commit comments

Comments
 (0)