using System.Collections.Generic; using BlueWater.Audios; using BlueWater.Uis; using Sirenix.OdinInspector; using UnityEngine; namespace BlueWater.Items { public class ItemManager : Singleton { [SerializeField, Required] private GameObject _dropItemControllerPrefab; [SerializeField, Required] private ItemDataSo _itemDataSo; public Dictionary ItemDictionary { get; private set; } [SerializeField, Required] private ItemDropTableSo _itemDropTableSo; public Dictionary ItemDropTableDictionary { get; private set; } protected override void OnAwake() { base.OnAwake(); Initialize(); } private void Initialize() { ItemDictionary = new Dictionary(_itemDataSo.ItemDataList.Count); foreach (var element in _itemDataSo.ItemDataList) { ItemDictionary.TryAdd(element.Idx, element); } ItemDropTableDictionary = new Dictionary(_itemDropTableSo.ItemDropTableList.Count); foreach (var element in _itemDropTableSo.ItemDropTableList) { ItemDropTableDictionary.TryAdd(element.Idx, element); } } public void ItemDrop(int idx, Vector3 dropPosition) { var itemDropTable = ItemDropTableDictionary[idx]; var droppedItemList = itemDropTable.GetDroppedItemList(); foreach (var element in droppedItemList) { var instantiateItem = Instantiate(_dropItemControllerPrefab, dropPosition, Quaternion.identity); instantiateItem.GetComponent().Initialize(element); } } public void ItemDropRandomPosition(int idx, Vector3 dropPosition) { var itemDropTable = ItemDropTableDictionary[idx]; 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); var instantiateItem = Instantiate(_dropItemControllerPrefab, newDropPosition, Quaternion.identity); instantiateItem.GetComponent().Initialize(element); instantiateItem.GetComponent().AddForce(Vector3.up * 20f, ForceMode.Impulse); } } public void Acquire(ItemSlot itemSlot) { AudioManager.Instance.PlaySfx("GetItem"); DataManager.Instance.CombatInventory.AddItem(itemSlot); CombatUiManager.Instance.ItemLootUi.ShowLootInfoUi(ItemDictionary[itemSlot.Idx], itemSlot.Count); } public ItemData GetItemDataByIdx(int idx) { if (ItemDictionary.TryGetValue(idx, out var itemData)) return itemData; Debug.LogError($"{idx}와 일치하는 아이템이 없습니다."); return null; } } }