using System.Collections.Generic; using UnityEngine; public class UnitCtrl : MonoBehaviour { public UnitInfo unit; public Animator anim; public GameObject defensObj; bool isHomeAttack; List enemyUnits; public bool isEnemy; float delay; private void Awake() { enemyUnits = new List(); isHomeAttack = false; delay = 0; } private void Start() { if(isEnemy) defensObj.tag = "enemy"; else defensObj.tag = "player"; } private void Update() { delay -= Time.deltaTime; if (delay > 0) return; if (enemyUnits.Count != 0 || isHomeAttack) { if (enemyUnits.Count == 0) { anim.SetTrigger("att"); Debug.Log("Ä·ÇÁ °ø°Ý"); delay = unit.attackSpeed; } else { if (enemyUnits[0] != null) { anim.SetTrigger("att"); enemyUnits[0].Attack(unit.attack); delay = unit.attackSpeed; } else { enemyUnits.RemoveAt(0); } } } else { 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) { unit.hp -= (dmg - unit.defense); Debug.Log($"{dmg - unit.defense}µ¥¹ÌÁö! {unit.hp}³²À½."); if(unit.hp <= 0) { //Àӽ÷Π»èÁ¦ÇÏ°Ô Ã³¸® ÃßÈÄ ÀçȰ¿ë ÇÒ¼ö ÀÖ°Ô Ã³¸®ÇÒ°Í Destroy(gameObject); } } }