Skip to content

Commit 9bfcc1a

Browse files
authoredNov 29, 2020
Merge pull request #20 from agmcleod/navigation
Navigation defined via tile object
2 parents 6409edd + c37e566 commit 9bfcc1a

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed
 

‎README.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tiled plugins for exporting Tilemaps and Tilesets in Godot 3.2 format
55
- export_to_godot_tilemap.js
66
- export_to_godot_tileset.js
77
- utils.js
8-
8+
99
The plugin requires Tiled version 1.3.4 or newer.
1010

1111
More information about the Tilemap structure of Godot can be found here:
@@ -35,14 +35,14 @@ When you want to add these plugins to your Tiled installation:
3535
subdirectory.
3636

3737
(Alternatively, clone this git repository into the extensions directory)
38-
38+
3939
Tiled extension directory is:
40-
40+
4141
- **Windows**
4242
`C:/Users/<USER>/AppData/Local/Tiled/extensions/`
4343
- **macOS**
4444
`~/Library/Preferences/Tiled/extensions/`
45-
- **Linux**
45+
- **Linux**
4646
`~/.config/tiled/extensions/`
4747

4848
* If using a version older than Tiled 1.3.3, restart Tiled, but better update your Tiled installation
@@ -66,8 +66,8 @@ Either way the value of projectRoot is transformed to a resource path which Godo
6666
**_!!! Pay attention to the "/" instead of standard windows "\\".
6767
Single Backslash "\\" is used for escaping special symbols._**
6868

69-
This is needed so when you export to a subfolder in your Godot project all the relative
70-
paths for the resources `(res://)` are set correctly and relative to the custom property
69+
This is needed so when you export to a subfolder in your Godot project all the relative
70+
paths for the resources `(res://)` are set correctly and relative to the custom property
7171
you've added `"projectRoot"`;
7272

