WXGame/Blacksmith/Assets/Script/DrawingManager.cs

164 lines
3.9 KiB
C#
Raw Normal View History

2024-11-13 16:56:37 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2024-12-04 17:26:27 +08:00
using cfg.BlacksmithData;
2024-11-13 16:56:37 +08:00
using UnityEngine;
public class DrawingManager : MonoBehaviour
{
private List<ItemData> _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<ItemData> itemDatas = new List<ItemData>();
foreach (var item in itemsSlot)
{
if (item!=0)
{
2024-12-04 17:26:27 +08:00
var data=itemDatas.FirstOrDefault(d => d.id == item);
2024-11-13 16:56:37 +08:00
if (data==null)
{
2024-12-04 17:26:27 +08:00
ItemData newData = new ItemData(item, 1);
2024-11-13 16:56:37 +08:00
itemDatas.Add(newData);
}
else
{
data.num++;
}
}
}
DataManager.RemoveItem(itemDatas, (bo) =>
{
2024-12-04 17:26:27 +08:00
DataManager.AddItem(itemID,1);
2024-11-13 16:56:37 +08:00
action?.Invoke();
ResetSlot();
});
}
else
{
action?.Invoke();
ResetSlot();
}
}
/// <summary>
/// 判断是否能打开
/// </summary>
/// <returns></returns>
public bool DrawOpen()
{
2024-12-04 17:26:27 +08:00
Dictionary<int, ItemData> items = new Dictionary<int, ItemData>();
2024-11-13 16:56:37 +08:00
for (int i = 0; i < itemsSlot.Length; i++)
{
if (itemsSlot[i]!=0)
{
2024-12-04 17:26:27 +08:00
if (items.ContainsKey(itemsSlot[i]))
2024-11-13 16:56:37 +08:00
{
2024-12-04 17:26:27 +08:00
items[itemsSlot[i]].num++;
2024-11-13 16:56:37 +08:00
}
else
{
2024-12-04 17:26:27 +08:00
var itemNum = new ItemData(itemsSlot[i], 1);
items.Add(itemsSlot[i],itemNum);
2024-11-13 16:56:37 +08:00
}
}
}
2024-12-04 17:26:27 +08:00
var srafData = JsonTab.Instance.tables.CraftingRecipes.Get(itemID);
Dictionary<int, ItemData> craftings = new Dictionary<int, ItemData>();
AddDrawingData(srafData.Levelprops1,ref craftings);
AddDrawingData(srafData.Levelprops2,ref craftings);
AddDrawingData(srafData.Levelprops3,ref craftings);
AddDrawingData(srafData.Levelprops4,ref craftings);
2024-11-13 16:56:37 +08:00
var srafting = GameSystem.ins.DataAsset.crafting.FirstOrDefault(d => d.id == itemID);
2024-12-04 17:26:27 +08:00
if (craftings.Count!=items.Count)
2024-11-13 16:56:37 +08:00
{
return false;
}
else
{
foreach (var cData in srafting.craftID)
{
2024-12-04 17:26:27 +08:00
if (!items.ContainsKey(cData.id))
2024-11-13 16:56:37 +08:00
{
return false;
}
2024-12-04 17:26:27 +08:00
if (items[cData.id].num!=cData.num)
2024-11-13 16:56:37 +08:00
{
return false;
}
}
}
return true;
}
2024-12-04 17:26:27 +08:00
private static void AddDrawingData(int id,ref Dictionary<int, ItemData> craftings)
{
if (id==0)
{
return;
}
if (craftings.ContainsKey(id))
{
craftings[id].num++;
}
else
{
var item =new ItemData(id,1);
craftings.Add(id,item);
}
}
/// <summary>
2024-11-13 16:56:37 +08:00
/// 设置锻造槽位物品
/// </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;
}
}