@@ -12,6 +12,7 @@ class GodotTilesetExporter {
12
12
this . spriteImagePath = this . spriteImagePath . replace ( / ^ \/ + / , '' ) ;
13
13
this . shapesResources = "" ;
14
14
this . shapes = "" ;
15
+ this . navpolyMap = [ ] ;
15
16
this . firstShapeID = "0" ;
16
17
} ;
17
18
@@ -37,6 +38,10 @@ class GodotTilesetExporter {
37
38
// noinspection JSUnresolvedVariable
38
39
let tiles = this . tileset . tiles ;
39
40
41
+ let minNavId = tiles . reduce ( ( id , tile ) => {
42
+ return Math . max ( id , tile . id )
43
+ } , 0 ) ;
44
+
40
45
for ( let index = 0 ; index < tiles . length ; index ++ ) {
41
46
42
47
let tile = tiles [ index ] ;
@@ -62,8 +67,13 @@ class GodotTilesetExporter {
62
67
// noinspection JSUnresolvedVariable
63
68
let object = tileObjects [ oIndex ] ;
64
69
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
+ }
67
77
}
68
78
}
69
79
}
@@ -84,7 +94,16 @@ class GodotTilesetExporter {
84
94
this . shapesResources += this . getCollisionShapeRectangle ( tile . id , object ) ;
85
95
this . exportShapes ( tile , autotileCoordinates ) ;
86
96
}
97
+ }
87
98
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
+ }
88
107
}
89
108
90
109
exportShapes ( tile , autotileCoordinates ) {
@@ -98,6 +117,11 @@ class GodotTilesetExporter {
98
117
) ;
99
118
}
100
119
120
+ exportNavigationShape ( id , autotileCoordinates ) {
121
+ this . navpolyMap . push ( `Vector2( ${ autotileCoordinates . x } , ${ autotileCoordinates . y } )` )
122
+ this . navpolyMap . push ( `SubResource( ${ id } )` )
123
+ }
124
+
101
125
getTilesetTemplate ( ) {
102
126
// noinspection JSUnresolvedVariable
103
127
return `[gd_resource type="TileSet" load_steps=3 format=2]
@@ -115,7 +139,7 @@ ${this.shapesResources}[resource]
115
139
0/autotile/tile_size = Vector2( ${ this . tileset . tileWidth } , ${ this . tileset . tileHeight } )
116
140
0/autotile/spacing = ${ this . tileset . tileSpacing }
117
141
0/autotile/occluder_map = [ ]
118
- 0/autotile/navpoly_map = [ ]
142
+ 0/autotile/navpoly_map = [ ${ this . navpolyMap . join ( ', ' ) } ]
119
143
0/autotile/priority_map = [ ]
120
144
0/autotile/z_index_map = [ ]
121
145
0/occluder_offset = Vector2( 0, 0 )
@@ -166,6 +190,39 @@ points = PoolVector2Array( ${coordinateString} )
166
190
return `[sub_resource type="ConvexPolygonShape2D" id=${ id } ]
167
191
points = PoolVector2Array( ${ topLeft . x } , ${ topLeft . y } , ${ topRight . x } , ${ topRight . y } , ${ bottomRight . x } , ${ bottomRight . y } , ${ bottomLeft . x } , ${ bottomLeft . y } )
168
192
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
+
169
226
` ;
170
227
}
171
228
0 commit comments