|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System; |
| 6 | + |
| 7 | +public class Pathfinding : MonoBehaviour { |
| 8 | + |
| 9 | + Grid grid; |
| 10 | + static Pathfinding instance; |
| 11 | + |
| 12 | + void Awake() { |
| 13 | + grid = GetComponent<Grid>(); |
| 14 | + instance = this; |
| 15 | + } |
| 16 | + |
| 17 | + public static Vector2[] RequestPath(Vector2 from, Vector2 to) { |
| 18 | + return instance.FindPath (from, to); |
| 19 | + } |
| 20 | + |
| 21 | + Vector2[] FindPath(Vector2 from, Vector2 to) { |
| 22 | + |
| 23 | + Stopwatch sw = new Stopwatch(); |
| 24 | + sw.Start(); |
| 25 | + |
| 26 | + Vector2[] waypoints = new Vector2[0]; |
| 27 | + bool pathSuccess = false; |
| 28 | + |
| 29 | + Node startNode = grid.NodeFromWorldPoint(from); |
| 30 | + Node targetNode = grid.NodeFromWorldPoint(to); |
| 31 | + startNode.parent = startNode; |
| 32 | + |
| 33 | + |
| 34 | + if (startNode.walkable && targetNode.walkable) { |
| 35 | + Heap<Node> openSet = new Heap<Node>(grid.MaxSize); |
| 36 | + HashSet<Node> closedSet = new HashSet<Node>(); |
| 37 | + openSet.Add(startNode); |
| 38 | + |
| 39 | + while (openSet.Count > 0) { |
| 40 | + Node currentNode = openSet.RemoveFirst(); |
| 41 | + closedSet.Add(currentNode); |
| 42 | + |
| 43 | + if (currentNode == targetNode) { |
| 44 | + sw.Stop(); |
| 45 | + print ("Path found: " + sw.ElapsedMilliseconds + " ms"); |
| 46 | + pathSuccess = true; |
| 47 | + break; |
| 48 | + } |
| 49 | + |
| 50 | + foreach (Node neighbour in grid.GetNeighbours(currentNode)) { |
| 51 | + if (!neighbour.walkable || closedSet.Contains(neighbour)) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour)+TurningCost(currentNode,neighbour); |
| 56 | + if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { |
| 57 | + neighbour.gCost = newMovementCostToNeighbour; |
| 58 | + neighbour.hCost = GetDistance(neighbour, targetNode); |
| 59 | + neighbour.parent = currentNode; |
| 60 | + |
| 61 | + if (!openSet.Contains(neighbour)) |
| 62 | + openSet.Add(neighbour); |
| 63 | + else |
| 64 | + openSet.UpdateItem(neighbour); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (pathSuccess) { |
| 71 | + waypoints = RetracePath(startNode,targetNode); |
| 72 | + } |
| 73 | + |
| 74 | + return waypoints; |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + int TurningCost(Node from, Node to) { |
| 80 | + return 0; |
| 81 | + Vector2 dirOld = new Vector2(from.gridX - from.parent.gridX, from.gridY - from.parent.gridY); |
| 82 | + Vector2 dirNew = new Vector2(to.gridX - from.gridX, to.gridY - from.gridY); |
| 83 | + if (dirNew == dirOld) |
| 84 | + return 0; |
| 85 | + else if (dirOld.x != 0 && dirOld.y != 0 && dirNew.x != 0 && dirNew.y != 0) { |
| 86 | + return 5; |
| 87 | + } |
| 88 | + else { |
| 89 | + return 10; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Vector2[] RetracePath(Node startNode, Node endNode) { |
| 94 | + List<Node> path = new List<Node>(); |
| 95 | + Node currentNode = endNode; |
| 96 | + |
| 97 | + while (currentNode != startNode) { |
| 98 | + path.Add(currentNode); |
| 99 | + currentNode = currentNode.parent; |
| 100 | + } |
| 101 | + Vector2[] waypoints = SimplifyPath(path); |
| 102 | + Array.Reverse(waypoints); |
| 103 | + return waypoints; |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + Vector2[] SimplifyPath(List<Node> path) { |
| 108 | + List<Vector2> waypoints = new List<Vector2>(); |
| 109 | + Vector2 directionOld = Vector2.zero; |
| 110 | + |
| 111 | + for (int i = 1; i < path.Count; i ++) { |
| 112 | + Vector2 directionNew = new Vector2(path[i-1].gridX - path[i].gridX,path[i-1].gridY - path[i].gridY); |
| 113 | + if (directionNew != directionOld) { |
| 114 | + waypoints.Add(path[i].worldPosition); |
| 115 | + } |
| 116 | + directionOld = directionNew; |
| 117 | + } |
| 118 | + return waypoints.ToArray(); |
| 119 | + } |
| 120 | + |
| 121 | + int GetDistance(Node nodeA, Node nodeB) { |
| 122 | + int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX); |
| 123 | + int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY); |
| 124 | + |
| 125 | + if (dstX > dstY) |
| 126 | + return 14*dstY + 10* (dstX-dstY); |
| 127 | + return 14*dstX + 10 * (dstY-dstX); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +} |
0 commit comments