Skip to content

Commit fc56be8

Browse files
committed
navigation first pass
1 parent 8eec8ec commit fc56be8

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

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)