124 lines
3.2 KiB
C#
124 lines
3.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class UnitCtrl : MonoBehaviour
|
|
{
|
|
public UnitInfo unit;
|
|
public Animator anim;
|
|
public GameObject defensObj;
|
|
bool isHomeAttack;
|
|
|
|
List<UnitCtrl> enemyUnits;
|
|
|
|
public bool isEnemy;
|
|
|
|
float delay;
|
|
|
|
private void Awake()
|
|
{
|
|
enemyUnits = new List<UnitCtrl>();
|
|
isHomeAttack = false;
|
|
delay = 0;
|
|
}
|
|
private void Start()
|
|
{
|
|
if(isEnemy)
|
|
defensObj.tag = "enemy";
|
|
else
|
|
defensObj.tag = "player";
|
|
}
|
|
private void Update()
|
|
{
|
|
if (PlayCtrl.Instance.isEndGame)
|
|
return;
|
|
delay -= Time.deltaTime;
|
|
if (delay > 0)
|
|
return;
|
|
if (enemyUnits.Count != 0 || isHomeAttack)
|
|
{
|
|
if (enemyUnits.Count == 0)
|
|
{
|
|
anim.SetTrigger("att");
|
|
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("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<UnitCtrl>());
|
|
}
|
|
if (collision.gameObject.CompareTag("player_home"))
|
|
{
|
|
isHomeAttack = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (collision.gameObject.CompareTag("enemy"))
|
|
{
|
|
enemyUnits.Add(collision.gameObject.GetComponentInParent<UnitCtrl>());
|
|
}
|
|
if (collision.gameObject.CompareTag("enemy_home"))
|
|
{
|
|
isHomeAttack = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// °ø°ÝÀ» ¹Þ¾ÒÀ»¶§ È£ÃâµÇ°Ô ¸¸µé±â
|
|
/// </summary>
|
|
/// <param name="dmg">°ø°Ý ´ë¹ÌÁö</param>
|
|
public void Attack(int dmg)
|
|
{
|
|
unit.hp -= (dmg - unit.defense);
|
|
Debug.Log($"{dmg - unit.defense}µ¥¹ÌÁö! {unit.hp}³²À½.");
|
|
if(unit.hp <= 0)
|
|
{
|
|
//Àӽ÷Π»èÁ¦ÇÏ°Ô Ã³¸® ÃßÈÄ ÀçȰ¿ë ÇÒ¼ö ÀÖ°Ô Ã³¸®ÇÒ°Í
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
}
|