From 8eec8ec0b015af30750252941c2fa92d42d09ba3 Mon Sep 17 00:00:00 2001 From: Aaron McLeod Date: Fri, 6 Nov 2020 16:37:30 -0500 Subject: [PATCH] function refactor --- export_to_godot_tilemap.js | 25 ++++--------------------- export_to_godot_tileset.js | 15 +-------------- utils.js | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/export_to_godot_tilemap.js b/export_to_godot_tilemap.js index b3f2711..a20c67e 100644 --- a/export_to_godot_tilemap.js +++ b/export_to_godot_tilemap.js @@ -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} ) @@ -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: "", @@ -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); diff --git a/export_to_godot_tileset.js b/export_to_godot_tileset.js index 172f68b..26036c7 100644 --- a/export_to_godot_tileset.js +++ b/export_to_godot_tileset.js @@ -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; @@ -98,19 +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 - Math.max(this.tileset.margin - this.tileset.tileSpacing / 2, 0)) - / (this.tileset.tileWidth + this.tileset.tileSpacing) - ); - } - getTilesetTemplate() { // noinspection JSUnresolvedVariable return `[gd_resource type="TileSet" load_steps=3 format=2] diff --git a/utils.js b/utils.js index 368eacd..a97684f 100644 --- a/utils.js +++ b/utils.js @@ -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); +}