* @zh 在导航网格上寻路 * @en Find path on navigation mesh
(
startX: number,
startY: number,
endX: number,
endY: number,
options?: IPathfindingOptions
)
| 340 | * @en Find path on navigation mesh |
| 341 | */ |
| 342 | findPath( |
| 343 | startX: number, |
| 344 | startY: number, |
| 345 | endX: number, |
| 346 | endY: number, |
| 347 | options?: IPathfindingOptions |
| 348 | ): IPathResult { |
| 349 | const opts = { ...DEFAULT_PATHFINDING_OPTIONS, ...options }; |
| 350 | |
| 351 | const startPolygon = this.findPolygonAt(startX, startY); |
| 352 | const endPolygon = this.findPolygonAt(endX, endY); |
| 353 | |
| 354 | if (!startPolygon || !endPolygon) { |
| 355 | return EMPTY_PATH_RESULT; |
| 356 | } |
| 357 | |
| 358 | // Same polygon |
| 359 | if (startPolygon.id === endPolygon.id) { |
| 360 | return { |
| 361 | found: true, |
| 362 | path: [createPoint(startX, startY), createPoint(endX, endY)], |
| 363 | cost: euclideanDistance( |
| 364 | createPoint(startX, startY), |
| 365 | createPoint(endX, endY) |
| 366 | ), |
| 367 | nodesSearched: 1 |
| 368 | }; |
| 369 | } |
| 370 | |
| 371 | // A* on polygon graph |
| 372 | const polygonPath = this.findPolygonPath(startPolygon, endPolygon, opts); |
| 373 | |
| 374 | if (!polygonPath.found) { |
| 375 | return EMPTY_PATH_RESULT; |
| 376 | } |
| 377 | |
| 378 | // Convert polygon path to point path using funnel algorithm |
| 379 | const start = createPoint(startX, startY); |
| 380 | const end = createPoint(endX, endY); |
| 381 | const pointPath = this.funnelPath(start, end, polygonPath.polygons); |
| 382 | |
| 383 | return { |
| 384 | found: true, |
| 385 | path: pointPath, |
| 386 | cost: this.calculatePathLength(pointPath), |
| 387 | nodesSearched: polygonPath.nodesSearched |
| 388 | }; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @zh 在多边形图上寻路 |
nothing calls this directly
no test coverage detected