thewar_client/Client/Assets/1_Script/Info/UnitCtrl.cs

165 lines
4.5 KiB
C#

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;
public long tableIndex;
List<UnitCtrl> enemyUnits;
public bool isEnemy;
float delay;
bool _isDie = false;
public bool isDie { get { return _isDie; } }
public void DataSet(long tableIndex)
{
//이곳은 List유닛에게 세팅됨
this.tableIndex = tableIndex;
unit.UnitSet(Statics.excelDatas.unitData[tableIndex]);
}
public void UnitBonusSet(bool isEnemy, int unitLevel)
{
//이곳은 생성된 유닛에게 세팅됨
this.isEnemy = isEnemy;
unit.UnitSet(Statics.excelDatas.unitData[tableIndex]);
unit.UnitBonusSet(new UnitBonus(unitLevel, new int[0], 0, 0));
}
private void Awake()
{
enemyUnits = new List<UnitCtrl>();
isHomeAttack = false;
delay = 0;
_isDie = false;
}
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 || _isDie)
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 && !enemyUnits[0].isDie)
{
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<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)
{
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;
_isDie = true;
Destroy(gameObject, 1.0f);
}
}
}
// 리지드, 콜라이더 위치 변경 Unit > oder
// oder 콜라이더 트리거 체크
// 디펜스 오브젝트 좀더 상위로 위치변경