331 lines
9.1 KiB
C#
331 lines
9.1 KiB
C#
|
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;
|
||
|
[SerializeField] private List<ItemDrawingButton> _itemButtons;
|
||
|
[SerializeField] private RectTransform seleTransform;
|
||
|
[SerializeField] private Button craft;
|
||
|
[SerializeField] private Button _switchIButton;
|
||
|
[SerializeField] private Button _switchWButton;
|
||
|
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);
|
||
|
_switchIButton.onClick.AddListener(ResetItemData);
|
||
|
_switchWButton.onClick.AddListener(ResetItem);
|
||
|
}
|
||
|
|
||
|
private void OnDisable()
|
||
|
{
|
||
|
craft.onClick.RemoveListener(CraftingEvent);
|
||
|
_switchIButton.onClick.RemoveListener(ResetItemData);
|
||
|
_switchWButton.onClick.RemoveListener(ResetItem);
|
||
|
}
|
||
|
|
||
|
public void OpenFreePanel(int id)
|
||
|
{
|
||
|
itemID = id;
|
||
|
ResetSlot();
|
||
|
cItem.SaveIcon(id);
|
||
|
ResetCrafting(id);
|
||
|
if (!_switchIButton.gameObject.activeSelf)
|
||
|
{
|
||
|
ResetItemData();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
ResetItem();
|
||
|
}
|
||
|
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());
|
||
|
if (!_switchIButton.gameObject.activeSelf)
|
||
|
{
|
||
|
ResetItemData();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
ResetItem();
|
||
|
}
|
||
|
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++;
|
||
|
}
|
||
|
}
|
||
|
_switchIButton.gameObject.SetActive(false);
|
||
|
_switchWButton.gameObject.SetActive(true);
|
||
|
}
|
||
|
private void ResetItem()
|
||
|
{
|
||
|
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>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++;
|
||
|
}
|
||
|
}
|
||
|
_switchIButton.gameObject.SetActive(true);
|
||
|
_switchWButton.gameObject.SetActive(false);
|
||
|
}
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|