[프로토타입 2단계] isometric Astar알고리즘 활용하여 길찾기
Project/TODO2019. 6. 6. 22:56
Astar알고리즘 활용하여 길찾기
보완할점 : 대각선으로만 이동 가능하게끔 설정, 다시 마우스 클릭 시 이전기록 초기화, 딕셔너리 키 ㅇ
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class TestIsometricAstar : MonoBehaviour { public int colCount, rowCount; public Vector2 startPos; private GameObject mapTile; private Coroutine routine; private int tileWidth = 64; private int tileHeight = 32; private int col = 0; //행 private int row = 0; //열 private GameObject my; private GameObject motherNode; private GameObject targetNode; private GameObject selectedTile; private Dictionary<Vector2, GameObject> dicFIndTile = new Dictionary<Vector2, GameObject>(); private List<GameObject> openList = new List<GameObject>(); private List<GameObject> closeList = new List<GameObject>(); void Start() { this.CreateMap(); this.CreateTile(); this.TileCheck(); } void TileCheck() { if (this.routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.TileCheckImpl()); } IEnumerator TileCheckImpl() { while (true) { if (Input.GetMouseButtonDown(0)) { Vector2 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(worldPos, Vector2.zero); if (hit.collider != null) { this.targetNode = hit.collider.gameObject; this.FIndNextPos(); } } yield return null; } } private void FIndNextPos() { if (this.routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.FindNextPosImpl()); } private IEnumerator FindNextPosImpl() { while(true) { if(this.targetNode == this.motherNode) { Debug.Log("도착"); this.TileCheck(); break; } this.FindNode(); this.SetFGH(); this.SortingOpenList(); yield return null; } } private void SortingOpenList() { IEnumerable<GameObject> sortNode = this.openList.OrderBy(x => x.GetComponent<TestTile>().f); this.selectedTile = sortNode.FirstOrDefault<GameObject>(); this.NextNode(); } private void SetActiveFGH(GameObject tile) { if (!this.openList.Contains(tile)) { tile.GetComponent<TestTile>().G.gameObject.SetActive(false); tile.GetComponent<TestTile>().F.gameObject.SetActive(false); tile.GetComponent<TestTile>().H.gameObject.SetActive(false); } else { tile.GetComponent<TestTile>().G.gameObject.SetActive(true); tile.GetComponent<TestTile>().F.gameObject.SetActive(true); tile.GetComponent<TestTile>().H.gameObject.SetActive(true); } } private void NextNode() { this.motherNode = this.selectedTile; this.selectedTile.GetComponentInChildren<SpriteRenderer>().color = Color.magenta; } private void FindNode() { var mother = this.motherNode.GetComponent<TestTile>(); 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<TestTile>().isBlock != true && !this.closeList.Contains(tile)) { this.openList.Add(tile); this.openList.Remove(motherNode); this.closeList.Add(motherNode); if (tile != this.motherNode) { tile.GetComponent<SpriteRenderer>().color = Color.yellow; } } } } } private void SetFGH() { foreach (var tile in this.openList) { var tileScript = tile.GetComponent<TestTile>(); this.SetActiveFGH(tile); var getG = tile.GetComponent<TestTile>().g; var getH = tile.GetComponent<TestTile>().h; getG = Mathf.Round(Vector2.Distance(tile.GetComponent<TestTile>().vec2, this.motherNode.GetComponent<TestTile>().vec2) * 10); var dx = Mathf.Abs(tile.GetComponent<TestTile>().vec2.x - this.targetNode.GetComponent<TestTile>().vec2.x); var dy = Mathf.Abs(tile.GetComponent<TestTile>().vec2.y - this.targetNode.GetComponent<TestTile>().vec2.y); getH = (dx + dy) * 10; tile.GetComponent<TestTile>().f = getG + getH; tileScript.G.text = getG.ToString(); tileScript.H.text = getH.ToString(); tileScript.F.text = tile.GetComponent<TestTile>().f.ToString(); } } #region 맵, 타일 생성 private void CreateTile() { if (this.my == null) { this.my = Instantiate(Resources.Load<GameObject>("myIsoTile")); this.CreatedTileMap(my, this.startPos.x, this.startPos.y); this.motherNode = my; } } private void CreateMap() { while (true) { this.mapTile = Instantiate(Resources.Load<GameObject>("mapTile")); this.mapTile.tag = "Tile"; this.mapTile.transform.SetParent(this.transform); this.CreatedTileMap(this.mapTile, this.col, row); this.dicFIndTile.Add(new Vector2(col, row), mapTile); this.col++; if (this.col >= this.colCount) { this.col = 0; this.row++; } if (this.row >= this.rowCount) { row = 0; break; } } } private void CreatedTileMap(GameObject tileMap, float x, float y) { var vec2 = new Vector2(x, y); if (tileMap.GetComponentInChildren<TextMesh>()) { tileMap.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", x, y); this.SetActiveFGH(tileMap); } tileMap.AddComponent<TestTile>(); tileMap.GetComponent<TestTile>().vec2 = vec2; var screen = this.MapToScreen(vec2); var screenPos = Camera.main.ScreenToWorldPoint(new Vector2(screen.x + 512, screen.y + 384 + 100)); tileMap.transform.position = new Vector3(screenPos.x, screenPos.y, 0); } public Vector2 MapToScreen(Vector2 mapPos) { var screenX = mapPos.x * this.tileWidth - (mapPos.y * this.tileWidth); var screenY = mapPos.y * -tileHeight - (mapPos.x * tileHeight); return new Vector2(screenX, screenY); } #endregion } | 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestTile : MonoBehaviour { public TextMesh G; public TextMesh H; public TextMesh F; public float g; public float h; public float f = 1f; public Vector2 vec2; public bool closeNode; public bool isBlock; public TestTile(Vector2 vec2) { this.vec2 = vec2; } } | cs |
'Project > TODO' 카테고리의 다른 글
[프로토타입]캐릭터 이동 및 빌딩생성 (0) | 2019.06.09 |
---|---|
[프로토타입3단계] 캐릭터로 이동시키기 (0) | 2019.06.08 |
[프로토타입]딕셔너리 키 오류 해결 (0) | 2019.06.07 |
[프로토타입 1단계] isometric 타일 좌표 찍어서 위치 이동시키기 (0) | 2019.06.05 |
[190605]오늘 할 일 (0) | 2019.06.05 |