2024-06-03 18:26:03 +00:00
|
|
|
using System.Collections.Generic;
|
2024-06-21 22:11:53 +00:00
|
|
|
using System.IO;
|
|
|
|
using Newtonsoft.Json.Linq;
|
2024-06-03 18:26:03 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
2024-06-25 06:19:16 +00:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
using UnityEditor;
|
|
|
|
#endif
|
|
|
|
|
2024-07-06 12:13:58 +00:00
|
|
|
namespace BlueWater.Items
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-09-12 07:36:24 +00:00
|
|
|
[CreateAssetMenu(fileName = "ItemDropTable", menuName = "ScriptableObjects/ItemDropTable")]
|
|
|
|
public class ItemDropTableSo : DataSo<ItemDropTable> // DataSo<ItemDropTable>를 상속
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
private const string ItemDropJsonPath = "Assets/Resources/Json/ItemDropTable.json";
|
|
|
|
private const string CharacterDataJsonPath = "Assets/Resources/Json/CharacterData.json";
|
|
|
|
private const string FilePath = "Assets/02.Scripts/ScriptableObject/Item/ItemDropTable.asset";
|
2024-06-03 18:26:03 +00:00
|
|
|
|
2024-06-25 06:19:16 +00:00
|
|
|
#if UNITY_EDITOR
|
2024-06-21 22:11:53 +00:00
|
|
|
[MenuItem("Tools/ItemDropTable ScriptableObject 생성")]
|
|
|
|
private static void CreateItemDropTable()
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
var itemDropJsonContent = File.ReadAllText(ItemDropJsonPath);
|
|
|
|
var characterDataJsonContent = File.ReadAllText(CharacterDataJsonPath);
|
|
|
|
|
|
|
|
var instance = CreateFromJson(itemDropJsonContent, characterDataJsonContent);
|
|
|
|
AssetDatabase.CreateAsset(instance, FilePath);
|
|
|
|
AssetDatabase.SaveAssets();
|
2024-09-12 07:36:24 +00:00
|
|
|
Debug.Log("ItemDropTable ScriptableObject created successfully.");
|
2024-06-21 22:11:53 +00:00
|
|
|
}
|
2024-06-25 06:19:16 +00:00
|
|
|
#endif
|
2024-06-21 22:11:53 +00:00
|
|
|
|
|
|
|
private static ItemDropTableSo CreateFromJson(string itemDropJsonString, string characterDataJsonString)
|
|
|
|
{
|
2024-09-12 07:36:24 +00:00
|
|
|
var itemDropTableSo = CreateInstance<ItemDropTableSo>();
|
|
|
|
|
|
|
|
var newItemDropTables = ParseJsonToCharacterDrops(itemDropJsonString, characterDataJsonString);
|
|
|
|
|
|
|
|
// Dictionary<string, ItemDropTable>로 변환하여 직렬화 가능한 리스트에 추가
|
|
|
|
var serializedDataList = new List<SerializableKeyValuePair<ItemDropTable>>();
|
|
|
|
foreach (var kvp in newItemDropTables)
|
|
|
|
{
|
|
|
|
serializedDataList.Add(new SerializableKeyValuePair<ItemDropTable>
|
|
|
|
{
|
|
|
|
Key = kvp.Key,
|
|
|
|
Value = kvp.Value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
itemDropTableSo.SetSerializedDataList(serializedDataList);
|
|
|
|
|
|
|
|
return itemDropTableSo;
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 07:36:24 +00:00
|
|
|
private static Dictionary<string, ItemDropTable> ParseJsonToCharacterDrops(string itemDropJsonString, string characterDataJsonString)
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-08-22 10:39:15 +00:00
|
|
|
var itemDropTables = new Dictionary<string, ItemDropTable>();
|
2024-06-21 22:11:53 +00:00
|
|
|
var characterDataDictionary = ParseCharacterDataJson(characterDataJsonString);
|
|
|
|
|
|
|
|
var jsonArray = JArray.Parse(itemDropJsonString);
|
|
|
|
foreach (var element in jsonArray)
|
|
|
|
{
|
2024-08-22 10:39:15 +00:00
|
|
|
var characterIdx = (string)element["CharacterIdx"];
|
2024-06-21 22:11:53 +00:00
|
|
|
var dropItem = new DropItem
|
|
|
|
{
|
2024-08-22 10:39:15 +00:00
|
|
|
ItemIdx = (string)element["ItemIdx"],
|
2024-06-21 22:11:53 +00:00
|
|
|
DropRate = (int)element["DropRate"],
|
|
|
|
QuantityMin = (int)element["QuantityMin"],
|
|
|
|
QuantityMax = (int)element["QuantityMax"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!itemDropTables.ContainsKey(characterIdx))
|
|
|
|
{
|
|
|
|
var characterName = characterDataDictionary.GetValueOrDefault(characterIdx, "Unknown");
|
|
|
|
itemDropTables[characterIdx] = new ItemDropTable(new CharacterData(characterIdx, characterName));
|
|
|
|
}
|
|
|
|
|
|
|
|
itemDropTables[characterIdx].DropItems.Add(dropItem);
|
|
|
|
}
|
|
|
|
|
2024-09-12 07:36:24 +00:00
|
|
|
return itemDropTables;
|
2024-06-21 22:11:53 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 10:39:15 +00:00
|
|
|
private static Dictionary<string, string> ParseCharacterDataJson(string characterDataJsonString)
|
2024-06-21 22:11:53 +00:00
|
|
|
{
|
2024-08-22 10:39:15 +00:00
|
|
|
var characterDataDictionary = new Dictionary<string, string>();
|
2024-06-21 22:11:53 +00:00
|
|
|
var jsonArray = JArray.Parse(characterDataJsonString);
|
|
|
|
|
|
|
|
foreach (var element in jsonArray)
|
|
|
|
{
|
2024-08-22 10:39:15 +00:00
|
|
|
var characterIdx = (string)element["CharacterIdx"];
|
2024-06-21 22:11:53 +00:00
|
|
|
var name = (string)element["Name"];
|
|
|
|
characterDataDictionary[characterIdx] = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return characterDataDictionary;
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|