using System; using System.Collections; using System.Collections.Generic; using System.Linq; using cfg.BlacksmithData; using UnityEngine; public class DrawingManager : MonoBehaviour { private List _list; public int[] itemsSlot=new int[4]; //当前购买的道具id public int itemID = 0; public void OnEnable() { ResetSlot(); } /// /// 开始合成 /// /// public void CraftItem(bool bo, Action action = null) { if (DrawOpen()&&bo) { List itemDatas = new List(); 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(); } } /// /// 判断是否能打开 /// /// public bool DrawOpen() { Dictionary items = new Dictionary(); 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 craftings = new Dictionary(); 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 craftings) { if (id==0) { return; } if (craftings.ContainsKey(id)) { craftings[id].num++; } else { var item =new ItemDataNum(id,1); craftings.Add(id,item); } } /// /// 设置锻造槽位物品 /// /// /// public void SaveIndexID(int index, int id) { itemsSlot[index] = id; } /// /// 重置锻造槽位 /// 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; } }