[A스타알고리즘]a*algorithm
Algoritm2019. 5. 23. 15:11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tile : MonoBehaviour { public TextMesh G; public TextMesh H; public TextMesh F; public GameObject arrow; public float g; public float h; public float f; public Vector2 vec2; public bool closeNode; public bool isBlock; public Tile(Vector2 vec2) { this.vec2 = vec2; } } | cs |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class TestAstar : MonoBehaviour { public int col; public int row; public Vector2[] impossiblePos; public Vector2 startPos; public Vector2 endPos; public Button findNextNodeBtn; public Button displayOpenList; public Button closeListBtn; public Button clearColorBtn; public Button findNodeBtn; private int tileHeight = 100; private int tileWidth = 100; private Vector2 tilesPos; private GameObject selectedTile; private GameObject tile; private GameObject targetNode; private GameObject motherNode; private List<GameObject> openList = new List<GameObject>(); private List<GameObject> closeList = new List<GameObject>(); private Dictionary<Vector2, GameObject> dicFindTile = new Dictionary<Vector2, GameObject>(); private void Awake() { } void Start() { this.CreateMap(); this.ButtonManager(); } #region 버튼 private void ButtonManager() { this.findNextNodeBtn.onClick.AddListener(() => { this.SortingOpenList(); this.NextNode(); }); this.displayOpenList.onClick.AddListener(() => { foreach(var tile in this.openList) { tile.GetComponentInChildren<SpriteRenderer>().color = Color.yellow; this.SetActiveArrowAndFGH(tile); } }); this.closeListBtn.onClick.AddListener(() => { this.CloseListColoring(); }); this.findNodeBtn.onClick.AddListener(() => { this.FindNode(); }); this.clearColorBtn.onClick.AddListener(() => { this.ClearColor(); }); } #endregion private void SortingOpenList() { IEnumerable<GameObject> sortNode = this.openList.OrderBy(x => x.GetComponent<Tile>().f); //foreach (var node in sortNode) //{ // Debug.Log(node.GetComponent<Tile>().f); //} this.selectedTile = sortNode.FirstOrDefault<GameObject>(); } private void NextNode() { this.motherNode = this.selectedTile; this.selectedTile.GetComponentInChildren<SpriteRenderer>().color = Color.magenta; } private void SetFGH() { foreach (var tile in this.openList) { var tileScript = tile.GetComponent<Tile>(); this.targetNode.GetComponent<Tile>(); var getG = tile.GetComponent<Tile>().g; var getH = tile.GetComponent<Tile>().h; getG = Mathf.Round(Vector2.Distance(tile.GetComponent<Tile>().vec2, this.motherNode.GetComponent<Tile>().vec2) * 10); var dx = Mathf.Abs(tile.GetComponent<Tile>().vec2.x - this.endPos.x); var dy = Mathf.Abs(tile.GetComponent<Tile>().vec2.y - this.endPos.y); getH = (dx + dy) * 10; tile.GetComponent<Tile>().f = getG + getH; tileScript.G.text = getG.ToString(); tileScript.H.text = getH.ToString(); tileScript.F.text = tile.GetComponent<Tile>().f.ToString(); } } private void TurnArrow() { } private void CloseListColoring() { for (int i = 0; i < col; i++) { for (int j = 0; j < row; j++) { var test = new Vector2(j, i); var tiles = dicFindTile[test]; if (this.closeList.Contains(tiles)) { tiles.GetComponent<SpriteRenderer>().color = Color.cyan; } } } } #region 타일초기화 private void ClearColor() { for (int i = 0; i < col; i++) { for (int j = 0; j < row; j++) { var tiles = new Vector2(j, i); var tile = dicFindTile[tiles]; tile.GetComponent<SpriteRenderer>().color = Color.black; tile.GetComponent<Tile>().arrow.SetActive(false); var tilePos = tile.GetComponent<Tile>().vec2; if (tilePos == startPos) { tile.GetComponent<SpriteRenderer>().color = Color.green; } if (tilePos == endPos) { tile.GetComponent<SpriteRenderer>().color = Color.red; } for (int a = 0; a < this.impossiblePos.Length; a++) { if (tilePos == this.impossiblePos[a]) { tile.GetComponent<SpriteRenderer>().color = Color.blue; } } } } } #endregion #region 타일생성 private void CreateMap() { for (int i = 0; i < col; i++) { for (int j = 0; j < row; j++) { this.tilesPos = new Vector2(j, i); this.tile = Instantiate<GameObject>(Resources.Load("tile01") as GameObject); var tileLine = Instantiate<GameObject>(Resources.Load("tileLine2") as GameObject); tile.transform.SetParent(GameObject.Find("tiles").transform); tileLine.transform.SetParent(tile.transform); var screenPos = new Vector2(j * this.tileWidth + 512 - 300, i * -this.tileHeight + this.tileHeight * 6); var worldPos = Camera.main.ScreenToWorldPoint(screenPos); worldPos.z = 0; tile.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", j, i); tile.transform.position = worldPos; var tileJ = (tile.GetComponent<Tile>().vec2.x = j); var tileI = (tile.GetComponent<Tile>().vec2.y = i); this.TileColoring(j, i, tile); this.dicFindTile.Add(new Vector2(j, i), tile); } } } private void TileColoring(int j, int i, GameObject tile) { if (this.tilesPos == this.startPos) { tile.name = "start"; tile.GetComponent<SpriteRenderer>().color = Color.green; this.motherNode = tile; } if (this.tilesPos == this.endPos) { tile.name = "end"; tile.GetComponent<SpriteRenderer>().color = Color.red; this.targetNode = tile; } for (int a = 0; a < this.impossiblePos.Length; a++) { if (this.tilesPos == this.impossiblePos[a]) { tile.name = "block"; tile.GetComponent<SpriteRenderer>().color = Color.blue; tile.GetComponent<Tile>().isBlock = true; } } this.SetActiveArrowAndFGH(tile); } #endregion private void SetActiveArrowAndFGH(GameObject tile) { if (!this.openList.Contains(tile)) { tile.GetComponent<Tile>().G.gameObject.SetActive(false); tile.GetComponent<Tile>().F.gameObject.SetActive(false); tile.GetComponent<Tile>().H.gameObject.SetActive(false); tile.GetComponent<Tile>().arrow.SetActive(false); } else { tile.GetComponent<Tile>().G.gameObject.SetActive(true); tile.GetComponent<Tile>().F.gameObject.SetActive(true); tile.GetComponent<Tile>().H.gameObject.SetActive(true); tile.GetComponent<Tile>().arrow.SetActive(true); } } private void FindNode() { var mother = this.motherNode.GetComponent<Tile>(); var motherX = mother.vec2.x - 1; var motherY = mother.vec2.y - 1; var maxX = mother.vec2.x + 1; var maxY = mother.vec2.y + 1; if (motherX < 0) { motherX = 0; } if (motherY < 0) { motherY = 0; } for (int x = (int)motherX; x <= maxX; x++) { for (int y = (int)motherY; y <= maxY; y++) { var data = new Vector2(x, y); var tile = this.dicFindTile[data]; if (tile.GetComponent<Tile>().isBlock != true && !this.closeList.Contains(tile)) { this.openList.Add(tile); this.openList.Remove(motherNode); this.SetActiveArrowAndFGH(tile); this.SetFGH(); this.closeList.Add(motherNode); if (tile != this.motherNode) { tile.GetComponent<SpriteRenderer>().color = Color.yellow; this.TurnArrow(tile); } } } } } private void TurnArrow(GameObject tile) { var script = tile.GetComponent<Tile>(); var xPos = this.motherNode.transform.position.x - script.arrow.transform.position.x; var yPos = this.motherNode.transform.position.y - script.arrow.transform.position.y; var rad = Mathf.Atan2(yPos, xPos) * Mathf.Rad2Deg; script.arrow.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rad + 90)); } } | cs |