116 lines
3.6 KiB
C#
116 lines
3.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
public class Enemy : MonoBehaviour
|
|
{
|
|
[Header("적 스탯 설정")]
|
|
public string enemyName = "고블린 왕";
|
|
public int hp = 30;
|
|
public int maxHp = 30;
|
|
public int attackPower = 2; // 플레이어를 공격할 때의 기본 데미지 수치
|
|
|
|
[Header("UI 연결")]
|
|
public TextMeshProUGUI nameText; // 적 이름 표시 TMP
|
|
public Image hpImage; // 적 체력바 이미지
|
|
|
|
private Animator animator;
|
|
|
|
private void Start()
|
|
{
|
|
maxHp = hp;
|
|
|
|
// 자식 오브젝트의 애니메이터(SPUM 리소스 구조)를 획득합니다.
|
|
animator = GetComponentInChildren<Animator>();
|
|
|
|
// 적 캐릭터의 기본 방향을 플레이어를 바라보도록 좌측(스케일 양수)으로 정렬합니다.
|
|
Vector3 initialScale = transform.localScale;
|
|
initialScale.x = Mathf.Abs(initialScale.x);
|
|
transform.localScale = initialScale;
|
|
|
|
// 시작 시 적 이름을 UI 텍스트에 연동합니다.
|
|
if (nameText != null)
|
|
{
|
|
nameText.text = enemyName;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// 안전 조치: maxHp가 비정상(0 이하)이거나 현재 체력이 초과된 경우 자동 동기화
|
|
if (maxHp <= 0 || hp > maxHp)
|
|
{
|
|
maxHp = hp;
|
|
}
|
|
|
|
// 플레이어와 동일하게 Simple/Filled 이미지 방식 모두 가로가 정상 감축되도록 이중 연동합니다.
|
|
if (hpImage != null && maxHp > 0)
|
|
{
|
|
float fillRatio = Mathf.Clamp01((float)hp / maxHp);
|
|
hpImage.fillAmount = fillRatio;
|
|
hpImage.rectTransform.localScale = new Vector3(fillRatio, 1f, 1f);
|
|
}
|
|
}
|
|
|
|
// 플레이어의 공격을 받아 데미지를 입는 메서드
|
|
public void TakeDamage(int damage)
|
|
{
|
|
if (hp <= 0) return; // 이미 죽은 경우 무시
|
|
|
|
hp -= damage;
|
|
if (hp < 0) hp = 0;
|
|
|
|
Debug.Log($"[Enemy] 적 {enemyName}이(가) {damage} 피해를 입었습니다. 현재 체력: {hp}/{maxHp}");
|
|
|
|
if (animator != null)
|
|
{
|
|
if (hp <= 0)
|
|
{
|
|
// 1. 적 전사 사망 효과음 재생
|
|
if (SoundManager.Instance != null)
|
|
{
|
|
SoundManager.Instance.PlayEnemyDeath();
|
|
}
|
|
|
|
// SPUM 리소스 기준 사망(4_Death) 트리거 작동
|
|
animator.SetTrigger("4_Death");
|
|
Debug.Log($"[Enemy] 적 {enemyName}이(가) 사망하였습니다.");
|
|
}
|
|
else
|
|
{
|
|
// 2. 적 피격 효과음 재생
|
|
if (SoundManager.Instance != null)
|
|
{
|
|
SoundManager.Instance.PlayPunchHit();
|
|
}
|
|
|
|
// SPUM 리소스 기준 피격(3_Damaged) 트리거 작동
|
|
animator.SetTrigger("3_Damaged");
|
|
}
|
|
}
|
|
}
|
|
|
|
// 플레이어를 반격 공격하는 메서드
|
|
public void Attack(Player player)
|
|
{
|
|
if (hp <= 0 || player == null) return;
|
|
|
|
Debug.Log($"[Enemy] 적 {enemyName}이(가) 플레이어를 공격합니다! (데미지: {attackPower})");
|
|
|
|
// 3. 적 공격 휘두르기 효과음 재생
|
|
if (SoundManager.Instance != null)
|
|
{
|
|
SoundManager.Instance.PlayAttackSwing();
|
|
}
|
|
|
|
if (animator != null)
|
|
{
|
|
// SPUM 리소스 기준 공격(2_Attack) 트리거 작동
|
|
animator.SetTrigger("2_Attack");
|
|
}
|
|
|
|
// 플레이어 체력 차감 전송
|
|
player.TakeDamage(attackPower, enemyName);
|
|
}
|
|
}
|