[프로토타입] 몬스터그룹이 본진 공격하기
카테고리 없음2019. 6. 20. 00:25
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestCharacter : MonoBehaviour { public float g; public float h; public float f; public float speed; public Vector2 vec2; public List<GameObject> openList; public List<GameObject> closeList; private void Start() { this.speed = Random.Range(1.0f, 2.0f); } public TestCharacter(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 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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class TestMonster : MonoBehaviour { public int colCount, rowCount; public int fenceX, fenceY; public Vector2[] blockPos; public Vector2[] fencesPos; public Vector2[] characterPos; //private Vector2[] monsterSpanPos; private List<Vector2> monsterSpanPos = new List<Vector2>(); public Button moveMonsterBtn; public Button CreateMonsterLeftSpanBtn; public Button CreateMonsterRightSpanBtn; public Button CreateMonsterBottomSpanBtn; public Button testBtn; public GameObject testBuildingBtn; public Text txtTimer; private float timer = 5f; private int col = 0, row = 0; private int tileWidth = 64; private int tileHeight = 32; private GameObject monster; private GameObject workman; private GameObject village; private GameObject fence; private GameObject mapTile; private Coroutine routine; private Vector2 buildingPos; private Dictionary<Vector2, GameObject> dicTiles = new Dictionary<Vector2, GameObject>(); public List<GameObject> characterList = new List<GameObject>(); public List<GameObject> monsterList = new List<GameObject>(); private GameObject motherNode; private GameObject villageNode; private GameObject selectedTile; private int testCnt1 = 0; private int testCnt2 = 0; private void Start() { this.CreateCharacter(); this.CreateMap(); this.ButtonManager(); StartCoroutine(TouchMonsterSpanPos()); } private IEnumerator TouchMonsterSpanPos() { while(true) { var ray = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero); if (Input.GetMouseButtonDown(0)) { if (hit.collider != null) { this.monsterSpanPos.Add(hit.collider.GetComponent<TestTile>().vec2); } } yield return null; } } #region 버튼매니저 private void ButtonManager() { this.moveMonsterBtn.onClick.AddListener(() => { this.TestFindNodes(); }); this.testBtn.onClick.AddListener(() => { //StartCoroutine(this.MonsterAttack()); }); #region 캐릭터생성버튼 this.CreateMonsterLeftSpanBtn.onClick.AddListener(() => { this.CreateLeftMonster(); }); this.CreateMonsterRightSpanBtn.onClick.AddListener(() => { this.CreateRightMonster(); }); this.CreateMonsterBottomSpanBtn.onClick.AddListener(() => { this.CreateBottomMonster(); }); #endregion } #endregion private void TestFindNodes() { foreach (var monster in this.monsterList) { var tile = this.dicTiles[monster.GetComponent<TestCharacter>().vec2]; this.motherNode = tile; monster.GetComponent<TestCharacter>().openList = new List<GameObject>(); monster.GetComponent<TestCharacter>().closeList = new List<GameObject>(); if (this.FindeTargetNode(monster) == true) { StartCoroutine(this.FindNextPosImpl(monster)); } } } private bool FindeTargetNode(GameObject monster) { if (villageNode != null) { while (true) { if (villageNode != motherNode) { this.FindNode(monster); } else { Debug.Log("길찾기 완료"); return true; } } } else { Debug.Log("어디로 가"); return false; } } //길찾기 #region 길찾기 private void FindNode(GameObject monster) { var monsterScript = monster.GetComponent<TestCharacter>(); var mother = motherNode.GetComponent<TestTile>().vec2; var motherX = mother.x - 1; var motherY = mother.y - 1; var maxX = mother.x + 1; var maxY = mother.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++) { if (x < this.colCount && y < this.rowCount) { var data = new Vector2(x, y); var tile = this.dicTiles[data]; if (tile.GetComponent<TestTile>().isBlock != true && !monsterScript.closeList.Contains(tile)) { if (data.x == motherX && data.y == mother.y || data.y == mother.y) { monsterScript.openList.Add(tile); this.SetFGH(tile, motherNode); } if (data.y == motherY && data.x == mother.x || data.x == mother.x) { monsterScript.openList.Add(tile); this.SetFGH(tile, motherNode); } monsterScript.openList.Remove(motherNode); monsterScript.closeList.Add(motherNode); } } } } if (motherNode.GetComponent<TestTile>().isFence) { motherNode.GetComponent<TestTile>().capacity--; if (motherNode.GetComponent<TestTile>().capacity <= 0) { motherNode.GetComponent<TestTile>().isBlock = true; } //TODO } IEnumerable<GameObject> sortNode = monsterScript.openList.OrderBy(x => x.GetComponent<TestTile>().f); this.selectedTile = sortNode.FirstOrDefault<GameObject>(); this.motherNode = this.selectedTile; monsterScript.closeList.Add(motherNode); } #endregion #region 이동하기 private IEnumerator FindNextPosImpl(GameObject testMonster) { var monster = testMonster.GetComponent<TestCharacter>(); for (int i = 0; i < monster.closeList.Count; i++) { testMonster.GetComponent<Animator>().Play("run"); while (true) { var dir = (monster.closeList[i].transform.position - testMonster.transform.position).normalized; var nextNode = Vector2.Distance(testMonster.transform.position, monster.closeList[i].transform.position); testMonster.transform.position += dir * monster.speed * Time.deltaTime; if (nextNode <= 0.03f) { break; } yield return null; } if (i + 1 < monster.closeList.Count) { if (monster.closeList[i + 1].GetComponent<TestTile>().isFence == true || monster.closeList[i + 1].GetComponent<TestTile>().isBuilding == true) { monster.closeList[i + 1].GetComponent<TestTile>().capacity--; testMonster.GetComponent<Animator>().Play("attack"); //TODO break; } } var distance = Vector2.Distance(testMonster.transform.position, this.villageNode.transform.position); if (distance <= 0.04f) { testMonster.GetComponent<TestCharacter>().vec2 = this.villageNode.GetComponent<TestTile>().vec2; testMonster.GetComponent<Animator>().Play("idle"); Debug.Log("도착"); break; } } yield return new WaitForSeconds(0.6f); testMonster.GetComponent<Animator>().Play("idle"); //TODO } #endregion #region 타일초기화 private void ClearMap() { for (int i = 0; i < colCount; i++) { for (int j = 0; j < rowCount; j++) { var tiles = new Vector2(j, i); var tile = dicTiles[tiles]; if (!tile.GetComponent<TestTile>().isBlock || tile.GetComponent<TestTile>().isFence) { tile.GetComponent<SpriteRenderer>().color = Color.white; } } } } #endregion #region FGH 입력하기 private void SetFGH(GameObject tile, GameObject character) { 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, character.GetComponent<TestTile>().vec2) * 10); var dx = Mathf.Abs(tile.GetComponent<TestTile>().vec2.x - this.village.GetComponent<TestTile>().vec2.x); var dy = Mathf.Abs(tile.GetComponent<TestTile>().vec2.y - this.village.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(); } #endregion #region 몬스터 생성 private void CreateLeftMonster() { for (int i = 0; i < 5; i++) { this.monster = Instantiate(Resources.Load<GameObject>("Prefabs/monster")); var data = new Vector2(this.monsterSpanPos[2].x, this.monsterSpanPos[2].y); var leftMonsterPos = this.dicTiles[data]; this.monster.AddComponent<TestCharacter>(); this.monster.GetComponent<TestCharacter>().vec2 = leftMonsterPos.GetComponent<TestTile>().vec2; monster.transform.position = leftMonsterPos.transform.position; this.monsterList.Add(monster); } } private void CreateRightMonster() { for (int i = 0; i < 5; i++) { this.monster = Instantiate(Resources.Load<GameObject>("Prefabs/monster")); var data = new Vector2(this.monsterSpanPos[1].x, this.monsterSpanPos[1].y); var rightMonsterPos = this.dicTiles[data]; this.monster.AddComponent<TestCharacter>(); this.monster.GetComponent<TestCharacter>().vec2 = rightMonsterPos.GetComponent<TestTile>().vec2; monster.transform.position = rightMonsterPos.transform.position; this.monsterList.Add(monster); } } private void CreateBottomMonster() { for (int i = 0; i < 5; i++) { this.monster = Instantiate(Resources.Load<GameObject>("Prefabs/monster")); var data = new Vector2(this.monsterSpanPos[0].x, this.monsterSpanPos[0].y); var BottomMonsterPos = this.dicTiles[data]; this.monster.AddComponent<TestCharacter>(); this.monster.GetComponent<TestCharacter>().vec2 = BottomMonsterPos.GetComponent<TestTile>().vec2; monster.transform.position = BottomMonsterPos.transform.position; this.monsterList.Add(monster); } } #endregion #region 맵, 타일, 캐릭터 생성 //캐릭터 생성입력 private void CreateCharacter() { foreach (var characterPos in this.characterPos) { this.workman = Instantiate(Resources.Load<GameObject>("Prefabs/hero")); this.CreatedCharacter(this.workman, characterPos.x, characterPos.y); } } //캐릭터생성 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, screen.y)); character.transform.position = new Vector3(screenPos.x, screenPos.y, 0); } //맵생성입력 private void CreateMap() { this.village = Instantiate(Resources.Load<GameObject>("Prefabs/128x128building")); this.buildingPos = new Vector2(this.colCount / 2, this.rowCount / 2); this.CreatedMap(village, this.buildingPos.x, this.buildingPos.y); for (int i = 0; i < this.fencesPos.Length; i++) { this.fence = Instantiate(Resources.Load<GameObject>("Prefabs/fence")); this.CreatedMap(fence, this.fencesPos[i].x, this.fencesPos[i].y); } 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, this.row); this.dicTiles.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; this.WorldToCenterCamera(); break; } } } //맵생성 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.GetComponent<TestTile>().vec2 == this.buildingPos) { tileMap.GetComponent<TestTile>().isBuilding = true; this.villageNode = tileMap; } for (int a = 0; a < this.blockPos.Length; a++) { if (vec2 == this.blockPos[a]) { tileMap.name = "block"; tileMap.GetComponent<SpriteRenderer>().color = Color.red; tileMap.GetComponent<TestTile>().isBlock = true; } } for (int b = 0; b < this.fencesPos.Length; b++) { if (vec2 == this.fencesPos[b]) { tileMap.name = "fence"; tileMap.GetComponent<TestTile>().isFence = true; tileMap.GetComponent<TestTile>().capacity = 3; } } var screen = this.MapToScreen(vec2); var screenPos = Camera.main.ScreenToWorldPoint(new Vector2(screen.x, screen.y)); 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); } //카메라를 타일의 센터로 private void WorldToCenterCamera() { var pos = new Vector2(this.colCount / 2 - 1, this.rowCount / 2 - 1); var posX = this.dicTiles[pos].transform.position.x; var posY = this.dicTiles[pos].transform.position.y; Camera.main.transform.localPosition = new Vector3(posX, posY, -100); Camera.main.orthographicSize = this.rowCount / 2; } #endregion } | cs |