7373
If everything is fine when you go to _File -> Export As_, a new option should exist:
@@ -105,6 +105,15 @@ if Input.is_action_just_pressed("reload_scene"):
105105
```
106106
Don't forget to add key/mouse/controller mapping for the "reload_scene" action ;)
107107

108+
### Tileset objects for collisions & navigation
109+
110+
To setup a collision shape for a tile, first edit the tileset in Tiled:
111+
112+
- Select the tile in the tileset file
113+
- You should see a side panel "Tile Collision Editor", use the menu above it to create a new shape. Use rectangle or polygon only.
114+
115+
For navigation, do the same thing, but with the Object shape selected, set the `Type` field to `navigation`.
116+
108117
## Why use it?
109118

110119
The main focus was easily editing and creating new maps and tilesets.
@@ -124,7 +133,7 @@ More about my struggles can be read in Tiled Forum or Godot reddit. Check the Co
124133
- [ ] Export visibility and opacity from layers
125134
- [x] Export collision shapes<sup>*</sup>
126135
- [ ] Export occluder shapes<sup>*</sup>
127-
- [ ] Export navigation shapes<sup>*</sup>
136+
- [x] Export navigation shapes<sup>*</sup>
128137
- [ ] Support for one-way collision shapes
129138
- [ ] Support for image layers
130139
- [x] Support for tile objects, which are exported to Godot as Sprite nodes. (Other types of objects are not yet included.)
@@ -133,7 +142,7 @@ More about my struggles can be read in Tiled Forum or Godot reddit. Check the Co
133142
- [ ] Custom properties for maps, layers, tilesets, and objects are exported as metadata. Custom properties on tiles can be exported into the TileSet resource
134143
- [ ] Map background layer exported as a parallax background
135144

136-
Legend: ticked = done, unticked = to do
145+
Legend: ticked = done, unticked = to do
137146

138147
\* The Godot tileset editor supports only Rectangle and Polygon. That's Tiled are supported and are converted to polygons in Godot.
139148

‎export_to_godot_tileset.js

+60-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class GodotTilesetExporter {
1212
this.spriteImagePath = this.spriteImagePath.replace(/^\/+/, '');
1313
this.shapesResources = "";
1414
this.shapes = "";
15+
this.navpolyMap = [];
1516
this.firstShapeID = "0";
1617
};
1718

@@ -37,6 +38,10 @@ class GodotTilesetExporter {
3738
// noinspection JSUnresolvedVariable
3839
let tiles = this.tileset.tiles;
3940

41+
let minNavId = tiles.reduce((id, tile) => {
42+
return Math.max(id, tile.id)
43+
}, 0);
44+
4045
for (let index = 0; index < tiles.length; index++) {
4146

4247
let tile = tiles[index];
@@ -62,8 +67,13 @@ class GodotTilesetExporter {
6267
// noinspection JSUnresolvedVariable
6368
let object = tileObjects[oIndex];
6469

65-
//TODO: add occlusions, navigations
66-
this.exportCollisions(object, tile, autotileCoordinates);
70+
//TODO: add occlusions
71+
if (object.type === "navigation") {
72+
minNavId++;
73+
this.exportNavigations(object, minNavId, autotileCoordinates);
74+
} else {
75+
this.exportCollisions(object, tile, autotileCoordinates);
76+
}
6777
}
6878
}
6979
}
@@ -84,7 +94,16 @@ class GodotTilesetExporter {
8494
this.shapesResources += this.getCollisionShapeRectangle(tile.id, object);
8595
this.exportShapes(tile, autotileCoordinates);
8696
}
97+
}
8798

99+
exportNavigations(object, id, autotileCoordinates) {
100+
if (object.polygon.length > 0) {
101+
this.shapesResources += this.getNavigationShapePolygon(id, object);
102+
this.exportNavigationShape(id, autotileCoordinates);
103+
} else if (object.width > 0 && object.height > 0) {
104+
this.shapesResources += this.getNavigationShapeRectangle(id, object);
105+
this.exportNavigationShape(id, autotileCoordinates);
106+
}
88107
}
89108

90109
exportShapes(tile, autotileCoordinates) {
@@ -98,6 +117,11 @@ class GodotTilesetExporter {
98117
);
99118
}
100119

120+
exportNavigationShape(id, autotileCoordinates) {
121+
this.navpolyMap.push(`Vector2( ${autotileCoordinates.x}, ${autotileCoordinates.y} )`)
122+
this.navpolyMap.push(`SubResource( ${id} )`)
123+
}
124+
101125
getTilesetTemplate() {
102126
// noinspection JSUnresolvedVariable
103127
return `[gd_resource type="TileSet" load_steps=3 format=2]
@@ -115,7 +139,7 @@ ${this.shapesResources}[resource]
115139
0/autotile/tile_size = Vector2( ${this.tileset.tileWidth}, ${this.tileset.tileHeight} )
116140
0/autotile/spacing = ${this.tileset.tileSpacing}
117141
0/autotile/occluder_map = [ ]
118-
0/autotile/navpoly_map = [ ]
142+
0/autotile/navpoly_map = [ ${this.navpolyMap.join(', ')} ]
119143
0/autotile/priority_map = [ ]
120144
0/autotile/z_index_map = [ ]
121145
0/occluder_offset = Vector2( 0, 0 )
@@ -166,6 +190,39 @@ points = PoolVector2Array( ${coordinateString} )
166190
return `[sub_resource type="ConvexPolygonShape2D" id=${id}]
167191
points = PoolVector2Array( ${topLeft.x}, ${topLeft.y}, ${topRight.x}, ${topRight.y}, ${bottomRight.x}, ${bottomRight.y}, ${bottomLeft.x}, ${bottomLeft.y} )
168192
193+
`;
194+
}
195+
196+
getNavigationShapePolygon(id, object) {
197+
let coordinateString = "";
198+
// noinspection JSUnresolvedVariable
199+
object.polygon.forEach((coordinate) => {
200+
let coordinateX = object.x + coordinate.x;
201+
let coordinateY = object.y + coordinate.y;
202+
coordinateString += coordinateX + ", " + coordinateY + ", ";
203+
});
204+
// Remove trailing commas and blank
205+
coordinateString = coordinateString.replace(/,\s*$/, "");
206+
return `[sub_resource type="NavigationPolygon" id=${id}]
207+
vertices = PoolVector2Array( ${coordinateString} )
208+
polygons = [ PoolIntArray ( ${object.polygon.map((_value, index) => index).join(', ')} ) ]
209+
210+
`;
211+
}
212+
213+
getNavigationShapeRectangle(id, object) {
214+
const topLeft = { x: object.x, y: object.y };
215+
const topRight = { x: object.x + object.width, y: object.y };
216+
const bottomRight = {
217+
x: object.x + object.width,
218+
y: object.y + object.height,
219+
};
220+
const bottomLeft = { x: object.x, y: object.y + object.height };
221+
222+
return `[sub_resource type="NavigationPolygon" id=${id}]
223+
vertices = PoolVector2Array( ${topLeft.x}, ${topLeft.y}, ${topRight.x}, ${topRight.y}, ${bottomRight.x}, ${bottomRight.y}, ${bottomLeft.x}, ${bottomLeft.y} )
224+
polygons = [ PoolIntArray( 0, 1, 2, 3 ) ]
225+
169226
`;
170227
}
171228

0 commit comments

Comments
 (0)
Please sign in to comment.