Projext/Assets/Scripts/Enemy/BossAI/BossCounterPersistence.cs

55 lines
3.4 KiB
C#
Raw Normal View History

2026-02-13 09:11:54 +00:00
using UnityEngine; // 유니티 기능을 불러올거에요 -> UnityEngine을
using System.Collections.Generic; // 딕셔너리를 사용할거에요 -> System.Collections.Generic을
2026-02-10 15:29:22 +00:00
2026-02-13 09:11:54 +00:00
public class BossCounterPersistence : MonoBehaviour // 클래스를 선언할거에요 -> 데이터 저장을 담당하는 BossCounterPersistence를
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
public static BossCounterPersistence Instance { get; private set; } // 프로퍼티를 선언할거에요 -> 싱글톤 인스턴스를
2026-02-10 15:29:22 +00:00
2026-02-13 09:11:54 +00:00
// ⭐ [복구] 외부(DebugPanel 등)에서 참조하던 데이터 저장소
public Dictionary<CounterType, int> Data { get; private set; } = new Dictionary<CounterType, int>(); // 프로퍼티를 선언할거에요 -> 카운터 데이터를 담을 딕셔너리를
2026-02-10 15:29:22 +00:00
2026-02-13 09:11:54 +00:00
private void Awake() // 함수를 실행할거에요 -> 초기화 Awake를
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
if (Instance == null) Instance = this; // 조건이 맞으면 설정할거에요 -> 나를 유일한 인스턴스로
else Destroy(gameObject); // 아니면 파괴할거에요 -> 중복된 나를
2026-02-10 15:29:22 +00:00
}
2026-02-13 09:11:54 +00:00
public void SaveData(Dictionary<CounterType, int> counters) // 함수를 선언할거에요 -> 저장을 수행하는 SaveData를
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
Data = counters; // 갱신할거에요 -> 내부 데이터 프로퍼티를
foreach (var pair in counters) // 반복할거에요 -> 모든 카운터를
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
PlayerPrefs.SetInt($"BossCounter_{pair.Key}", pair.Value); // 저장할거에요 -> PlayerPrefs에 키와 값을
2026-02-10 15:29:22 +00:00
}
2026-02-13 09:11:54 +00:00
PlayerPrefs.Save(); // 확정할거에요 -> 저장을
Debug.Log("💾 [BossPersistence] 데이터 저장됨"); // 로그를 찍을거에요 -> 저장 완료 메시지를
2026-02-10 15:29:22 +00:00
}
2026-02-13 09:11:54 +00:00
public void LoadData(Dictionary<CounterType, int> counters) // 함수를 선언할거에요 -> 불러오기를 수행하는 LoadData를
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
foreach (CounterType type in System.Enum.GetValues(typeof(CounterType))) // 반복할거에요 -> 모든 타입을 돌면서
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
string key = $"BossCounter_{type}"; // 키를 만들거에요 -> 저장된 키 이름을
int val = PlayerPrefs.GetInt(key, 0); // 불러올거에요 -> 저장된 값을 (없으면 0)
2026-02-10 15:29:22 +00:00
2026-02-13 09:11:54 +00:00
if (counters.ContainsKey(type)) counters[type] = val; // 조건이 맞으면 갱신할거에요 -> 딕셔너리 값을
else counters.Add(type, val); // 아니면 추가할거에요 -> 딕셔너리에 값을
2026-02-10 15:29:22 +00:00
}
2026-02-13 09:11:54 +00:00
Data = counters; // 동기화할거에요 -> 내부 데이터 프로퍼티를
Debug.Log("📂 [BossPersistence] 데이터 로드됨"); // 로그를 찍을거에요 -> 로드 완료 메시지를
2026-02-10 15:29:22 +00:00
}
2026-02-13 09:11:54 +00:00
// ⭐ [복구] 외부에서 호출하는 데이터 초기화 함수
public void ResetAllData() // 함수를 선언할거에요 -> 모든 데이터를 삭제하는 ResetAllData를
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
PlayerPrefs.DeleteAll(); // 삭제할거에요 -> 저장된 모든 프리팹 데이터를
Data.Clear(); // 비울거에요 -> 메모리 상의 데이터를
Debug.Log("🗑️ [BossPersistence] 초기화 완료"); // 로그를 찍을거에요 -> 초기화 완료 메시지를
2026-02-10 15:29:22 +00:00
}
2026-02-13 09:11:54 +00:00
// ⭐ [복구] 해금 여부 확인 함수
public bool IsUnlocked(CounterType type) // 함수를 선언할거에요 -> 해금 여부를 반환하는 IsUnlocked를
2026-02-10 15:29:22 +00:00
{
2026-02-13 09:11:54 +00:00
return Data.ContainsKey(type) && Data[type] > 0; // 반환할거에요 -> 데이터가 있고 0보다 큰지 여부를
2026-02-10 15:29:22 +00:00
}
2026-02-12 15:23:25 +00:00
}