TalesOfNovariel/Assets/Scripts/InventorySlot.cs

158 lines
4.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Assets.Scripts
{
public class InventorySlot : MonoBehaviour
{
TooltipHandler tooltip;
Item[] items = new Item[3];
public bool[] itemRemoved = {false, false, false};
int currentBag = 0;
public ItemPlace place;
Item equip;
private void Start()
{
tooltip = GameObject.Find("TooltipHandler").GetComponent<TooltipHandler>();
}
public void updateCurrentBag(int currentBag)
{
this.currentBag = currentBag;
}
public void setItem(Item item)
{
items[currentBag] = item;
itemRemoved[currentBag] = false;
}
public Item getItem()
{
return items[currentBag];
}
public void removeItem()
{
items[currentBag] = null;
itemRemoved[currentBag] = true;
}
public void showTooltip()
{
if (place == ItemPlace.BAG)
{
if (items[currentBag] != null)
{
string text = items[currentBag].getDisplayText();
Item equip = GameObject.Find("Inventory").GetComponent<Inventory>().getEquip(items[currentBag].getPlace());
if (equip != null)
{
text = "(Current)\r\n" + equip.getDisplayText() + "----------\r\n" + text;
}
tooltip.showTooltip(text);
}
}
else
{
if (equip != null)
{
tooltip.showTooltip(equip.getDisplayText());
}
}
}
public void hideTooltip()
{
tooltip.hideToolTip();
}
public void onClick()
{
removeItem();
tooltip.hideToolTip();
}
public void OnMouseDrag()
{
GameObject.Find("Inventory").GetComponent<Inventory>().setDrag(gameObject);
}
public void OnMouseUp()
{
GameObject startDrag = GameObject.Find("Inventory").GetComponent<Inventory>().getDrag();
Item item;
bool isSwap = false;
if (startDrag.GetComponent<InventorySlot>().place == ItemPlace.BAG)
{
item = startDrag.GetComponent<InventorySlot>().getItem();
}
else
{
item = startDrag.GetComponent<InventorySlot>().getEquip();
}
if (item != null)
{
if (place != item.getPlace() && place != ItemPlace.BAG)
{
return;
}
if (place == ItemPlace.BAG)
{
if (items[currentBag] != null)
{
startDrag.GetComponent<InventorySlot>().setItem(items[currentBag]);
isSwap = true;
}
setItem(item);
}
else
{
if (equip != null)
{
if (startDrag.GetComponent<InventorySlot>().place == ItemPlace.BAG)
{
startDrag.GetComponent<InventorySlot>().setItem(equip);
isSwap = true;
}
else
{
return;
}
}
setEquip(item);
}
if (!isSwap)
{
if (startDrag.GetComponent<InventorySlot>().place != ItemPlace.BAG)
{
startDrag.GetComponent<InventorySlot>().removeEquip();
}
else
{
startDrag.GetComponent<InventorySlot>().removeItem();
}
}
}
}
public Item getEquip()
{
return equip;
}
public void setEquip(Item item)
{
equip = item;
}
public void removeEquip()
{
equip = null;
}
}
}