[프로토타입] 건물생성 시 주변가까운 캐릭터 데려오기
Project/TODO2019. 6. 12. 02:10
TestNearCharacter.unitypackage
추가할 것 : 이동방향 바라보기, 이미 건물로 갔던 캐릭터의 경로 재설정
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class TestCreatedBuilding : MonoBehaviour { public int colCount, rowCount; public Vector2[] chracterPos; public Button buildingBtn; public Button chracterMoveBtn; private int tileWidth = 64; private int tileHeight = 32; private int col = 0; //행 private int row = 0; //열 private Coroutine routine; private GameObject mapTile; private GameObject motherNode; private GameObject targetNode; private GameObject selectedTile; private GameObject selectedBuilding; private GameObject nearCharacter; private Dictionary<Vector2, GameObject> dicFIndTile = new Dictionary<Vector2, GameObject>(); private List<GameObject> openList = new List<GameObject>(); private List<GameObject> closeList = new List<GameObject>(); private List<GameObject> characterList = new List<GameObject>(); private List<GameObject> buildingList = new List<GameObject>(); private void Start() { this.CreateCharacter(); this.CreateMap(); this.buildingBtn.onClick.AddListener(() => { Createbuilding(); }); this.chracterMoveBtn.onClick.AddListener(() => { this.FIndNextPos(); }); } private void FIndNextPos() { if (this.routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.FindNextPosImpl()); } private IEnumerator FindNextPosImpl() { while (true) { var dir = (this.motherNode.transform.position - this.nearCharacter.transform.position).normalized; var nextNode = Vector2.Distance(this.nearCharacter.transform.position, this.motherNode.transform.position); var distance = Vector2.Distance(this.nearCharacter.transform.position, this.targetNode.transform.position); this.nearCharacter.transform.position += dir * 2f * Time.deltaTime; if (nextNode <= 0.05f) { if (distance <= 0.02f) { Debug.Log("도착"); this.ClearMap(); break; } this.FindNode(); Debug.Log("다음"); } yield return null; } } private void FindNode() { var nearCharacterPos = this.motherNode.GetComponent<TestTile>().vec2; var nearCharacterX = nearCharacterPos.x - 1; var nearCharacterY = nearCharacterPos.y - 1; var maxX = nearCharacterPos.x + 1; var maxY = nearCharacterPos.y + 1; if (nearCharacterX < 0) { nearCharacterX = 0; } if (nearCharacterY < 0) { nearCharacterY = 0; } for (int x = (int)nearCharacterX; x <= maxX; x++) { for (int y = (int)nearCharacterY; y <= maxY; y++) { if (x < this.colCount && y < this.rowCount) { var data = new Vector2(x, y); var tile = this.dicFIndTile[data]; if (!this.closeList.Contains(tile)) { if (data.x == nearCharacterX && data.y == nearCharacterPos.y || data.y == nearCharacterPos.y) { this.openList.Add(tile); } if (data.y == nearCharacterY && data.x == nearCharacterPos.x || data.x == nearCharacterPos.x) { this.openList.Add(tile); } this.openList.Remove(motherNode); this.SetFGH(); } } } } foreach (var datas in openList) { datas.GetComponent<SpriteRenderer>().color = Color.grey; } this.closeList.Add(motherNode); this.NextNode(); } private void SetFGH() { foreach (var tile in this.openList) { var tileScript = tile.GetComponent<TestTile>(); 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(); } } private void NextNode() { this.motherNode.GetComponent<SpriteRenderer>().color = Color.blue; if (this.targetNode != this.motherNode) { IEnumerable<GameObject> sortNode = this.openList.OrderBy(x => x.GetComponent<TestTile>().f); this.selectedTile = sortNode.FirstOrDefault<GameObject>(); this.motherNode = this.selectedTile; } } private void ClearMap() { Debug.Log("타일초기화"); for (int i = 0; i < colCount; i++) { for (int j = 0; j < rowCount; j++) { var tiles = new Vector2(j, i); var tile = dicFIndTile[tiles]; tile.GetComponent<SpriteRenderer>().color = Color.white; } } this.openList.Clear(); this.closeList.Clear(); this.targetNode = null; this.selectedTile = null; } #region 마우스 클릭 시 빌딩생성 public void Createbuilding() { if (routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.CreatebuildingImpl()); } private IEnumerator CreatebuildingImpl() { var building = Instantiate(Resources.Load<GameObject>("Prefabs/isoTile")); while (true) { Vector2 ray = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero); building.transform.position = ray; if (Input.GetMouseButtonDown(0)) { if (hit.collider != null) { this.selectedBuilding = hit.collider.gameObject; this.targetNode = hit.collider.gameObject; hit.collider.gameObject.GetComponent<TestTile>().isBlock = true; this.FindNearCharacterNode(); building.transform.position = hit.collider.gameObject.transform.position; break; } } yield return null; } } #endregion #region 주변캐릭터 탐색 private void FindNearCharacterNode() { foreach (var characters in this.characterList) { var character = characters.GetComponent<TestCharacter>().vec2; var characterX = character.x - 1; var characterY = character.y - 1; var maxX = character.x + 1; var maxY = character.y + 1; if (characterX < 0) { characterX = 0; } if (characterY < 0) { characterY = 0; } for (int x = (int)characterX; x <= maxX; x++) { for (int y = (int)characterY; y <= maxY; y++) { if (x < this.colCount && y < this.rowCount) { var data = new Vector2(x, y); var nearNode = this.dicFIndTile[data]; var getG = characters.GetComponent<TestCharacter>().g; var getH = characters.GetComponent<TestCharacter>().h; //다음 타일까지의 이동비용 getG = Mathf.Round(Vector2.Distance(characters.GetComponent<TestCharacter>().vec2, nearNode.GetComponent<TestTile>().vec2) * 10); //최종목적지점까지의 예상이동비용 var dx = Mathf.Abs(characters.GetComponent<TestCharacter>().vec2.x - this.selectedBuilding.GetComponent<TestTile>().vec2.x); var dy = Mathf.Abs(characters.GetComponent<TestCharacter>().vec2.y - this.selectedBuilding.GetComponent<TestTile>().vec2.y); getH = (dx + dy) * 10; characters.GetComponent<TestCharacter>().f = getG + getH; } } } } //F값이 제일 적은 캐릭터 var sortCharacterF = this.characterList.OrderBy(x => x.GetComponent<TestCharacter>().f); this.nearCharacter = sortCharacterF.FirstOrDefault<GameObject>(); //캐릭터의 키값으로 타일 찾고 마더로 this.motherNode = this.dicFIndTile[nearCharacter.GetComponent<TestCharacter>().vec2]; } #endregion #region FGH 활성화 비활성화 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); } } #endregion #region 맵, 타일, 캐릭터 생성 //캐릭터 생성입력 private void CreateCharacter() { var character = Instantiate(Resources.Load<GameObject>("Prefabs/hero")); var character1 = Instantiate(Resources.Load<GameObject>("Prefabs/hero")); var character2 = Instantiate(Resources.Load<GameObject>("Prefabs/hero")); var character3 = Instantiate(Resources.Load<GameObject>("Prefabs/hero")); this.CreatedCharacter(character, this.chracterPos[0].x, this.chracterPos[0].y); this.CreatedCharacter(character1, this.chracterPos[1].x, this.chracterPos[1].y); this.CreatedCharacter(character2, this.chracterPos[2].x, this.chracterPos[2].y); this.CreatedCharacter(character3, this.chracterPos[3].x, this.chracterPos[3].y); } //맵생성입력 private void CreateMap() { while (true) { this.mapTile = Instantiate(Resources.Load<GameObject>("Prefabs/tile")); this.mapTile.tag = "Tile"; this.mapTile.transform.SetParent(this.transform); this.CreatedMap(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 CreatedCharacter(GameObject character, float x, float y) { var vec2 = new Vector2(x, y); character.AddComponent<TestCharacter>(); character.GetComponent<TestCharacter>().vec2 = vec2; this.characterList.Add(character); var screen = this.MapToScreen(vec2); var screenPos = Camera.main.ScreenToWorldPoint(new Vector2(screen.x + 512, screen.y + 384 + 100)); character.transform.position = new Vector3(screenPos.x, screenPos.y, 0); } //맵생성 private void CreatedMap(GameObject tileMap, float x, float y) { var vec2 = new Vector2(x, y); tileMap.AddComponent<TestTile>(); tileMap.GetComponent<TestTile>().vec2 = vec2; if (tileMap.GetComponentInChildren<TextMesh>()) { tileMap.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", x, y); this.SetActiveFGH(tileMap); } foreach (var chracter in this.characterList) { if (chracter.GetComponent<TestCharacter>().vec2 == tileMap.GetComponent<TestTile>().vec2) { this.motherNode = tileMap; } } 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 |
'Project > TODO' 카테고리의 다른 글
[프로토타입]0626타일클릭 후 몬스터그룹 이동시키기,건물파괴 (0) | 2019.06.26 |
---|---|
프로토타입 건물생성 하면 주변 캐릭터가 찾아오기 (0) | 2019.06.14 |
[프로토타입]캐릭터 이동 및 빌딩생성 (0) | 2019.06.09 |
[프로토타입3단계] 캐릭터로 이동시키기 (0) | 2019.06.08 |
[프로토타입]딕셔너리 키 오류 해결 (0) | 2019.06.07 |