Skip to content

Commit 6409edd

Browse files
authored
Merge pull request #19 from agmcleod/master
Change to column number calculation
2 parents 3ef90d1 + 8eec8ec commit 6409edd

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

export_to_godot_tilemap.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class GodotTilemapExporter {
104104
let objectPositionY = object.y - (object.tile.height / 2);
105105

106106
this.tileMapsString += `
107-
107+
108108
[node name="${object.name}" type="Sprite" parent="${layer.name}"]
109109
position = Vector2( ${objectPositionX}, ${objectPositionY} )
110110
texture = ExtResource( ${textureResourceId} )
@@ -159,7 +159,7 @@ region_rect = Rect2( ${tileOffset.x}, ${tileOffset.y}, ${object.tile.width}, ${o
159159
tileset = {
160160
tileset: tile.tileset,
161161
tilesetID: null,
162-
tilesetColumns: this.getTilesetColumns(tile.tileset),
162+
tilesetColumns: getTilesetColumns(tile.tileset),
163163
layer: layer,
164164
isEmpty: tile.tileset === null,
165165
poolIntArrayString: "",
@@ -322,33 +322,16 @@ region_rect = Rect2( ${tileOffset.x}, ${tileOffset.y}, ${object.tile.width}, ${o
322322
return secondParam;
323323
}
324324

325-
/**
326-
* Tileset should expose columns ... but didn't at the moment so we
327-
* calculate them base on the image width and tileWidth.
328-
* Takes into account margin (extra space around the image edges) and
329-
* tile spacing (padding between individual tiles).
330-
* @returns {number}
331-
*/
332-
getTilesetColumns(tileset) {
333-
// noinspection JSUnresolvedVariable
334-
const imageWidth = tileset.imageWidth + tileset.tileSpacing - tileset.margin
335-
const tileWidth = tileset.tileWidth + tileset.tileSpacing
336-
const calculatedColumnCount = imageWidth / tileWidth
337-
// Tiled ignores "partial" tiles (extra unaccounted for pixels in the image),
338-
// so we need to return as Math.floor to avoid throwing off the tile indices.
339-
return Math.floor(calculatedColumnCount);
340-
}
341-
342325
/**
343326
* Calculate the X and Y offset (in pixels) for the specified tile
344327
* ID within the specified tileset image.
345-
*
328+
*
346329
* @param {Tileset} tileset - The full Tileset object
347330
* @param {int} tileId - Id for the tile to extract offset for
348331
* @returns {object} - An object with pixel offset in the format {x: int, y: int}
349332
*/
350333
getTileOffset(tileset, tileId) {
351-
let columnCount = this.getTilesetColumns(tileset);
334+
let columnCount = getTilesetColumns(tileset);
352335
let row = Math.floor(tileId / columnCount);
353336
let col = tileId % columnCount;
354337
let xOffset = tileset.margin + (tileset.tileSpacing * col);

export_to_godot_tileset.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class GodotTilesetExporter {
4141

4242
let tile = tiles[index];
4343

44-
const tilesetColumns = this.getTilesetColumns();
44+
const tilesetColumns = getTilesetColumns(this.tileset);
4545
if ((autotileCoordinates.x + 1) > tilesetColumns) {
4646
autotileCoordinates.x = 0;
4747
autotileCoordinates.y += 1;
@@ -98,16 +98,6 @@ class GodotTilesetExporter {
9898
);
9999
}
100100

101-
/**
102-
* Tileset should expose columns ... but didn't at the moment so we
103-
* calculate them base on the image width and tileWidth
104-
* return {number}
105-
**/
106-
getTilesetColumns() {
107-
// noinspection JSUnresolvedVariable
108-
return Math.floor((this.tileset.imageWidth - this.tileset.margin) / this.tileset.tileWidth);
109-
}
110-
111101
getTilesetTemplate() {
112102
// noinspection JSUnresolvedVariable
113103
return `[gd_resource type="TileSet" load_steps=3 format=2]

utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,20 @@ function getResPath(projectRoot, outputPath) {
3030
}
3131
return projectRoot
3232
}
33+
34+
/**
35+
* Tileset should expose columns ... but didn't at the moment so we
36+
* calculate them base on the image width and tileWidth.
37+
* Takes into account margin (extra space around the image edges) and
38+
* tile spacing (padding between individual tiles).
39+
* @returns {number}
40+
*/
41+
function getTilesetColumns(tileset) {
42+
// noinspection JSUnresolvedVariable
43+
const imageWidth = tileset.imageWidth + tileset.tileSpacing - tileset.margin
44+
const tileWidth = tileset.tileWidth + tileset.tileSpacing
45+
const calculatedColumnCount = imageWidth / tileWidth
46+
// Tiled ignores "partial" tiles (extra unaccounted for pixels in the image),
47+
// so we need to return as Math.floor to avoid throwing off the tile indices.
48+
return Math.floor(calculatedColumnCount);
49+
}

0 commit comments

Comments
 (0)