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 191 192 193 194 195 196 197 198
| public enum ObsType { None, Wall, Water, Cliff }
public class AStarNode { public ObsType type; public int x; public int y; public float g; public float h; public float f; public AStarNode father; public AStarNode(int _x,int _y,ObsType _type) { type = _type; x = _x; y = _y; } }
[Serializable] public class GridPos { public GridPos(int col, int row) { Col = col; Row = row; } public int Col; public int Row; }
public class AStarMgr { private int mapRow; private int mapCol; public AStarNode[,] nodes; private List<AStarNode> openList = new List<AStarNode>(); private List<AStarNode> closeList = new List<AStarNode>(); public void InItMapInfo(int col, int row) { this.mapCol = col; this.mapRow = row; nodes = new AStarNode[col, row]; for (int i = 0; i < col; i++) { for (int j = 0; j < row; j++) { AStarNode node = new AStarNode(i, j, Random.Range(0, 100) < 20 ? ObsType.Wall : Ob nodes[i, j] = node; } } } public List<AStarNode> FindPath(GridPos startPos, GridPos end { //判断起始点是不是在地图的范围内 if (startPos.Col < 0 || startPos.Col >= mapCol || startPos.Row < 0 || startPos.Row || endPos.Col < 0 || endPos.Col >= m || endPos.Row < 0 || endPos.Row >= m ) return null; AStarNode start = nodes[startPos.Col, startPos.Row]; AStarNode end = nodes[endPos.Col, endPos.Row]; if (start.type != ObsType.None || end.type != ObsType.Non return null; closeList.Clear(); openList.Clear(); start.father = null; start.f = 0; start.g = 0; start.h = 0; closeList.Add(start); while (true) { FindNearlyToOpenList(start.x, start.y - 1, 1.4f, star FindNearlyToOpenList(start.x - 1, start.y, 1.4f, star FindNearlyToOpenList(start.x + 1, start.y, 1.4f, star //FindNearlyToOpenList(start.x - 1, start.y + 1, 1.4f FindNearlyToOpenList(start.x, start.y + 1, 1.4f, star //FindNearlyToOpenList(start.x + 1, start.y + 1, 1.4f if (openList.Count == 0) 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; } } } public List<AStarNode> FindRange(GridPos startPos, int step) { if (startPos.Col < 0 || startPos.Col >= mapCol || startPos.Row < 0 || startPos.Row return null; AStarNode start = nodes[startPos.Col, startPos.Row]; if (start.type != ObsType.None) return null; closeList.Clear(); openList.Clear(); closeList.Add(start); while (step-- > 0) { AddNearOneStep(closeList.ToArray()); } return openList; } 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 FindNearlyToOpenList(int x, int y, float g, ASta { if (x < 0 || x >= mapCol || y < 0 || y >= mapRow) return; AStarNode node = nodes[x, y]; if (node == null || node.type != ObsType.None || 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 - no node.f = node.g + node.h; openList.Add(node); } private void AddNearOneStep(AStarNode[] curStepNodes) { closeList.Clear(); foreach (var it in curStepNodes) { AddNearNode(it.x - 1, it.y); AddNearNode(it.x, it.y - 1); AddNearNode(it.x + 1, it.y); AddNearNode(it.x, it.y + 1); } } private void AddNearNode(int x, int y) { if (x < 0 || x >= mapCol || y < 0 || y >= mapRow) return; AStarNode node = nodes[x, y]; if (node == null || node.type != ObsType.None) return; openList.Add(node); closeList.Add(node); } }
|