87 lines
3.0 KiB
C#
87 lines
3.0 KiB
C#
![]() |
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using DDD.ScriptableObjects;
|
||
|
using Sirenix.OdinInspector;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace DDD.Managers
|
||
|
{
|
||
|
public class ItemManager : Singleton<ItemManager>
|
||
|
{
|
||
|
[field: Title("ScriptableObject")]
|
||
|
[field: SerializeField, Required]
|
||
|
public ItemDataSo ItemDataSo { get; private set; }
|
||
|
|
||
|
[field: SerializeField, Required]
|
||
|
public CraftRecipeDataSo CraftRecipeDataSo { get; private set; }
|
||
|
|
||
|
[Title("테스트용 데이터")]
|
||
|
[SerializeField]
|
||
|
private List<string> _testCraftRecipes = new();
|
||
|
|
||
|
[field: Title("실시간 데이터")]
|
||
|
[field: ShowInInspector]
|
||
|
public Dictionary<string, CraftRecipeData> AcquiredCraftRecipeDatas { get; private set; }
|
||
|
|
||
|
protected override void OnAwake()
|
||
|
{
|
||
|
base.OnAwake();
|
||
|
|
||
|
AcquiredCraftRecipeDatas = new Dictionary<string, CraftRecipeData>(CraftRecipeDataSo.GetDataCount());
|
||
|
|
||
|
// 테스트용 데이터
|
||
|
if (_testCraftRecipes != null)
|
||
|
{
|
||
|
foreach (string craftRecipeIdx in _testCraftRecipes)
|
||
|
{
|
||
|
AcquireRecipe(craftRecipeIdx, saveAfter: false);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
LoadCraftRecipes();
|
||
|
SaveCraftRecipes();
|
||
|
}
|
||
|
|
||
|
public void AcquireRecipe(string craftRecipeIdx, bool saveAfter = true)
|
||
|
{
|
||
|
CraftRecipeData acquiredCraftRecipeData = CraftRecipeDataSo.GetDataByIdx(craftRecipeIdx);
|
||
|
if (acquiredCraftRecipeData != null && AcquiredCraftRecipeDatas.TryAdd(craftRecipeIdx, acquiredCraftRecipeData))
|
||
|
{
|
||
|
// 새로 획득한 경우에만 저장 처리
|
||
|
if(saveAfter) SaveCraftRecipes();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void SaveCraftRecipes()
|
||
|
{
|
||
|
List<string> craftRecipes = new List<string>(AcquiredCraftRecipeDatas.Keys);
|
||
|
List<string> loadCraftRecipes = ES3.Load<List<string>>(SaveData.CraftRecipes, defaultValue: null);
|
||
|
|
||
|
if (loadCraftRecipes == null || craftRecipes.Count != loadCraftRecipes.Count || !craftRecipes.SequenceEqual(loadCraftRecipes))
|
||
|
{
|
||
|
ES3.Save(SaveData.CraftRecipes, craftRecipes);
|
||
|
Debug.Log("제작레시피 저장 완료");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void LoadCraftRecipes()
|
||
|
{
|
||
|
if (ES3.KeyExists(SaveData.CraftRecipes))
|
||
|
{
|
||
|
List<string> craftRecipes = ES3.Load<List<string>>(SaveData.CraftRecipes);
|
||
|
foreach (string recipeIdx in craftRecipes)
|
||
|
{
|
||
|
if (string.IsNullOrEmpty(recipeIdx)) continue;
|
||
|
|
||
|
AcquireRecipe(recipeIdx, saveAfter: false);
|
||
|
}
|
||
|
|
||
|
Debug.Log("제작레시피 불러오기 완료");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.Log("저장된 제작레시피 데이터가 없습니다.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|