Projext/Assets/Scripts/Level_Scripts/LevelUpUIManager.cs
2026-02-25 19:39:20 +09:00

119 lines
6.7 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections; // 코루틴을 사용할거에요 -> System.Collections를
using System.Collections.Generic; // 리스트를 사용할거에요 -> System.Collections.Generic을
using UnityEngine; // 유니티 기능을 불러올거에요 -> UnityEngine을
using UnityEngine.UI; // 버튼 제어를 위해 불러올거에요 -> UnityEngine.UI를
public class LevelUpUIManager : MonoBehaviour // 클래스를 선언할거에요 -> 레벨업 UI를 관리하는 LevelUpUIManager를
{
[SerializeField] private GameObject panel; // 변수를 선언할거에요 -> 카드 선택 패널을
[SerializeField] private CardUI[] cardPrefabs; // 배열을 선언할거에요 -> 카드 UI 프리팹들을
[SerializeField] private Transform cardParent; // 변수를 선언할거에요 -> 카드가 생성될 부모 위치를
[SerializeField] private List<CardData> cardPool; // 리스트를 선언할거에요 -> 카드 데이터 풀을
[Header("--- 확정 버튼 설정 ---")] // 인스펙터 제목을 달거에요 -> 확정 버튼을
[SerializeField] private Button applyButton; // 변수를 선언할거에요 -> Apply 버튼을
private CardUI _selectedCardUI; // 변수를 선언할거에요 -> 현재 선택된 카드를
private void OnEnable() // 함수를 실행할거에요 -> 활성화 시
{
PlayerLevelSystem.OnLevelUp += Show; // 구독할거에요 -> 레벨업 이벤트를
if (applyButton != null) applyButton.onClick.AddListener(OnApplyButtonClick); // 연결할거에요 -> 버튼 클릭을
}
private void OnDisable() // 함수를 실행할거에요 -> 비활성화 시
{
PlayerLevelSystem.OnLevelUp -= Show; // 해제할거에요 -> 레벨업 이벤트를
if (applyButton != null) applyButton.onClick.RemoveListener(OnApplyButtonClick); // 해제할거에요 -> 버튼 클릭을
}
void Show() // 함수를 선언할거에요 -> 카드 선택 UI를 표시하는 Show를
{
panel.SetActive(true); // 켤거에요 -> 패널을
Time.timeScale = 0f; // 멈출거에요 -> 게임 시간을
_selectedCardUI = null; // 초기화할거에요 -> 선택 카드를
if (applyButton != null) applyButton.interactable = false; // 비활성화할거에요 -> Apply 버튼을
foreach (Transform child in cardParent) // 반복할거에요 -> 기존 카드들을
Destroy(child.gameObject); // 파괴할거에요 -> 이전 카드를
int slotCount = 3; // [FIX] 값을 설정할거에요 -> 카드 개수를 3장으로
/* =========================
* 1⃣ CardData 선택 (중복 없음)
* ========================= */
List<CardData> selectedCards = new List<CardData>(); // 리스트를 만들거에요 -> 선택된 카드 데이터를
List<CardData> tempDataPool = new List<CardData>(cardPool); // 리스트를 복사할거에요 -> 임시 풀을
for (int i = 0; i < slotCount && tempDataPool.Count > 0; i++) // 반복할거에요 -> 슬롯 수만큼
{
int idx = Random.Range(0, tempDataPool.Count); // 뽑을거에요 -> 랜덤 인덱스를
selectedCards.Add(tempDataPool[idx]); // 추가할거에요 -> 선택된 카드를
tempDataPool.RemoveAt(idx); // 제거할거에요 -> 중복 방지용으로
}
/* =========================
* 2⃣ 메인 버프 스탯 배분 — 3장이 서로 다른 스탯을 메인으로 가짐
*
* 카드A → Health 메인 카드B → Speed 메인 카드C → Damage 메인
* (순서는 매번 랜덤으로 섞임)
* ========================= */
List<StatType> mainStats = new List<StatType> // 리스트를 만들거에요 -> 전체 스탯 목록을
{
StatType.Health, // 추가할거에요 -> 체력을
StatType.Speed, // 추가할거에요 -> 속도를
StatType.Damage // 추가할거에요 -> 공격력을
};
ShuffleList(mainStats); // 섞을거에요 -> 배정 순서를 랜덤으로
/* =========================
* 3⃣ CardUI 프리팹 선택 + 메인 스탯 전달 (중복 없음)
* ========================= */
List<CardUI> tempPrefabs = new List<CardUI>(cardPrefabs); // 리스트를 복사할거에요 -> 프리팹 풀을
for (int i = 0; i < selectedCards.Count && tempPrefabs.Count > 0; i++) // 반복할거에요 -> 선택된 카드 수만큼
{
int idx = Random.Range(0, tempPrefabs.Count); // 뽑을거에요 -> 랜덤 인덱스를
CardUI ui = Instantiate(tempPrefabs[idx], cardParent); // 생성할거에요 -> 카드 UI를
// ⭐ i번째 카드에 i번째 메인 스탯 배정 (3장이 절대 겹치지 않음)
StatType mainStat = i < mainStats.Count ? mainStats[i] : mainStats[0]; // 결정할거에요 -> 이 카드의 메인 버프 스탯을
ui.Setup(selectedCards[i], this, mainStat); // 설정할거에요 -> 카드 데이터 + 메인 스탯을
tempPrefabs.RemoveAt(idx); // 제거할거에요 -> 중복 방지용으로
}
}
public void OnCardClick(CardUI clickedUI) // 함수를 선언할거에요 -> 카드 클릭 처리를
{
if (_selectedCardUI != null) _selectedCardUI.SetSelected(false); // 해제할거에요 -> 이전 선택을
_selectedCardUI = clickedUI; // 저장할거에요 -> 새 선택을
_selectedCardUI.SetSelected(true); // 설정할거에요 -> 선택 상태를
if (applyButton != null) applyButton.interactable = true; // 활성화할거에요 -> Apply 버튼을
}
private void OnApplyButtonClick() // 함수를 선언할거에요 -> Apply 버튼 클릭 처리를
{
if (_selectedCardUI == null) return; // 조건이 맞으면 중단할거에요 -> 선택 없으면
_selectedCardUI.ApplyCurrentEffect(); // 실행할거에요 -> 카드 효과를
Close(); // 실행할거에요 -> UI 닫기를
}
public void Close() // 함수를 선언할거에요 -> UI 닫기를
{
Time.timeScale = 1f; // 복원할거에요 -> 게임 시간을
panel.SetActive(false); // 끌거에요 -> 패널을
}
private void ShuffleList<T>(List<T> list) // 함수를 선언할거에요 -> 리스트를 랜덤으로 섞는 ShuffleList를
{
for (int i = list.Count - 1; i > 0; i--) // 반복할거에요 -> 뒤에서부터
{
int j = Random.Range(0, i + 1); // 뽑을거에요 -> 랜덤 인덱스를
T temp = list[i]; // 임시 저장할거에요 -> 현재 값을
list[i] = list[j]; // 교체할거에요 -> i와 j 위치를
list[j] = temp; // 교체할거에요 -> j에 임시값을
}
}
}