CapersProject/Assets/02.Scripts/Item/ItemManager.cs
2024-06-04 03:26:03 +09:00

78 lines
3.1 KiB
C#

using System.Collections.Generic;
using BlueWater.Audios;
using BlueWater.Uis;
using Sirenix.OdinInspector;
using UnityEngine;
namespace BlueWater.Items
{
public class ItemManager : Singleton<ItemManager>
{
[SerializeField, Required]
private GameObject _dropItemControllerPrefab;
[SerializeField, Required]
private ItemDataSo _itemDataSo;
public Dictionary<int, ItemData> ItemDictionary { get; private set; }
[SerializeField, Required]
private ItemDropTableSo _itemDropTableSo;
public Dictionary<int, ItemDropTable> ItemDropTableDictionary { get; private set; }
protected override void OnAwake()
{
base.OnAwake();
Initialize();
}
private void Initialize()
{
ItemDictionary = new Dictionary<int, ItemData>(_itemDataSo.ItemDataList.Count);
foreach (var element in _itemDataSo.ItemDataList)
{
ItemDictionary.TryAdd(element.Idx, element);
}
ItemDropTableDictionary = new Dictionary<int, ItemDropTable>(_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<DropItemController>().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<DropItemController>().Initialize(element);
instantiateItem.GetComponent<Rigidbody>().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);
}
}
}