CapersProject/Assets/02.Scripts/Item/ItemManager.cs
Nam Tae Gun fb6a0a14f2 Ver 0.2.3.3 업데이트 내용
+ Title Ui 수정
+ 전투 맵 이동 위치 수정
+ 맵 입구 이미지 변경
+ 풀잎 아이템 추가에 따른 Excel, Json, So 수정
+ 타이탄 슬라임 맵에서 풀이 잘릴 때, 40% 확률로 풀잎을 드롭
+ 타이탄 슬라임 젬스톤 위치 및 재질 변경
+ AutoDropItem 프리팹 추가
2024-06-30 00:59:31 +09:00

112 lines
4.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector;
using UnityEngine;
namespace BlueWater.Items
{
public class ItemManager : Singleton<ItemManager>
{
[SerializeField, Required]
private Item _defaultItemPrefab;
[SerializeField, Required]
private ItemDataSo _itemDataSo;
private Dictionary<int, ItemData> _itemDictionary;
[SerializeField, Required]
private ItemDropTableSo _itemDropTableSo;
private Dictionary<int, ItemDropTable> _itemDropTableDictionary;
[Title("드롭 아이템 설정")]
[SerializeField]
private float _randomDropRadius = 3f;
[SerializeField]
private float _minSeparationDistance = 1.5f;
private const int MaxAttempts = 1000;
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.ItemDropTables.Count);
foreach (var element in _itemDropTableSo.ItemDropTables)
{
_itemDropTableDictionary.TryAdd(element.CharacterData.CharacterIdx, element);
}
}
public void ItemDropRandomPosition(int idx, Vector3 dropPosition, float randomDropRadius = float.PositiveInfinity)
{
var itemDropTable = GetItemDropTableByIdx(idx);
if (itemDropTable == null) return;
var droppedItemList = itemDropTable.GetDroppedItemList();
var droppedPositions = new List<Vector3>();
foreach (var element in droppedItemList)
{
Vector3 newDropPosition;
var attempts = 0;
do
{
var radius = float.IsPositiveInfinity(randomDropRadius) ? _randomDropRadius : randomDropRadius;
attempts++;
var newDropPositionX = Random.Range(dropPosition.x - radius, dropPosition.x + radius);
var newDropPositionZ = Random.Range(dropPosition.z - radius, dropPosition.z + radius);
newDropPosition = new Vector3(newDropPositionX, dropPosition.y, newDropPositionZ);
} while (!IsDropPositionAvailable(newDropPosition, droppedPositions) && attempts < MaxAttempts);
droppedPositions.Add(newDropPosition);
var itemPrefab = _itemDictionary[element.Idx].ItemPrefab;
if (!itemPrefab)
{
itemPrefab = _defaultItemPrefab;
}
var instantiateItem = Instantiate(itemPrefab, newDropPosition, itemPrefab.transform.rotation);
instantiateItem.Initialize(element);
instantiateItem.AddForce(Vector3.up * 20f, ForceMode.Impulse);
}
}
private bool IsDropPositionAvailable(Vector3 position, List<Vector3> positions)
{
return positions.Any(pos => Vector3.Distance(position, pos) > _minSeparationDistance);
}
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;
}
}
}