WXGame/Blacksmith/Assets/Script/DrawingManager.cs

165 lines
4.0 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<ItemDataNum> _list;
2024-11-13 16:56:37 +08:00
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>();
2024-11-13 16:56:37 +08:00
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)
{
ItemDataNum newData = new ItemDataNum(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()
{
Dictionary<int, ItemDataNum> items = new Dictionary<int, ItemDataNum>();
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
{
var itemNum = new ItemDataNum(itemsSlot[i], 1);
2024-12-04 17:26:27 +08:00
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, ItemDataNum> craftings = new Dictionary<int, ItemDataNum>();
2024-12-04 17:26:27 +08:00
AddDrawingData(srafData.Levelprops1,ref craftings);
AddDrawingData(srafData.Levelprops2,ref craftings);
AddDrawingData(srafData.Levelprops3,ref craftings);
AddDrawingData(srafData.Levelprops4,ref craftings);
2024-12-13 20:38:15 +08:00
var data=JsonTab.Instance.tables.CraftingRecipes.Get(itemID);
2024-12-04 17:26:27 +08:00
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
{
2024-12-13 20:38:15 +08:00
foreach (var cData in craftings)
2024-11-13 16:56:37 +08:00
{
2024-12-13 20:38:15 +08:00
if (!items.ContainsKey(cData.Key))
2024-11-13 16:56:37 +08:00
{
return false;
}
2024-12-13 20:38:15 +08:00
if (items[cData.Key].num!=cData.Value.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, ItemDataNum> craftings)
2024-12-04 17:26:27 +08:00
{
if (id==0)
{
return;
}
if (craftings.ContainsKey(id))
{
craftings[id].num++;
}
else
{
var item =new ItemDataNum(id,1);
2024-12-04 17:26:27 +08:00
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;
}
}