Skip to content

Commit 5eb3f11

Browse files
authored
Merge pull request #49 from nerochiaro/use_types
Use tiled-api npm package
2 parents 5c34855 + b886f9d commit 5eb3f11

File tree

7 files changed

+172
-16
lines changed

7 files changed

+172
-16
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ Desktop.ini
4949
*.log*
5050

5151
/.idea/
52+
53+
######################
54+
# NPM
55+
######################
56+
57+
/node_modules/

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Tiled plugins for exporting Tilemaps and Tilesets in Godot 3.2 format
44

5-
- export_to_godot_tilemap.js
6-
- export_to_godot_tileset.js
7-
- utils.js
5+
- export_to_godot_tilemap.mjs
6+
- export_to_godot_tileset.mjs
7+
- utils.mjs
88

99
The plugin requires Tiled version 1.3.4 or newer.
1010

@@ -232,7 +232,7 @@ If you are using the plugins and have any problems, feature requests or have fou
232232

233233
#### Customizing or contributing tips
234234

235-
If you want to debug something you can use the utils.js
235+
If you want to debug something you can use the utils.mjs
236236
There are three useful functions:
237237
- log() - shortcut for the console.log (infinite parameters)
238238
- logf() - logs with flattening circular objects (single parameter)

export_to_godot_tilemap.mjs

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { getResPath, stringifyKeyValue, stringifyNode, splitCommaSeparated, getT
44
class GodotTilemapExporter {
55

66
// noinspection DuplicatedCode
7+
/**
8+
* Constructs a new instance of the tilemap exporter
9+
* @param {TileMap} map the tilemap to export
10+
* @param {string} fileName path of the file the tilemap should be exported to
11+
*/
712
constructor(map, fileName) {
813
this.map = map;
914
this.fileName = fileName;
@@ -62,11 +67,8 @@ class GodotTilemapExporter {
6267

6368
/**
6469
* Generate a string with all tilesets in the map.
65-
* Godot allows only one tileset per tilemap so if you use more than one tileset per layer it's not going to work.
6670
* Godot supports several image textures per tileset but Tiled Editor doesn't.
67-
* Tiled editor supports only one tile
68-
* sprite image per tileset.
69-
* @returns {string}
71+
* Tiled editor supports only one tile sprite image per tileset.
7072
*/
7173
setTilesetsString() {
7274

@@ -98,6 +100,12 @@ class GodotTilemapExporter {
98100
}
99101
}
100102

103+
/**
104+
* Handle exporting a single layer
105+
* @param {Layer} layer the target layer
106+
* @param {number} mode the layer mode
107+
* @param {string} layer_parent path of the parent of the layer
108+
*/
101109
handleLayer(layer, mode, layer_parent) {
102110
// noinspection JSUnresolvedVariable
103111
if (layer.isTileLayer) {
@@ -246,6 +254,12 @@ class GodotTilemapExporter {
246254
}
247255
}
248256

257+
/**
258+
* Prepare properties for a Godot node
259+
* @param {TiledObjectProperties} object_props Properties from the layer
260+
* @param {TiledObjectProperties} set_props The base properties for the node
261+
* @returns {TiledObjectProperties} the merged property set for the node
262+
*/
249263
merge_properties(object_props, set_props){
250264
for (const [key, value] of Object.entries(object_props)) {
251265
if(key.startsWith("godot:node:")){
@@ -255,7 +269,12 @@ class GodotTilemapExporter {
255269

256270
return set_props;
257271
}
258-
272+
273+
/**
274+
* Prepare the meta properties for a Godot node
275+
* @param {TiledObjectProperties} object_props
276+
* @returns {object} the meta properties
277+
*/
259278
meta_properties(object_props){
260279
let results = {};
261280
for (const [key, value] of Object.entries(object_props)) {
@@ -276,11 +295,23 @@ class GodotTilemapExporter {
276295
}
277296

278297
/**
279-
* Creates all the tiles coordinate for the current layer and picks the first tileset which is used.
280-
* It's important to not use more than one tileset for a layer.
281-
* Otherwise the tiles from the second layer are going to be displayed incorrectly as tiles form the first
282-
* or with a wrong index leading to crash on export.
283-
* @returns {{tilesetID: *, poolIntArrayString: string, layerName: *}}
298+
* @typedef {{
299+
* tileset: Tileset,
300+
* tilesetID: number?,
301+
* tilesetColumns: number,
302+
* layer: Layer,
303+
* isEmpty: boolean,
304+
* poolIntArrayString: string,
305+
* parent: string
306+
* }} LayerData
307+
*/
308+
309+
/**
310+
* Creates all the tiles coordinates for a layer.
311+
* Each element in the retuned array corresponds to the tile coordinates for each of
312+
* the tilesets used in the layer.
313+
* @param {TileLayer} layer the target layer
314+
* @returns {LayerData[]} the data about the tilesets used in the target layer
284315
*/
285316
getLayerData(layer) {
286317
// noinspection JSUnresolvedVariable
@@ -370,10 +401,20 @@ class GodotTilemapExporter {
370401
return tilesetList;
371402
}
372403

404+
/**
405+
* Find the id of a tileset by its name
406+
* @param {Tileset} tileset The tileset to find the id of
407+
* @returns {string|undefined} the id of the tileset if found, undefined otherwise
408+
*/
373409
getTilesetIDByTileset(tileset) {
374410
return this.tilesetsIndex.get(tileset.name);
375411
}
376412

413+
/**
414+
* Calculate the second parameter for the given cell
415+
* @param {cell} cell the target cell
416+
* @returns {number} the second parameter
417+
*/
377418
getSecondParam(cell) {
378419
/**
379420
* no rotation or flips
@@ -523,6 +564,12 @@ ${this.tileMapsString}
523564

524565
/**
525566
* Template for a tilemap node
567+
* @param {string} tileMapName
568+
* @param {number} mode
569+
* @param {number} tilesetID
570+
* @param {string} poolIntArrayString
571+
* @param {Layer} layer
572+
* @param {string} parent
526573
* @returns {string}
527574
*/
528575
getTileMapTemplate(tileMapName, mode, tilesetID, poolIntArrayString, layer, parent = ".") {
@@ -563,9 +610,16 @@ const customTileMapFormat = {
563610
name: "Godot Tilemap format",
564611
extension: "tscn",
565612

613+
/**
614+
* Map exporter function
615+
* @param {TileMap} map the map to export
616+
* @param {string} fileName path of the file where to export the map
617+
* @returns {undefined}
618+
*/
566619
write: function (map, fileName) {
567620
const exporter = new GodotTilemapExporter(map, fileName);
568621
exporter.write();
622+
return undefined;
569623
}
570624
};
571625

export_to_godot_tileset.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { getResPath, getTilesetColumns } from './utils.mjs';
44
class GodotTilesetExporter {
55

66
// noinspection DuplicatedCode
7+
/**
8+
* Constructs a new instance of the tileset exporter
9+
* @param {Tileset} tileset the tileset to export
10+
* @param {string} fileName path of the file the tileset should be exported to
11+
*/
712
constructor(tileset, fileName) {
813
this.tileset = tileset;
914
this.fileName = fileName;
@@ -89,6 +94,12 @@ class GodotTilesetExporter {
8994
this.shapes = this.shapes.replace(/,\s*$/, "");
9095
}
9196

97+
/**
98+
* Exports the collision shape for an object
99+
* @param {MapObject} object the object to export collision shape from
100+
* @param {Tile} tile the tile the object is part of
101+
* @param {point} autotileCoordinates autotile coordinates for the tile
102+
*/
92103
exportCollisions(object, tile, autotileCoordinates) {
93104
// noinspection JSUnresolvedVariable
94105
if (object.polygon.length > 0) {
@@ -100,6 +111,12 @@ class GodotTilesetExporter {
100111
}
101112
}
102113

114+
/**
115+
* Exports the navigation shapes for an object
116+
* @param {MapObject} object the object to export navigation shapes from
117+
* @param {number} id the navigation shape id
118+
* @param {point} autotileCoordinates autotile coordinates for the tile
119+
*/
103120
exportNavigations(object, id, autotileCoordinates) {
104121
if (object.polygon.length > 0) {
105122
this.shapesResources += this.getNavigationShapePolygon(id, object);
@@ -110,6 +127,11 @@ class GodotTilesetExporter {
110127
}
111128
}
112129

130+
/**
131+
* Exports the shape resources for a tile
132+
* @param {Tile} tile the target tile
133+
* @param {point} autotileCoordinates autotile coordinates for the tile
134+
*/
113135
exportShapes(tile, autotileCoordinates) {
114136
if (this.firstShapeID === "") {
115137
this.firstShapeID = 'SubResource( ' + tile.id + ' )';
@@ -121,6 +143,11 @@ class GodotTilesetExporter {
121143
);
122144
}
123145

146+
/**
147+
* Export a single navigation shape
148+
* @param {number} id id of the shape
149+
* @param {point} autotileCoordinates autotile coordinates for the tile
150+
*/
124151
exportNavigationShape(id, autotileCoordinates) {
125152
this.navpolyMap.push(`Vector2( ${autotileCoordinates.x}, ${autotileCoordinates.y} )`)
126153
this.navpolyMap.push(`SubResource( ${id} )`)
@@ -236,9 +263,16 @@ const customTilesetFormat = {
236263
name: "Godot Tileset format",
237264
extension: "tres",
238265

266+
/**
267+
* Tileset exporter function
268+
* @param {Tileset} tileset the tileset to export
269+
* @param {string} fileName path of the file where to export the tileset
270+
* @returns {undefined}
271+
*/
239272
write: function (tileset, fileName) {
240273
const exporter = new GodotTilesetExporter(tileset, fileName);
241274
exporter.write();
275+
return undefined;
242276
}
243277
};
244278

package-lock.json

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "tiled-to-godot-export",
3+
"version": "1.8.0",
4+
"description": "Tiled plugins for exporting Tilemaps and Tilesets in Godot 3.x format",
5+
"main": "export_to_godot_tilemap.mjs",
6+
"repository": {
7+
"type": "git",
8+
"url": "git@github.com:mapeditor/tiled-to-godot-export.git"
9+
},
10+
"author": "Mihail Ilinov <mihail@ilinov.eu>",
11+
"license": "GPL-2.0-or-later",
12+
"bugs": {
13+
"url": "https://github.com/mapeditor/tiled-to-godot-export/issues"
14+
},
15+
"homepage": "https://github.com/mapeditor/tiled-to-godot-export",
16+
"devDependencies": {
17+
"@mapeditor/tiled-api": "^1.8.2"
18+
}
19+
}

utils.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ export function stringifyNode(nodeProperties, contentProperties = {}, metaProper
153153
*
154154
* @param {string} key
155155
* @param {string|array} value
156-
* @param {bool} quote
157-
* @param {bool} spaces
156+
* @param {boolean} quoteKey
157+
* @param {boolean} quoteValue
158+
* @param {boolean} spaces
159+
* @param {string} separator
158160
*/
159161
export function stringifyKeyValue(key, value, quoteKey, quoteValue, spaces, separator = "=") {
160162
// flatten arrays

0 commit comments

Comments
 (0)