zombiegame2/Assets/LevelSystem.cs

83 lines
2.2 KiB
C#
Raw Permalink Normal View History

2026-02-06 08:57:52 +00:00
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class LevelUpSystem : MonoBehaviour
{
[Header("UI 연결")]
public Image hpBar;
public Image expBar;
public TextMeshProUGUI levelText;
[Header("이펙트 설정")]
public GameObject levelUpVFX; // 여기에 만든 프리팹을 넣으세요
[Header("플레이어 정보")]
public int level = 1;
public float currentHp = 100f;
public float maxHp = 100f;
public float currentExp = 0f;
public float maxExp = 100f;
private Animator levelAnimator;
void Start()
{
// Level_Text 오브젝트에 붙어있는 애니메이터를 가져옵니다.
if (levelText != null)
{
levelAnimator = levelText.GetComponent<Animator>();
}
UpdateUI();
}
void Update()
{
// 테스트용: 스페이스바를 누르면 경험치 25씩 획득
if (Input.GetKeyDown(KeyCode.Space))
{
GainExp(25f);
}
}
public void GainExp(float amount)
{
currentExp += amount;
while (currentExp >= maxExp)
{
LevelUp();
}
UpdateUI();
}
void LevelUp()
{
currentExp -= maxExp;
level++;
maxExp += 50f; // 레벨업 시 필요 경험치 증가
currentHp = maxHp; // 레벨업 시 체력 회복
// 1. UI 숫자 팝업 애니메이션 실행
if (levelAnimator != null)
{
levelAnimator.SetTrigger("LevelUp");
}
// 2. 레벨업 VFX 프리팹 소환
if (levelUpVFX != null)
{
// 화면 중앙(Vector3.zero)에 생성. 나중에 플레이어 위치로 바꿀 수 있습니다.
GameObject effect = Instantiate(levelUpVFX, Vector3.zero, Quaternion.identity);
Destroy(effect, 2f); // 2초 뒤 자동 삭제
}
Debug.Log("레벨업! 현재 레벨: " + level);
}
void UpdateUI()
{
if (hpBar != null) hpBar.fillAmount = currentHp / maxHp;
if (expBar != null) expBar.fillAmount = currentExp / maxExp;
if (levelText != null) levelText.text = level.ToString();
}
}