using System; using System.Collections.Generic; using UnityEngine; public class UnitCtrl : MonoBehaviour { public UnitInfo unit; public Animator anim; public GameObject defensObj; public Sprite unitSprite; bool isHomeAttack; public BoxCollider2D[] components; List enemyUnits; public bool isEnemy; float delay; public void DataSet(UnitData ud) { unit.name = ud.name; unit.hp = ud.hp; unit.attack = ud.attack; unit.attackSpeed = ud.attack_speed; unit.defense = ud.defense; unit.moveSpeed = ud.move_speed; unit.buy = ud.buy.ToArray(); } private void Awake() { enemyUnits = new List(); isHomeAttack = false; delay = 0; } private void Start() { if (isEnemy) { defensObj.tag = "enemy"; transform.localScale = Vector3.one; } else { transform.localScale = new Vector3(-1, 1, 1); defensObj.tag = "player"; } } private void Update() { if (PlayCtrl.Instance.isEndGame) return; delay -= Time.deltaTime; if (delay > 0) return; if (enemyUnits.Count != 0 || isHomeAttack) { anim.SetBool("isMove", false); if (enemyUnits.Count == 0) { anim.SetTrigger("attack"); if (isEnemy) { PlayCtrl.Instance.player.campHp -= unit.attack; if(PlayCtrl.Instance.player.campHp <= 0) { Debug.Log("ÆÐ¹è!"); PlayCtrl.Instance.isEndGame = true; } } else { PlayCtrl.Instance.enemy.campHp -= unit.attack; if (PlayCtrl.Instance.enemy.campHp <= 0) { Debug.Log("½Â¸®!"); PlayCtrl.Instance.isEndGame = true; } } delay = unit.attackSpeed; } else { if (enemyUnits[0] != null) { anim.SetTrigger("attack"); enemyUnits[0].Attack(unit.attack); delay = unit.attackSpeed; } else { enemyUnits.RemoveAt(0); } } } else { anim.SetBool("isMove", true); transform.position += Vector3.left * unit.moveSpeed * Time.deltaTime * (isEnemy ? 1 : -1); } } private void OnTriggerEnter2D(Collider2D collision) { if (isEnemy) { if (collision.gameObject.CompareTag("player")) { enemyUnits.Add(collision.gameObject.GetComponentInParent()); } if (collision.gameObject.CompareTag("player_home")) { isHomeAttack = true; } } else { if (collision.gameObject.CompareTag("enemy")) { enemyUnits.Add(collision.gameObject.GetComponentInParent()); } if (collision.gameObject.CompareTag("enemy_home")) { isHomeAttack = true; } } } /// /// °ø°ÝÀ» ¹Þ¾ÒÀ»¶§ È£ÃâµÇ°Ô ¸¸µé±â /// /// °ø°Ý ´ë¹ÌÁö public void Attack(int dmg) { if(unit.defense > dmg) { dmg = unit.defense + 100; } unit.hp -= (dmg - unit.defense); Debug.Log($"{dmg - unit.defense}µ¥¹ÌÁö! {unit.hp}³²À½."); if(unit.hp <= 0) { //Àӽ÷Π»èÁ¦ÇÏ°Ô Ã³¸® ÃßÈÄ ÀçȰ¿ë ÇÒ¼ö ÀÖ°Ô Ã³¸®ÇÒ°Í anim.SetBool("isDie", true); components[0].enabled = false; components[1].enabled = false; Destroy(gameObject, 1.0f); } } } // ¸®Áöµå, ÄݶóÀÌ´õ À§Ä¡ º¯°æ Unit > oder // oder ÄݶóÀÌ´õ Æ®¸®°Å üũ // µðÆæ½º ¿ÀºêÁ§Æ® Á»´õ »óÀ§·Î À§Ä¡º¯°æ