Skip to content

Commit 6743be6

Browse files
authoredJul 17, 2020
Merge pull request #14 from devharts/object-layers
Object layers
2 parents 8abadcb + 1cefb4c commit 6743be6

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed
 

‎export_to_godot_tilemap.js

+61-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class GodotTilemapExporter {
1010
this.tileOffset = 65536;
1111
this.tileMapsString = "";
1212
this.tilesetsString = "";
13+
this.extResourceId = 0;
1314

1415
/**
1516
* Tiled doesn't have tileset ID so we create a map
@@ -48,11 +49,11 @@ class GodotTilemapExporter {
4849
for (let index = 0; index < this.map.tilesets.length; ++index) {
4950
// noinspection JSUnresolvedVariable
5051
const tileset = this.map.tilesets[index];
51-
const tilesetID = index + 1;
52-
this.tilesetsIndex.set(tileset.name, tilesetID);
52+
this.extResourceId = index + 1;
53+
this.tilesetsIndex.set(tileset.name, this.extResourceId);
5354
// noinspection JSUnresolvedVariable
5455
let tilesetPath = tileset.asset.fileName.replace(this.projectRoot, "").replace('.tsx', '.tres');
55-
this.tilesetsString += this.getTilesetResourceTemplate(tilesetID, tilesetPath);
56+
this.tilesetsString += this.getTilesetResourceTemplate(this.extResourceId, tilesetPath, "TileSet");
5657
}
5758

5859
}
@@ -78,6 +79,39 @@ class GodotTilemapExporter {
7879
this.tileMapsString += this.getTileMapTemplate(tileMapName, ld.tilesetID, ld.poolIntArrayString, ld.parent, layer.map.tileWidth, layer.map.tileHeight);
7980
}
8081
}
82+
} else if (layer.isObjectLayer) {
83+
this.tileMapsString += `
84+
85+
[node name="${layer.name}" type="Node2D" parent="."]`;
86+
for (const object of layer.objects) {
87+
if (object.tile) {
88+
let tilesetsIndexKey = object.tile.tileset.name + "_Image";
89+
let textureResourceId = 0;
90+
if (!this.tilesetsIndex.get(tilesetsIndexKey)) {
91+
this.extResourceId = this.extResourceId + 1;
92+
textureResourceId = this.extResourceId;
93+
this.tilesetsIndex.set(tilesetsIndexKey, this.extResourceId);
94+
let tilesetPath = object.tile.tileset.image.replace(this.projectRoot, "");
95+
this.tilesetsString += this.getTilesetResourceTemplate(this.extResourceId, tilesetPath, "Texture");
96+
} else {
97+
textureResourceId = this.tilesetsIndex.get(tilesetsIndexKey);
98+
}
99+
100+
let tileOffset = this.getTileOffset(object.tile.tileset, object.tile.id);
101+
102+
// Account for anchoring in Godot (corner vs. middle):
103+
let objectPositionX = object.x + (object.tile.width / 2);
104+
let objectPositionY = object.y - (object.tile.height / 2);
105+
106+
this.tileMapsString += `
107+
108+
[node name="${object.name}" type="Sprite" parent="${layer.name}"]
109+
position = Vector2( ${objectPositionX}, ${objectPositionY} )
110+
texture = ExtResource( ${textureResourceId} )
111+
region_enabled = true
112+
region_rect = Rect2( ${tileOffset.x}, ${tileOffset.y}, ${object.tile.width}, ${object.tile.height} )`;
113+
}
114+
}
81115
}
82116
}
83117
}
@@ -305,6 +339,26 @@ class GodotTilemapExporter {
305339
return Math.floor(calculatedColumnCount);
306340
}
307341

342+
/**
343+
* Calculate the X and Y offset (in pixels) for the specified tile
344+
* ID within the specified tileset image.
345+
*
346+
* @param {Tileset} tileset - The full Tileset object
347+
* @param {int} tileId - Id for the tile to extract offset for
348+
* @returns {object} - An object with pixel offset in the format {x: int, y: int}
349+
*/
350+
getTileOffset(tileset, tileId) {
351+
let columnCount = this.getTilesetColumns(tileset);
352+
let row = Math.floor(tileId / columnCount);
353+
let col = tileId % columnCount;
354+
let xOffset = tileset.margin + (tileset.tileSpacing * col);
355+
let yOffset = tileset.margin + (tileset.tileSpacing * row);
356+
return {
357+
x: (col * tileset.tileWidth) + xOffset,
358+
y: (row * tileset.tileHeight) + yOffset
359+
};
360+
}
361+
308362
/**
309363
* Template for a scene
310364
* @returns {string}
@@ -322,8 +376,10 @@ ${this.tileMapsString}
322376
* Template for a tileset resource
323377
* @returns {string}
324378
*/
325-
getTilesetResourceTemplate(id, path) {
326-
return `[ext_resource path="res://${path}" type="TileSet" id=${id}]
379+
getTilesetResourceTemplate(id, path, type) {
380+
// Strip leading slashes to prevent invalid triple slashes in Godot res:// path:
381+
path = path.replace(/^\/+/, '');
382+
return `[ext_resource path="res://${path}" type="${type}" id=${id}]
327383
`;
328384
}
329385

‎export_to_godot_tileset.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class GodotTilesetExporter {
88
// noinspection JSUnresolvedFunction
99
this.projectRoot = getResPath(this.tileset.property("projectRoot"), fileName);
1010
this.spriteImagePath = this.tileset.image.replace(this.projectRoot, "");
11+
// Strip leading slashes to prevent invalid triple slashes in Godot res:// path:
12+
this.spriteImagePath = this.spriteImagePath.replace(/^\/+/, '');
1113
this.shapesResources = "";
1214
this.shapes = "";
1315
this.firstShapeID = "0";
@@ -30,7 +32,7 @@ class GodotTilesetExporter {
3032

3133
iterateTiles() {
3234

33-
let autotileCoordinates = {x: 0, y: 0};
35+
let autotileCoordinates = { x: 0, y: 0 };
3436

3537
// noinspection JSUnresolvedVariable
3638
let tiles = this.tileset.tiles;
@@ -166,10 +168,10 @@ points = PoolVector2Array( ${coordinateString} )
166168
}
167169

168170
getCollisionShapeRectangle(id, object) {
169-
const topLeft = {x: object.x, y: object.y};
170-
const topRight = {x: (object.x + object.width), y: object.y};
171-
const bottomRight = {x: (object.x + object.width), y: (object.y + object.height)};
172-
const bottomLeft = {x: object.x, y: (object.y + object.height)};
171+
const topLeft = { x: object.x, y: object.y };
172+
const topRight = { x: (object.x + object.width), y: object.y };
173+
const bottomRight = { x: (object.x + object.width), y: (object.y + object.height) };
174+
const bottomLeft = { x: object.x, y: (object.y + object.height) };
173175

174176
return `[sub_resource type="ConvexPolygonShape2D" id=${id}]
175177
points = PoolVector2Array( ${topLeft.x}, ${topLeft.y}, ${topRight.x}, ${topRight.y}, ${bottomRight.x}, ${bottomRight.y}, ${bottomLeft.x}, ${bottomLeft.y} )

‎utils.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ var Flatted=function(a,l){return{parse:function(n,t){var e=JSON.parse(n,i).map(f
33

44
var log = console.log.bind(console);
55

6-
function logf(data){
7-
console.log(Flatted.stringify(data));
6+
function logf(data) {
7+
console.log(Flatted.stringify(data));
88
}
9-
function logk(data){
10-
console.log(Object.keys(data));
9+
function logk(data) {
10+
console.log(Object.keys(data));
1111
}
1212

13-
function getResPath (projectRoot, outputPath) {
13+
function getResPath(projectRoot, outputPath) {
1414
const p = outputPath.split('/').slice(0, -1)
1515
// check for projectRoot
1616
// If projectRoot is not set, set it to current file's location

0 commit comments

Comments
 (0)
Please sign in to comment.