1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
public class AStarManager { private static AStarManager instance;
public static AStarManager Instance { get { if (instance == null) instance = new AStarManager(); return instance; } } private int mapW; private int mapH;
public AStarNode[,] nodes; private List<AStarNode> openList = new List<AStarNode>(); private List<AStarNode> closeList = new List<AStarNode>();
public void InitMapInfo(int w, int h) { this.mapH = h; this.mapW = w; nodes = new AStarNode[w, h]; for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) { AStarNode node = new AStarNode(i, j, Random.Range(0, 100) < 20 ? E_Node_Type.stop : E_Node_Type.walk); nodes[i, j] = node; } }
public List<AStarNode> FindPath(Vector2 startPos, Vector3 endPos) { AStarNode starNode = nodes[(int)startPos.x, (int)startPos.y]; AStarNode endNode = nodes[(int)endPos.x, (int)endPos.y]; if (startPos.x >= mapW || startPos.x < 0|| startPos.y >= mapH || startPos.y < 0|| endPos.x >= mapW || endPos.x < 0 || endPos.y >= mapH || endPos.y < 0) { Debug.Log("开始或者结束点在地图格子范围外"); return null; } AStarNode start = nodes[(int)startPos.x, (int)startPos.y]; AStarNode end = nodes[(int)endPos.x, (int)endPos.y]; if (start.type == E_Node_Type.stop || end.type == E_Node_Type.stop) { Debug.Log("开始或者结束点是障碍"); return null; }
closeList.Clear(); openList.Clear();
start.father = null; start.f = 0; start.g = 0; start.h = 0; closeList.Add(start);
while(true) { FindNearOpenList(start.x - 1, start.y - 1, 1.4f, start, end); FindNearOpenList(start.x, start.y - 1, 1, start, end); FindNearOpenList(start.x + 1, start.y - 1, 1.4f, start, end); FindNearOpenList(start.x - 1, start.y, 1, start, end); FindNearOpenList(start.x + 1, start.y, 1, start, end); FindNearOpenList(start.x - 1, start.y + 1, 1.4f, start, end); FindNearOpenList(start.x, start.y + 1, 1, start, end); FindNearOpenList(start.x + 1, start.y + 1, 1.4f, start, end);
if (openList.Count == 0) { Debug.Log("死路"); return null; }
openList.Sort(SortOpenList);
closeList.Add(openList[0]); start = openList[0]; openList.RemoveAt(0);
if (start == end) { List<AStarNode> path = new List<AStarNode>(); path.Add(end); while (end.father != null) { path.Add(end.father); end = end.father; } path.Reverse(); return path; } } }
private int SortOpenList(AStarNode a, AStarNode b) { if (a.f > b.f) return 1; else if (a.f == b.f) return 1; else return -1; }
private void FindNearOpenList(int x, int y, float g, AStarNode father, AStarNode end) { if (x >= mapW || x < 0 || y >= mapH || y < 0 ) return;
AStarNode node = nodes[x, y]; if (node == null || node.type == E_Node_Type.stop || closeList.Contains(node)|| openList.Contains(node)) return; node.father = father; node.g = father.g + g; node.h = Mathf.Abs(end.x - node.x) + Mathf.Abs(end.y - node.y); node.f = node.g + node.h; openList.Add(node); } }
|