310 lines
8.4 KiB
C#
Raw Normal View History

2024-12-16 17:25:10 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class FreePanel : MonoBehaviour
{
public static FreePanel ins;
[SerializeField] private List<CraftingUI> _craftingUis;
[SerializeField] private List<DrawingSlot> _slotUis;
[SerializeField] private int itemID;
[SerializeField] private CraftingUI cItem;
[SerializeField] private ItemDrawingButton itemObj;
[SerializeField] private Transform itemParent;
2024-12-21 19:57:23 +08:00
[SerializeField] private Transform itemSwpanParent;
2024-12-16 17:25:10 +08:00
[SerializeField] private List<ItemDrawingButton> _itemButtons;
2024-12-21 19:57:23 +08:00
[SerializeField] private List<ItemDrawingButton> _itemSwpanButtons;
2024-12-16 17:25:10 +08:00
[SerializeField] private RectTransform seleTransform;
[SerializeField] private Button craft;
public int[] itemsSlot=new int[4];
private int id = 0;
private int index = -1;
public void SaveSeledID(int sendID)
{
id = sendID;
index = -1;
seleTransform.GetComponent<SeletItemUI>().SaveSprite(id);
}
private void Awake()
{
ins = this;
}
private void OnEnable()
{
craft.onClick.AddListener(CraftingEvent);
}
private void OnDisable()
{
craft.onClick.RemoveListener(CraftingEvent);
}
public void OpenFreePanel(int id)
{
itemID = id;
ResetSlot();
cItem.SaveIcon(id);
ResetCrafting(id);
2024-12-21 19:57:23 +08:00
ResetItemData();
2024-12-16 17:25:10 +08:00
ResetSlotItem();
}
public void ResetSlot()
{
for (int i = 0; i < itemsSlot.Length; i++)
{
itemsSlot[i] = 0;
}
}
public void SaveIndexID(int index, int id)
{
itemsSlot[index] = id;
}
public int slotNum(int id)
{
int num = 0;
foreach (var itemid in itemsSlot)
{
if (id==itemid)
{
num++;
}
}
return num;
}
public void ResetSlotItem()
{
foreach (var slot in _slotUis)
{
slot.ResetIcon(this);
}
}
public void ResetCrafting(int id)
{
var slotData = JsonTab.Instance.tables.CraftingRecipes.Get(id);
if (slotData.Levelprops1!=0)
{
_craftingUis[0].gameObject.SetActive(true);
_craftingUis[0].SaveIcon(slotData.Levelprops1);
}
else
{
_craftingUis[0].gameObject.SetActive(false);
}
if (slotData.Levelprops2!=0)
{
_craftingUis[1].gameObject.SetActive(true);
_craftingUis[1].SaveIcon(slotData.Levelprops2);
}
else
{
_craftingUis[1].gameObject.SetActive(false);
}
if (slotData.Levelprops3!=0)
{
_craftingUis[2].gameObject.SetActive(true);
_craftingUis[2].SaveIcon(slotData.Levelprops3);
}
else
{
_craftingUis[2].gameObject.SetActive(false);
}
if (slotData.Levelprops4!=0)
{
_craftingUis[3].gameObject.SetActive(true);
_craftingUis[3].SaveIcon(slotData.Levelprops4);
}
else
{
_craftingUis[3].gameObject.SetActive(false);
}
}
public void CraftItem(bool bo, Action action = null)
{
if (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();
});
}
else
{
action?.Invoke();
ResetSlot();
}
}
private void CraftingEvent()
{
CraftItem(DrawOpen());
2024-12-21 19:57:23 +08:00
ResetItemData();
2024-12-16 17:25:10 +08:00
ResetSlotItem();
}
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);
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);
}
}
public void ResetItemData()
{
foreach (var item in _itemButtons)
{
item.gameObject.SetActive(false);
}
var allData=JsonTab.Instance.tables.Item.DataList;
int index = 0;
for (int i = 0; i < allData.Count; i++)
{
if (DataManager.GetItem(allData[i].ID)>0&&allData[i].Proptype!=0&&allData[i].Proptype==1)
{
if (index>=_itemButtons.Count )
{
var add = Instantiate(itemObj.gameObject,itemParent);
_itemButtons.Add(add.GetComponent<ItemDrawingButton>());
}
_itemButtons[index].gameObject.SetActive(true);
_itemButtons[index].SetItemData(allData[i].ID,this);
index++;
}
}
2024-12-21 19:57:23 +08:00
ResetItem();
2024-12-16 17:25:10 +08:00
}
private void ResetItem()
{
2024-12-21 19:57:23 +08:00
foreach (var item in _itemSwpanButtons)
2024-12-16 17:25:10 +08:00
{
item.gameObject.SetActive(false);
}
var allData=JsonTab.Instance.tables.Item.DataList;
int index = 0;
for (int i = 0; i < allData.Count; i++)
{
if (DataManager.GetItem(allData[i].ID)>0&&allData[i].Proptype>1)
{
2024-12-21 19:57:23 +08:00
if (index>=_itemSwpanButtons.Count )
2024-12-16 17:25:10 +08:00
{
var add = Instantiate(itemObj.gameObject,itemParent);
2024-12-21 19:57:23 +08:00
_itemSwpanButtons.Add(add.GetComponent<ItemDrawingButton>());
2024-12-16 17:25:10 +08:00
}
2024-12-21 19:57:23 +08:00
_itemSwpanButtons[index].gameObject.SetActive(true);
_itemSwpanButtons[index].SetItemData(allData[i].ID,this);
2024-12-16 17:25:10 +08:00
index++;
}
}
}
public void SaveIndex(int slotIndex)
{
index = slotIndex;
}
public void Update()
{
if (Input.GetMouseButtonUp(0))
{
if (id != 0)
{
Debug.Log(index);
if (index != -1)
{
SaveIndexID(index, id);
ResetSlotItem();
}
id = 0;
seleTransform.position = new Vector3(-3000, 0, 0);
index = -1;
}
}
if (Input.GetMouseButton(0))
{
if (id != 0)
{
Vector2 mouse = Input.mousePosition;
seleTransform.position = mouse;
}
}
}
}