Project_WL/Assets/Script/Info/DropItemInfo.cs

102 lines
3.1 KiB
C#

using System.Collections.Generic;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine;
public class DropItemInfo : MonoBehaviourSingletonTemplate<DropItemInfo>
{
Dictionary<int, List<DropItem>> dic_dropitem = new Dictionary<int, List<DropItem>>();
Dictionary<int, AsyncOperationHandle<GameObject>> dic_Handle = new Dictionary<int, AsyncOperationHandle<GameObject>>();
Dictionary<int, string> dic_addrPath = new Dictionary<int, string>
{
{ -1, "Assets/Res_Addr/Obj/DropItem_Item.prefab" },
{ 2, "Assets/Res_Addr/Obj/DropItem_Coin.prefab" },
};
List<DropItem> list_dropitems = new List<DropItem>();
private void Start()
{
Make_DropItem(-1, 2);
Make_DropItem(2, 30);
}
void Make_DropItem(int key, int count)
{
AddrResourceMgr.Ins.LoadObject<GameObject>(dic_addrPath[key], handle =>
{
if (handle.IsDone && handle.Status == AsyncOperationStatus.Succeeded)
{
dic_Handle.Add(key, handle);
dic_dropitem.Add(key, new List<DropItem> { DSUtil.Get_Clone<DropItem>(dic_Handle[key].Result, transform) });
for (int i = 0; i < count - 1; i++)
Get_DropItem(key);
}
});
}
DropItem Get_DropItem(int key)
{
var dropitem = DSUtil.Get_Clone<DropItem>(dic_Handle[key].Result, transform);
dropitem.gameObject.SetActive(false);
dic_dropitem[key].Add(dropitem);
return dropitem;
}
public void Drop_DropItem(int itemid, int amount, Vector3 startpos)
{
int index = -1;
var item = table_itemlist.Ins.Get_Data(itemid);
switch (item.n_ItemID)
{
case 2: index = item.n_ItemID; break;
}
if (dic_dropitem.ContainsKey(index))
{
for (int i = 0; i < dic_dropitem[index].Count; i++)
{
var temp = dic_dropitem[index][i];
if (!temp.gameObject.activeInHierarchy)
{
temp.Set(itemid, amount, startpos);
break;
}
}
}
else
{
var dropitem = Get_DropItem(index);
dropitem.Set(itemid, amount, startpos);
}
}
public List<DropItem> Get_DropItems()
{
list_dropitems.Clear();
foreach (var itemList in dic_dropitem.Values)
{
list_dropitems.AddRange(itemList.FindAll(item => item.gameObject.activeInHierarchy));
}
return list_dropitems;
}
public DropItem Get_NearestDropItem(Vector3 _pos)
{
var items = Get_DropItems();
if (items.Count == 0)
return null;
DropItem nearestItem = items[0];
float minDistance = Vector3.Distance(_pos, nearestItem.transform.position);
foreach (var item in items)
{
float distance = Vector3.Distance(_pos, item.transform.position);
if (distance < minDistance)
{
minDistance = distance;
nearestItem = item;
}
}
return nearestItem;
}
}