2024-06-03 18:26:03 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace BlueWater.Items
|
|
|
|
{
|
|
|
|
public class ItemManager : Singleton<ItemManager>
|
|
|
|
{
|
|
|
|
[SerializeField, Required]
|
2024-06-21 22:11:53 +00:00
|
|
|
private Item _defaultItemPrefab;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
[SerializeField, Required]
|
|
|
|
private ItemDataSo _itemDataSo;
|
2024-06-21 22:11:53 +00:00
|
|
|
private Dictionary<int, ItemData> _itemDictionary;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
[SerializeField, Required]
|
|
|
|
private ItemDropTableSo _itemDropTableSo;
|
2024-06-21 22:11:53 +00:00
|
|
|
private Dictionary<int, ItemDropTable> _itemDropTableDictionary;
|
2024-06-03 18:26:03 +00:00
|
|
|
|
|
|
|
protected override void OnAwake()
|
|
|
|
{
|
|
|
|
base.OnAwake();
|
|
|
|
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Initialize()
|
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
_itemDictionary = new Dictionary<int, ItemData>(_itemDataSo.ItemDataList.Count);
|
2024-06-03 18:26:03 +00:00
|
|
|
foreach (var element in _itemDataSo.ItemDataList)
|
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
_itemDictionary.TryAdd(element.Idx, element);
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 22:11:53 +00:00
|
|
|
_itemDropTableDictionary = new Dictionary<int, ItemDropTable>(_itemDropTableSo.ItemDropTables.Count);
|
|
|
|
foreach (var element in _itemDropTableSo.ItemDropTables)
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
_itemDropTableDictionary.TryAdd(element.CharacterData.CharacterIdx, element);
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ItemDropRandomPosition(int idx, Vector3 dropPosition)
|
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
var itemDropTable = GetItemDropTableByIdx(idx);
|
|
|
|
if (itemDropTable == null) return;
|
|
|
|
|
2024-06-03 18:26:03 +00:00
|
|
|
var droppedItemList = itemDropTable.GetDroppedItemList();
|
|
|
|
foreach (var element in droppedItemList)
|
|
|
|
{
|
|
|
|
var newDropPositionX = Random.Range(dropPosition.x - 1f, dropPosition.x + 1f);
|
|
|
|
var newDropPositionZ = Random.Range(dropPosition.z - 1f, dropPosition.z + 1f);
|
|
|
|
var newDropPosition = new Vector3(newDropPositionX, dropPosition.y, newDropPositionZ);
|
2024-06-21 22:11:53 +00:00
|
|
|
|
|
|
|
var itemPrefab = _itemDictionary[element.Idx].ItemPrefab;
|
|
|
|
if (!itemPrefab)
|
|
|
|
{
|
|
|
|
itemPrefab = _defaultItemPrefab;
|
|
|
|
}
|
|
|
|
var instantiateItem = Instantiate(itemPrefab, newDropPosition, Quaternion.identity);
|
|
|
|
instantiateItem.Initialize(element);
|
|
|
|
instantiateItem.AddForce(Vector3.up * 20f, ForceMode.Impulse);
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-18 18:16:19 +00:00
|
|
|
|
|
|
|
public ItemData GetItemDataByIdx(int idx)
|
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
if (_itemDictionary.TryGetValue(idx, out var itemData)) return itemData;
|
|
|
|
|
|
|
|
Debug.LogError($"{idx}와 일치하는 아이템이 없습니다.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemDropTable GetItemDropTableByIdx(int idx)
|
|
|
|
{
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
|
|
|
Debug.Log("ItemDropTable이 비어있습니다.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_itemDropTableDictionary.TryGetValue(idx, out var itemDropTable)) return itemDropTable;
|
2024-06-18 18:16:19 +00:00
|
|
|
|
|
|
|
Debug.LogError($"{idx}와 일치하는 아이템이 없습니다.");
|
|
|
|
return null;
|
|
|
|
}
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|