using System.Collections.Generic; using Sirenix.OdinInspector; using UnityEngine; namespace BlueWater.Items { public class ItemManager : Singleton { [SerializeField, Required] private Item _defaultItemPrefab; [SerializeField, Required] private ItemDataSo _itemDataSo; private Dictionary _itemDictionary; [SerializeField, Required] private ItemDropTableSo _itemDropTableSo; private Dictionary _itemDropTableDictionary; 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.ItemDropTables.Count); foreach (var element in _itemDropTableSo.ItemDropTables) { _itemDropTableDictionary.TryAdd(element.CharacterData.CharacterIdx, element); } } public void ItemDropRandomPosition(int idx, Vector3 dropPosition) { var itemDropTable = GetItemDropTableByIdx(idx); if (itemDropTable == null) return; 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 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); } } public ItemData GetItemDataByIdx(int idx) { 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; Debug.LogError($"{idx}와 일치하는 아이템이 없습니다."); return null; } } }