@@ -53,6 +53,11 @@ class Point {
53
53
}
54
54
}
55
55
56
+ export interface PathingOptions {
57
+ pathingDiameter ?: number ;
58
+ ignoreDestination ?: boolean ;
59
+ }
60
+
56
61
export class Pathfinding {
57
62
58
63
private currentPoint : Point ;
@@ -63,8 +68,12 @@ export class Pathfinding {
63
68
public constructor ( private actor : Actor ) {
64
69
}
65
70
66
- public walkTo ( position : Position , pathingDiameter : number = 16 ) : void {
67
- const path = this . pathTo ( position . x , position . y , pathingDiameter ) ;
71
+ public async walkTo ( position : Position , options : PathingOptions ) : Promise < void > {
72
+ if ( ! options . pathingDiameter ) {
73
+ options . pathingDiameter = 16 ;
74
+ }
75
+
76
+ const path = await this . pathTo ( position . x , position . y , options . pathingDiameter ) ;
68
77
69
78
if ( ! path ) {
70
79
throw new Error ( `Unable to find path.` ) ;
@@ -79,17 +88,23 @@ export class Pathfinding {
79
88
walkingQueue . clear ( ) ;
80
89
walkingQueue . valid = true ;
81
90
91
+ if ( options . ignoreDestination ) {
92
+ path . splice ( path . length - 1 , 1 ) ;
93
+ }
94
+
82
95
for ( const point of path ) {
83
96
walkingQueue . add ( point . x , point . y ) ;
84
97
}
85
98
}
86
99
87
- public pathTo ( destinationX : number , destinationY : number , diameter : number = 16 ) : Point [ ] {
100
+ public async pathTo ( destinationX : number , destinationY : number , diameter : number = 16 ) : Promise < Point [ ] > {
88
101
// @TODO check if destination is too far away
89
102
90
103
const radius = Math . floor ( diameter / 2 ) ;
91
104
const pathingStartX = this . actor . position . x - radius ;
92
105
const pathingStartY = this . actor . position . y - radius ;
106
+ const destinationIndexX = destinationX - pathingStartX ;
107
+ const destinationIndexY = destinationY - pathingStartY ;
93
108
94
109
if ( destinationX < pathingStartX || destinationY < pathingStartY ) {
95
110
throw new Error ( `Pathing diameter too small!` ) ;
@@ -112,7 +127,7 @@ export class Pathfinding {
112
127
while ( this . openPoints . length > 0 ) {
113
128
this . currentPoint = this . calculateBestPoint ( ) ;
114
129
115
- if ( this . currentPoint === this . points [ destinationX - pathingStartX ] [ destinationY - pathingStartY ] ) {
130
+ if ( this . currentPoint === this . points [ destinationIndexX ] [ destinationIndexY ] ) {
116
131
break ;
117
132
}
118
133
@@ -121,32 +136,38 @@ export class Pathfinding {
121
136
122
137
let level = this . actor . position . level ;
123
138
let { x, y, indexX, indexY } = this . currentPoint ;
139
+ let canPath = false ;
124
140
125
141
// West
126
142
if ( indexX > 0 && this . canPathNSEW ( new Position ( x - 1 , y , level ) , 0x1280108 ) ) {
127
143
this . calculateCost ( this . points [ indexX - 1 ] [ indexY ] ) ;
144
+ canPath = true ;
128
145
}
129
146
130
147
// East
131
148
if ( indexX < pointLen - 1 && this . canPathNSEW ( new Position ( x + 1 , y , level ) , 0x1280180 ) ) {
132
149
this . calculateCost ( this . points [ indexX + 1 ] [ indexY ] ) ;
150
+ canPath = true ;
133
151
}
134
152
135
153
// South
136
154
if ( indexY > 0 && this . canPathNSEW ( new Position ( x , y - 1 , level ) , 0x1280102 ) ) {
137
155
this . calculateCost ( this . points [ indexX ] [ indexY - 1 ] ) ;
156
+ canPath = true ;
138
157
}
139
158
140
159
// North
141
160
if ( indexY < pointLen - 1 && this . canPathNSEW ( new Position ( x , y + 1 , level ) , 0x1280120 ) ) {
142
161
this . calculateCost ( this . points [ indexX ] [ indexY + 1 ] ) ;
162
+ canPath = true ;
143
163
}
144
164
145
165
// South-West
146
166
if ( indexX > 0 && indexY > 0 ) {
147
167
if ( this . canPathDiagonally ( this . currentPoint . x , this . currentPoint . y , new Position ( x - 1 , y - 1 , level ) , - 1 , - 1 ,
148
168
0x128010e , 0x1280108 , 0x1280102 ) ) {
149
169
this . calculateCost ( this . points [ indexX - 1 ] [ indexY - 1 ] ) ;
170
+ canPath = true ;
150
171
}
151
172
}
152
173
@@ -155,6 +176,7 @@ export class Pathfinding {
155
176
if ( this . canPathDiagonally ( this . currentPoint . x , this . currentPoint . y , new Position ( x + 1 , y - 1 , level ) , 1 , - 1 ,
156
177
0x1280183 , 0x1280180 , 0x1280102 ) ) {
157
178
this . calculateCost ( this . points [ indexX + 1 ] [ indexY - 1 ] ) ;
179
+ canPath = true ;
158
180
}
159
181
}
160
182
@@ -163,6 +185,7 @@ export class Pathfinding {
163
185
if ( this . canPathDiagonally ( this . currentPoint . x , this . currentPoint . y , new Position ( x - 1 , y + 1 , level ) , - 1 , 1 ,
164
186
0x1280138 , 0x1280108 , 0x1280120 ) ) {
165
187
this . calculateCost ( this . points [ indexX - 1 ] [ indexY + 1 ] ) ;
188
+ canPath = true ;
166
189
}
167
190
}
168
191
@@ -171,11 +194,16 @@ export class Pathfinding {
171
194
if ( this . canPathDiagonally ( this . currentPoint . x , this . currentPoint . y , new Position ( x + 1 , y + 1 , level ) , 1 , 1 ,
172
195
0x12801e0 , 0x1280180 , 0x1280120 ) ) {
173
196
this . calculateCost ( this . points [ indexX + 1 ] [ indexY + 1 ] ) ;
197
+ canPath = true ;
174
198
}
175
199
}
200
+
201
+ if ( ! canPath ) {
202
+ break ;
203
+ }
176
204
}
177
205
178
- const destinationPoint = this . points [ destinationX - pathingStartX ] [ destinationY - pathingStartY ] ;
206
+ const destinationPoint = this . points [ destinationIndexX ] [ destinationIndexY ] ;
179
207
180
208
if ( destinationPoint === null || destinationPoint . parent === null ) {
181
209
return null ;
0 commit comments