using UnityEngine; using UnityEngine.AI; using System.Collections; using System; public abstract class MonsterClass : MonoBehaviour, IDamageable { [Header("--- 최적화 설정 ---")] protected Renderer mobRenderer; protected Transform playerTransform; [SerializeField] protected float optimizationDistance = 40f; [Header("몬스터 기본 스탯")] // ⭐ 종류별로 다르게 설정할 핵심 수치들 [SerializeField] protected float maxHP = 100f; [SerializeField] protected float attackDamage = 10f; // ⭐ [추가] 몬스터 공격력 [SerializeField] protected int expReward = 10; // ⭐ 경험치 수치 [SerializeField] protected float moveSpeed = 3.5f; // ⭐ 이동 속도 추가 가능 protected float currentHP; public event Action OnHealthChanged; [Header("피격 / 사망 / 대기 애니메이션")] [SerializeField] protected string Monster_Idle = "Monster_Idle"; [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, isDead, isAttacking; [Header("AI 설정 (공격 후 휴식)")] [SerializeField] protected float attackRestDuration = 1.5f; protected bool isResting; public static System.Action OnMonsterKilled; [Header("공통 사운드/이펙트")] [SerializeField] protected AudioClip hitSound, deathSound; [SerializeField] protected GameObject deathEffectPrefab; [SerializeField] protected ParticleSystem hitEffect; [SerializeField] protected Transform impactSpawnPoint; protected virtual void Awake() { mobRenderer = GetComponentInChildren(); animator = GetComponent(); agent = GetComponent(); audioSource = GetComponent(); // ⭐ 시작할 때 에이전트 속도를 스탯에 맞게 설정 if (agent != null) agent.speed = moveSpeed; } protected virtual void OnEnable() { playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform; if (mobRenderer != null) mobRenderer.enabled = true; Init(); if (MobUpdateManager.Instance != null) MobUpdateManager.Instance.RegisterMob(this); } public void ResetStats() { isDead = false; currentHP = maxHP; OnHealthChanged?.Invoke(currentHP, maxHP); Collider col = GetComponent(); if (col != null) col.enabled = true; } protected virtual void OnDisable() { if (MobUpdateManager.Instance != null) MobUpdateManager.Instance.UnregisterMob(this); } protected virtual void Init() { } protected abstract void ExecuteAILogic(); public void OnManagedUpdate() { if (isDead || playerTransform == null || !gameObject.activeInHierarchy) return; float distance = Vector3.Distance(transform.position, playerTransform.position); if (distance > optimizationDistance) { StopMovement(); if (mobRenderer != null && mobRenderer.enabled) mobRenderer.enabled = false; return; } if (mobRenderer != null && !mobRenderer.enabled) mobRenderer.enabled = true; if (mobRenderer != null && !mobRenderer.isVisible) { StopMovement(); return; } if (agent != null && agent.isOnNavMesh && agent.isStopped) agent.isStopped = false; ExecuteAILogic(); } protected void StopMovement() { if (agent != null && agent.isOnNavMesh) { agent.isStopped = true; agent.velocity = Vector3.zero; } if (animator != null) { animator.SetFloat("Speed", 0f); animator.Play(Monster_Idle); } } 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; isAttacking = false; isResting = false; StopAllCoroutines(); 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 OnAttackStart() { isAttacking = true; isResting = false; } public virtual void OnAttackEnd() { isAttacking = false; if (!isDead && !isHit) StartCoroutine(RestAfterAttack()); } protected IEnumerator RestAfterAttack() { isResting = true; yield return new WaitForSeconds(attackRestDuration); isResting = false; } 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); // ⭐ 설정된 경험치 수치 전달 Collider col = GetComponent(); if (col != null) col.enabled = false; if (agent && agent.isOnNavMesh) { agent.isStopped = true; agent.velocity = Vector3.zero; } animator.Play(Monster_Die, 0, 0f); if (deathSound) audioSource.PlayOneShot(deathSound); Invoke("ReturnToPool", 1.5f); } public bool IsDead => isDead; protected void ReturnToPool() => gameObject.SetActive(false); }