WXGame/Blacksmith/Assets/Script/DrawingManager.cs
2024-12-13 20:38:15 +08:00

165 lines
4.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using cfg.BlacksmithData;
using UnityEngine;
public class DrawingManager : MonoBehaviour
{
private List<ItemDataNum> _list;
public int[] itemsSlot=new int[4];
//当前购买的道具id
public int itemID = 0;
public void OnEnable()
{
ResetSlot();
}
/// <summary>
/// 开始合成
/// </summary>
/// <param name="action"></param>
public void CraftItem(bool bo, Action action = null)
{
if (DrawOpen()&&bo)
{
List<ItemDataNum> itemDatas = new List<ItemDataNum>();
foreach (var item in itemsSlot)
{
if (item!=0)
{
var data=itemDatas.FirstOrDefault(d => d.id == item);
if (data==null)
{
ItemDataNum newData = new ItemDataNum(item, 1);
itemDatas.Add(newData);
}
else
{
data.num++;
}
}
}
DataManager.RemoveItem(itemDatas, (bo) =>
{
DataManager.AddItem(itemID,1);
action?.Invoke();
ResetSlot();
});
}
else
{
action?.Invoke();
ResetSlot();
}
}
/// <summary>
/// 判断是否能打开
/// </summary>
/// <returns></returns>
public bool DrawOpen()
{
Dictionary<int, ItemDataNum> items = new Dictionary<int, ItemDataNum>();
for (int i = 0; i < itemsSlot.Length; i++)
{
if (itemsSlot[i]!=0)
{
if (items.ContainsKey(itemsSlot[i]))
{
items[itemsSlot[i]].num++;
}
else
{
var itemNum = new ItemDataNum(itemsSlot[i], 1);
items.Add(itemsSlot[i],itemNum);
}
}
}
var srafData = JsonTab.Instance.tables.CraftingRecipes.Get(itemID);
Dictionary<int, ItemDataNum> craftings = new Dictionary<int, ItemDataNum>();
AddDrawingData(srafData.Levelprops1,ref craftings);
AddDrawingData(srafData.Levelprops2,ref craftings);
AddDrawingData(srafData.Levelprops3,ref craftings);
AddDrawingData(srafData.Levelprops4,ref craftings);
var data=JsonTab.Instance.tables.CraftingRecipes.Get(itemID);
var srafting = GameSystem.ins.DataAsset.crafting.FirstOrDefault(d => d.id == itemID);
if (craftings.Count!=items.Count)
{
return false;
}
else
{
foreach (var cData in craftings)
{
if (!items.ContainsKey(cData.Key))
{
return false;
}
if (items[cData.Key].num!=cData.Value.num)
{
return false;
}
}
}
return true;
}
private static void AddDrawingData(int id,ref Dictionary<int, ItemDataNum> craftings)
{
if (id==0)
{
return;
}
if (craftings.ContainsKey(id))
{
craftings[id].num++;
}
else
{
var item =new ItemDataNum(id,1);
craftings.Add(id,item);
}
}
/// <summary>
/// 设置锻造槽位物品
/// </summary>
/// <param name="index"></param>
/// <param name="id"></param>
public void SaveIndexID(int index, int id)
{
itemsSlot[index] = id;
}
/// <summary>
/// 重置锻造槽位
/// </summary>
public void ResetSlot()
{
for (int i = 0; i < itemsSlot.Length; i++)
{
itemsSlot[i] = 0;
}
}
public int slotNum(int id)
{
int num = 0;
foreach (var itemid in itemsSlot)
{
if (id==itemid)
{
num++;
}
}
return num;
}
}