Projext/Assets/7.Other Code/PlayerLevelSystem.cs
2026-01-29 15:58:38 +09:00

86 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class PlayerLevelSystem : MonoBehaviour
{
[Header("레벨")]
public int level = 1;
public int currentExp = 0;
[Header("레벨업 경험치 테이블")]
[Tooltip("index 0 = Lv1 → Lv2 필요 경험치")]
[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)
{
Debug.Log($"몬스터 처치! 얻은 경험치: {amount}");
currentExp += amount;
while (currentExp >= RequiredExp)
{
Debug.Log("레벨업 조건 달성! Show 호출 준비");
currentExp -= RequiredExp;
LevelUp();
}
UpdateExpUI();
}
void LevelUp()
{
if (level >= expTable.Length + 1)
{
currentExp = 0;
return;
}
level++;
Debug.Log($"🎉 레벨 업! 현재 레벨: {level}");
OnLevelUp?.Invoke(); // ⭐ 레벨업 알림
}
void UpdateExpUI()
{
float fill = (float)currentExp / RequiredExp;
expFillImage.fillAmount = fill;
expText.text = $"{currentExp} / {RequiredExp}";
levelText.text = $"Lv. {level}"; // ⭐ 레벨 표시
}
}