86 lines
2.9 KiB
C#
86 lines
2.9 KiB
C#
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
namespace BlueWaterProject
|
|
{
|
|
public class ItemManager : Singleton<ItemManager>
|
|
{
|
|
[SerializeField, Required] private GameObject oceanItemDropPrefab;
|
|
[SerializeField, Required] private GameObject fieldItemDropPrefab;
|
|
|
|
[SerializeField] private List<Item> itemList;
|
|
public Dictionary<int, Item> ItemDictionary { get; private set; }
|
|
|
|
[SerializeField] private List<ItemDropTable> itemDropTableList;
|
|
public Dictionary<int, ItemDropTable> ItemDropTableDictionary { get; private set; }
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
|
|
Init();
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
ItemDictionary = new Dictionary<int, Item>(itemList.Count);
|
|
foreach (var element in itemList)
|
|
{
|
|
ItemDictionary.TryAdd(element.idx, element);
|
|
}
|
|
|
|
ItemDropTableDictionary = new Dictionary<int, ItemDropTable>(itemDropTableList.Count);
|
|
foreach (var element in itemDropTableList)
|
|
{
|
|
ItemDropTableDictionary.TryAdd(element.idx, element);
|
|
}
|
|
}
|
|
|
|
[Button("FromJson")]
|
|
private void LoadItemsFromJson()
|
|
{
|
|
var jsonFile = Resources.Load<TextAsset>("JSON/item_table");
|
|
var items = JsonHelper.FromJson<Item>(jsonFile.text);
|
|
|
|
foreach (var element in items)
|
|
{
|
|
itemList.Add(element);
|
|
}
|
|
|
|
jsonFile = Resources.Load<TextAsset>("JSON/item_drop_table");
|
|
var itemDropTables = JsonHelper.FromJson<ItemDropTable>(jsonFile.text);
|
|
|
|
foreach (var element in itemDropTables)
|
|
{
|
|
itemDropTableList.Add(element);
|
|
}
|
|
}
|
|
|
|
public void ItemDrop(int idx, Vector3 dropPosition)
|
|
{
|
|
var itemDropTable = ItemDropTableDictionary[idx];
|
|
var droppedItemList = itemDropTable.GetDroppedItemList();
|
|
foreach (var element in droppedItemList)
|
|
{
|
|
GameObject prefab = null;
|
|
if (itemDropTable.item_drop_type == ItemDropTable.ItemDropType.OCEAN)
|
|
{
|
|
prefab = oceanItemDropPrefab;
|
|
}
|
|
else if (itemDropTable.item_drop_type == ItemDropTable.ItemDropType.FIELD)
|
|
{
|
|
prefab = fieldItemDropPrefab;
|
|
}
|
|
var instantiateItem = Instantiate(prefab, dropPosition, Quaternion.identity);
|
|
instantiateItem.GetComponentInChildren<DropItemController>().Init(element);
|
|
}
|
|
}
|
|
|
|
public void Acquire(ItemSlot itemSlot)
|
|
{
|
|
DataManager.Inst.CurrentInventory.AddItem(itemSlot);
|
|
}
|
|
}
|
|
} |