forked from noxious/server
A* and move logic improvements
This commit is contained in:
@ -3,10 +3,10 @@ export type Node = Position & { parent?: Node; g: number; h: number; f: number }
|
||||
|
||||
export class AStar {
|
||||
private static readonly DIRECTIONS = [
|
||||
{ x: 0, y: -1 },
|
||||
{ x: 0, y: 1 },
|
||||
{ x: -1, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 0, y: -1 }, // Up
|
||||
{ x: 0, y: 1 }, // Down
|
||||
{ x: -1, y: 0 }, // Left
|
||||
{ x: 1, y: 0 }, // Right
|
||||
{ x: -1, y: -1 },
|
||||
{ x: -1, y: 1 },
|
||||
{ x: 1, y: -1 },
|
||||
@ -54,7 +54,8 @@ export class AStar {
|
||||
private static getDistance(a: Position, b: Position): number {
|
||||
const dx = Math.abs(a.x - b.x),
|
||||
dy = Math.abs(a.y - b.y)
|
||||
return Math.sqrt(dx * dx + dy * dy)
|
||||
// Manhattan distance for straight paths, then Euclidean for diagonals
|
||||
return dx + dy + (Math.sqrt(2) - 2) * Math.min(dx, dy)
|
||||
}
|
||||
|
||||
private static reconstructPath(endNode: Node): Node[] {
|
||||
|
Reference in New Issue
Block a user