a*寻路算法
之前做unity3d游戏时用到的a*寻路算法,整理出来记录一下
1 public struct MyVector 2 { 3 public int x; 4 public int y; 5 public MyVector( int _x, int _y ) 6 { 7 x = _x; 8 y = _y; 9 } 10 }; 11 12 public class Point 13 { 14 public MyVector position; 15 public int h; 16 public int g; 17 public Point father; 18 19 public Point() 20 { 21 } 22 public Point( MyVector _position, int _h, int _g, Point F ) 23 { 24 position = _position; 25 h = _h; 26 g = _g; 27 father = F; 28 } 29 } 30 31 public class WayFinding 32 { 33 public List<Point> Open_List = new List<Point>();//open list 34 public List<Point> Close_List = new List<Point>();//close list 35 36 private Point GetMinF() 37 { 38 Point _pMin = null; 39 foreach( Point p in Open_List ) 40 { 41 if ( _pMin == null || _pMin.g + _pMin.h > p.g + p.h ) 42 { 43 _pMin = p; 44 } 45 } 46 return _pMin; 47 } 48 private bool IsBar( Point p, int[,] map ) 49 { 50 int x = p.position.x; 51 int y = p.position.y; 52 if ( map[x, y] == 0 ) 53 { 54 return true; 55 } 56 return false; 57 } 58 private bool IsInCloseList( MyVector p ) 59 { 60 foreach ( Point _p in Close_List ) 61 { 62 if ( p.x == _p.position.x && p.y == _p.position.y ) 63 { 64 return true; 65 } 66 } 67 return false; 68 } 69 private Point GetPointFromCloseList( MyVector p ) 70 { 71 foreach ( Point _p in Close_List ) 72 { 73 if ( p.x == _p.position.x && p.y == _p.position.y ) 74 { 75 return _p; 76 } 77 } 78 return null; 79 } 80 private bool IsInOpenList( MyVector p ) 81 { 82 foreach ( Point _p in Open_List ) 83 { 84 if ( p.x == _p.position.x && p.y == _p.position.y ) 85 { 86 return true; 87 } 88 } 89 return false; 90 } 91 private Point GetPointFromOpenList( MyVector p ) 92 { 93 foreach ( Point _p in Open_List ) 94 { 95 if ( p.x == _p.position.x && p.y == _p.position.y ) 96 { 97 return _p; 98 } 99 } 100 return null; 101 } 102 private int GetG( Point p ) 103 { 104 if ( p.father == null ) 105 return 0; 106 if ( p.position.x == p.father.position.x || p.position.y == p.father.position.y ) 107 { 108 return ( p.father.g + 10 ); 109 } 110 else 111 { 112 return ( p.father.g + 14 ); 113 } 114 } 115 private int GetH( MyVector p, MyVector cp ) 116 { 117 return ( Mathf.Abs( p.x - cp.x ) + Mathf.Abs( p.y -cp.y ) )*10; 118 } 119 private void CheckPoint( Point p0, Ball[,] map, Point pa, ref Point pb ) 120 { 121 int x = 0, y = 0; 122 for ( int i = 0; i < 4; i++ ) 123 { 124 switch(i) 125 { 126 case 0: 127 x = p0.position.x + 1; 128 y = p0.position.y; 129 break; 130 case 1: 131 x = p0.position.x - 1; 132 y = p0.position.y; 133 break; 134 case 2: 135 x = p0.position.x; 136 y = p0.position.y - 1; 137 break; 138 case 3: 139 x = p0.position.x; 140 y = p0.position.y + 1; 141 break; 142 } 143 if ( x >= 0 && x < 9 && y >= 0 && y < 9 ) 144 { 145 if ( !map[x,y].isExist && !IsInCloseList( new MyVector( x , y ) ) ) 146 { 147 if ( IsInOpenList( new MyVector( x , y ) ) ) 148 { 149 Point pt = GetPointFromOpenList( new MyVector( x, y ) ); 150 int G_new = 0; 151 152 G_new = GetG( pt ); 153 if ( G_new < pt.g ) 154 { 155 Open_List.Remove( pt ); 156 pt.father = p0; 157 pt.g = G_new; 158 Open_List.Add ( pt ); 159 } 160 } 161 else 162 { 163 Point pt = new Point (); 164 pt.position.x = x; 165 pt.position.y = y; 166 pt.father = p0; 167 pt.g = GetG( pt ); 168 pt.h = GetH( pt.position, pb.position ); 169 Open_List.Add( pt ); 170 } 171 } 172 } 173 } 174 } 175 176 public Point FindWay( Ball[,] balls, Point pa, Point pb ) 177 { 178 Point head = new Point (); 179 Open_List.Add(pa); 180 181 while ( !( IsInOpenList( pb.position ) || Open_List.Count == 0 ) ) 182 { 183 Point p0 = GetMinF(); 184 if ( p0 == null ) 185 { 186 break; 187 } 188 Open_List.Remove( p0 ); 189 Close_List.Add( p0 ); 190 CheckPoint( p0, balls, pa, ref pb ); 191 } 192 Point p = GetPointFromOpenList( pb.position ); 193 194 Point tempP = new Point (); 195 if ( p != null ) 196 { 197 tempP.position.x = p.position.x; 198 tempP.position.y = p.position.y; 199 tempP.father = null; 200 201 head = tempP; 202 p = p.father; 203 } 204 else 205 { 206 return null; 207 } 208 209 210 while ( p.father != null ) 211 { 212 Point temp = new Point (); 213 214 temp.position.x = p.position.x; 215 temp.position.y = p.position.y; 216 temp.father = head; 217 218 head = temp; 219 p = p.father; 220 } 221 222 return head; 223 } 224 }

浙公网安备 33010602011771号