Projext/Assets/Scripts/Player/Stats/PlayerLevelSystem.cs

70 lines
1.9 KiB
C#
Raw Normal View History

2026-01-29 06:58:38 +00:00
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("레벨 설정")]
2026-01-29 06:58:38 +00:00
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;
2026-01-29 06:58:38 +00:00
public static System.Action OnLevelUp;
private int RequiredExp
{
get
{
int index = level - 1;
if (index >= expTable.Length) return expTable[expTable.Length - 1];
2026-01-29 06:58:38 +00:00
return expTable[index];
}
}
private void OnEnable() { MonsterClass.OnMonsterKilled += GainExp; UpdateExpUI(); }
private void OnDisable() { MonsterClass.OnMonsterKilled -= GainExp; }
2026-01-29 06:58:38 +00:00
void GainExp(int amount)
{
currentExp += amount;
while (currentExp >= RequiredExp) { currentExp -= RequiredExp; LevelUp(); }
2026-01-29 06:58:38 +00:00
UpdateExpUI();
}
void LevelUp()
{
if (level >= expTable.Length + 1) { currentExp = 0; return; }
level++;
// ✨ 힘 대신 공격력(+10) 증가
if (stats != null) stats.AddBaseLevelUpStats(1000f, 10f);
2026-01-29 06:58:38 +00:00
if (pHealth != null) pHealth.RefreshHealthUI();
StartCoroutine(DelayedCardPopup());
}
2026-01-29 06:58:38 +00:00
private IEnumerator DelayedCardPopup()
{
yield return new WaitForSeconds(1.5f);
OnLevelUp?.Invoke();
2026-01-29 06:58:38 +00:00
}
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}";
2026-01-29 06:58:38 +00:00
}
}