Skip to content

Commit

Permalink
Merge pull request #19 from agmcleod/master
Browse files Browse the repository at this point in the history
Change to column number calculation
  • Loading branch information
MikeMnD authored Nov 7, 2020
2 parents 3ef90d1 + 8eec8ec commit 6409edd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
25 changes: 4 additions & 21 deletions export_to_godot_tilemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class GodotTilemapExporter {
let objectPositionY = object.y - (object.tile.height / 2);

this.tileMapsString += `
[node name="${object.name}" type="Sprite" parent="${layer.name}"]
position = Vector2( ${objectPositionX}, ${objectPositionY} )
texture = ExtResource( ${textureResourceId} )
Expand Down Expand Up @@ -159,7 +159,7 @@ region_rect = Rect2( ${tileOffset.x}, ${tileOffset.y}, ${object.tile.width}, ${o
tileset = {
tileset: tile.tileset,
tilesetID: null,
tilesetColumns: this.getTilesetColumns(tile.tileset),
tilesetColumns: getTilesetColumns(tile.tileset),
layer: layer,
isEmpty: tile.tileset === null,
poolIntArrayString: "",
Expand Down Expand Up @@ -322,33 +322,16 @@ region_rect = Rect2( ${tileOffset.x}, ${tileOffset.y}, ${object.tile.width}, ${o
return secondParam;
}

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

/**
* Calculate the X and Y offset (in pixels) for the specified tile
* ID within the specified tileset image.
*
*
* @param {Tileset} tileset - The full Tileset object
* @param {int} tileId - Id for the tile to extract offset for
* @returns {object} - An object with pixel offset in the format {x: int, y: int}
*/
getTileOffset(tileset, tileId) {
let columnCount = this.getTilesetColumns(tileset);
let columnCount = getTilesetColumns(tileset);
let row = Math.floor(tileId / columnCount);
let col = tileId % columnCount;
let xOffset = tileset.margin + (tileset.tileSpacing * col);
Expand Down
12 changes: 1 addition & 11 deletions export_to_godot_tileset.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GodotTilesetExporter {

let tile = tiles[index];

const tilesetColumns = this.getTilesetColumns();
const tilesetColumns = getTilesetColumns(this.tileset);
if ((autotileCoordinates.x + 1) > tilesetColumns) {
autotileCoordinates.x = 0;
autotileCoordinates.y += 1;
Expand Down Expand Up @@ -98,16 +98,6 @@ class GodotTilesetExporter {
);
}

/**
* Tileset should expose columns ... but didn't at the moment so we
* calculate them base on the image width and tileWidth
* return {number}
**/
getTilesetColumns() {
// noinspection JSUnresolvedVariable
return Math.floor((this.tileset.imageWidth - this.tileset.margin) / this.tileset.tileWidth);
}

getTilesetTemplate() {
// noinspection JSUnresolvedVariable
return `[gd_resource type="TileSet" load_steps=3 format=2]
Expand Down
17 changes: 17 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,20 @@ function getResPath(projectRoot, outputPath) {
}
return projectRoot
}

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

0 comments on commit 6409edd

Please sign in to comment.