Projext/Assets/Scripts/Player/Stats/PlayerLevelSystem.cs
2026-02-02 17:30:23 +09:00

95 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class PlayerLevelSystem : MonoBehaviour
{
[Header("--- 참조 ---")]
[SerializeField] private Stats stats;
[SerializeField] private PlayerHealth pHealth;
[Header("레벨 설정")]
public int level = 1;
public int currentExp = 0;
[SerializeField] private int[] expTable;
[Header("UI")]
[SerializeField] private Image expFillImage;
[SerializeField] private TextMeshProUGUI expText;
[SerializeField] private TextMeshProUGUI levelText;
public static System.Action OnLevelUp;
private int RequiredExp
{
get
{
int index = level - 1;
if (index >= expTable.Length) return expTable[expTable.Length - 1];
return expTable[index];
}
}
private void OnEnable()
{
MonsterClass.OnMonsterKilled += GainExp;
UpdateExpUI();
}
private void OnDisable()
{
MonsterClass.OnMonsterKilled -= GainExp;
}
void GainExp(int amount)
{
currentExp += amount;
while (currentExp >= RequiredExp)
{
currentExp -= RequiredExp;
LevelUp();
}
UpdateExpUI();
}
void LevelUp()
{
if (level >= expTable.Length + 1) { currentExp = 0; return; }
level++;
Debug.Log($"🎉 [LevelSystem] 레벨 업! 현재 레벨: {level}");
// 1. 기초 스탯 먼저 상승 (체력 +1000, 힘 +100 - 유저님 설정값)
if (stats != null) stats.AddBaseLevelUpStats(1000f, 100f);
// 2. ⭐ [핵심] 늘어난 스탯으로 피 채우고 UI 즉시 갱신 함수 호출
if (pHealth != null)
{
pHealth.RefreshHealthUI();
}
else
{
Debug.LogError("[LevelSystem] pHealth 참조가 비어있습니다! 인스펙터에 PlayerHealth를 드래그해서 넣어주세요.");
}
// 3. 1.5초 대기 후 카드 선택 UI 팝업
StartCoroutine(DelayedCardPopup());
}
private IEnumerator DelayedCardPopup()
{
yield return new WaitForSeconds(1.5f);
Debug.Log("1.5초 경과: 카드 UI 팝업 실행");
OnLevelUp?.Invoke();
}
void UpdateExpUI()
{
float fill = (float)currentExp / RequiredExp;
if (expFillImage != null) expFillImage.fillAmount = fill;
if (expText != null) expText.text = $"{currentExp} / {RequiredExp}";
if (levelText != null) levelText.text = $"Lv. {level}";
}
}