2026-01-29 06:58:38 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
public class MonsterClass : MonoBehaviour, IDamageable
|
|
|
|
|
|
{
|
|
|
|
|
|
[Header("스탯")]
|
|
|
|
|
|
[SerializeField] protected float maxHP = 100f;
|
|
|
|
|
|
protected float currentHP;
|
|
|
|
|
|
public event Action<float, float> OnHealthChanged;
|
|
|
|
|
|
|
|
|
|
|
|
[Header("피격 / 사망 애니메이션")]
|
|
|
|
|
|
[SerializeField] protected string Monster_GetDamage = "Monster_GetDamage";
|
|
|
|
|
|
[SerializeField] protected string Monster_Die = "Monster_Die";
|
|
|
|
|
|
|
|
|
|
|
|
protected Animator animator;
|
|
|
|
|
|
protected NavMeshAgent agent;
|
|
|
|
|
|
protected AudioSource audioSource;
|
|
|
|
|
|
protected bool isHit;
|
|
|
|
|
|
protected bool isDead;
|
|
|
|
|
|
|
|
|
|
|
|
[Header("경험치")]
|
|
|
|
|
|
[SerializeField] protected int expReward = 10;
|
|
|
|
|
|
public static System.Action<int> OnMonsterKilled;
|
|
|
|
|
|
|
|
|
|
|
|
[Header("공통 사운드/이펙트")]
|
|
|
|
|
|
[SerializeField] protected AudioClip hitSound;
|
|
|
|
|
|
[SerializeField] protected AudioClip deathSound;
|
|
|
|
|
|
[SerializeField] protected GameObject deathEffectPrefab;
|
|
|
|
|
|
[SerializeField] protected ParticleSystem hitEffect;
|
|
|
|
|
|
[SerializeField] protected Transform impactSpawnPoint;
|
|
|
|
|
|
|
2026-01-29 16:11:10 +00:00
|
|
|
|
[Header("--- 드랍 설정 ---")]
|
|
|
|
|
|
[SerializeField] private GameObject potionPrefab;
|
|
|
|
|
|
[SerializeField][Range(0f, 100f)] private float potionDropChance = 30f;
|
|
|
|
|
|
|
|
|
|
|
|
[Space(10)]
|
|
|
|
|
|
[SerializeField] private GameObject[] weaponPrefabs;
|
|
|
|
|
|
[SerializeField][Range(0f, 100f)] private float weaponDropChance = 5f;
|
|
|
|
|
|
|
2026-01-29 06:58:38 +00:00
|
|
|
|
protected virtual void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
currentHP = maxHP;
|
|
|
|
|
|
animator = GetComponent<Animator>();
|
|
|
|
|
|
agent = GetComponent<NavMeshAgent>();
|
|
|
|
|
|
audioSource = GetComponent<AudioSource>();
|
|
|
|
|
|
OnHealthChanged?.Invoke(currentHP, maxHP);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void TakeDamage(float amount) { OnDamaged(amount); }
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnDamaged(float damage)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isDead) return;
|
|
|
|
|
|
currentHP -= damage;
|
|
|
|
|
|
OnHealthChanged?.Invoke(currentHP, maxHP);
|
|
|
|
|
|
|
|
|
|
|
|
if (currentHP <= 0) { Die(); return; }
|
|
|
|
|
|
if (!isHit) StartHit();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void StartHit()
|
|
|
|
|
|
{
|
|
|
|
|
|
isHit = true;
|
|
|
|
|
|
if (agent && agent.isOnNavMesh) { agent.isStopped = true; agent.velocity = Vector3.zero; }
|
|
|
|
|
|
animator.Play(Monster_GetDamage, 0, 0f);
|
|
|
|
|
|
if (hitEffect) hitEffect.Play();
|
|
|
|
|
|
if (hitSound) audioSource.PlayOneShot(hitSound);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnHitEnd() { isHit = false; if (agent && agent.isOnNavMesh) agent.isStopped = false; }
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Die()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isDead) return;
|
|
|
|
|
|
isDead = true;
|
|
|
|
|
|
|
|
|
|
|
|
OnMonsterKilled?.Invoke(expReward);
|
|
|
|
|
|
|
2026-01-29 16:11:10 +00:00
|
|
|
|
// 몬스터 사망 시 아이템 드랍
|
|
|
|
|
|
TryDropItems();
|
|
|
|
|
|
|
2026-01-29 06:58:38 +00:00
|
|
|
|
if (agent && agent.isOnNavMesh) { agent.isStopped = true; agent.velocity = Vector3.zero; }
|
|
|
|
|
|
animator.Play(Monster_Die, 0, 0f);
|
|
|
|
|
|
if (deathSound) audioSource.PlayOneShot(deathSound);
|
|
|
|
|
|
|
|
|
|
|
|
Destroy(gameObject, 1.5f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-29 16:11:10 +00:00
|
|
|
|
private void TryDropItems()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. 포션 드랍 체크
|
|
|
|
|
|
if (potionPrefab != null && UnityEngine.Random.Range(0f, 100f) <= potionDropChance)
|
|
|
|
|
|
{
|
|
|
|
|
|
SpawnItem(potionPrefab);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 무기 드랍 체크
|
|
|
|
|
|
if (weaponPrefabs != null && weaponPrefabs.Length > 0 && UnityEngine.Random.Range(0f, 100f) <= weaponDropChance)
|
|
|
|
|
|
{
|
|
|
|
|
|
int randomIndex = UnityEngine.Random.Range(0, weaponPrefabs.Length);
|
|
|
|
|
|
SpawnItem(weaponPrefabs[randomIndex]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ⭐ [최종 수정] 아이템 생성 함수
|
|
|
|
|
|
private void SpawnItem(GameObject prefab)
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 spawnPos = transform.position + Vector3.up * 0.5f;
|
|
|
|
|
|
GameObject item = Instantiate(prefab, spawnPos, Quaternion.identity);
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ [유저 요청] 인스펙터 변수 없이 '프리팹의 원래 크기'를 그대로 적용합니다.
|
|
|
|
|
|
item.transform.localScale = prefab.transform.localScale;
|
|
|
|
|
|
|
|
|
|
|
|
// 아이템이 몬스터 몸 안에서 나오는 느낌을 위해 살짝 위로만 튀게 합니다.
|
|
|
|
|
|
if (item.TryGetComponent<Rigidbody>(out var rb))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 위로 톡 튀어오르게 함 (마찰력 등은 유저님이 리지드바디에서 설정한 대로 돌아감)
|
|
|
|
|
|
Vector3 popDir = Vector3.up * 3f + UnityEngine.Random.insideUnitSphere * 0.5f;
|
|
|
|
|
|
rb.AddForce(popDir, ForceMode.Impulse);
|
|
|
|
|
|
|
|
|
|
|
|
// 무기일 경우만 약간의 회전을 줘서 역동적으로 떨어지게 합니다.
|
|
|
|
|
|
if (prefab != potionPrefab)
|
|
|
|
|
|
{
|
|
|
|
|
|
rb.AddTorque(UnityEngine.Random.insideUnitSphere * 5f, ForceMode.Impulse);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-29 06:58:38 +00:00
|
|
|
|
public void OnDeathAnimEnd() { }
|
|
|
|
|
|
}
|