247 lines
8.6 KiB
C#
247 lines
8.6 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;
|
|
Inventory inventory;
|
|
|
|
private void Start()
|
|
{
|
|
tooltip = GameObject.Find("TooltipHandler").GetComponent<TooltipHandler>();
|
|
inventory = GameObject.Find("Inventory").GetComponent<Inventory>();
|
|
loadImages();
|
|
}
|
|
|
|
public void updateCurrentBag(int currentBag)
|
|
{
|
|
this.currentBag = currentBag;
|
|
}
|
|
|
|
public void setItem(Item item, int bag)
|
|
{
|
|
items[bag] = item;
|
|
itemRemoved[bag] = false;
|
|
}
|
|
|
|
public Item getItem(int bag)
|
|
{
|
|
return items[bag];
|
|
}
|
|
|
|
public void removeItem()
|
|
{
|
|
items[currentBag] = null;
|
|
itemRemoved[currentBag] = true;
|
|
}
|
|
|
|
public void showTooltip()
|
|
{
|
|
Item item = getItemForTooltip();
|
|
if (item != null)
|
|
{
|
|
inventory.itemDisplay.transform.Find("itemImage").GetComponent<RawImage>().texture = item.image;
|
|
inventory.itemDisplay.transform.Find("itemImage").GetComponent<RawImage>().color = item.rarityColor;
|
|
inventory.itemDisplay.transform.Find("itemName").GetComponent<Text>().text = item.getName();
|
|
string text = "Stats:\r\n";
|
|
Dictionary<string, int> attributes = item.getAttributes();
|
|
if (item.getRarity() == ItemRarity.LEGENDARY)
|
|
{
|
|
text = "All stats: +" + attributes["STR"];
|
|
}
|
|
else
|
|
{
|
|
foreach (string key in attributes.Keys)
|
|
{
|
|
text = text + key + ": +" + attributes[key] + "\r\n";
|
|
}
|
|
}
|
|
|
|
inventory.itemDisplay.transform.Find("itemStats").GetComponent<Text>().text = text;
|
|
int changeY = 0;
|
|
if (inventory.itemDisplay.transform.localScale == new Vector3(0,0,0))
|
|
{
|
|
if (Input.mousePosition.y < Screen.height / 2)
|
|
{
|
|
changeY = -150;
|
|
}
|
|
inventory.itemDisplay.transform.position = new Vector3(Input.mousePosition.x - 175, Input.mousePosition.y - changeY, 0);
|
|
}
|
|
inventory.itemDisplay.transform.localScale = new Vector3(1, 1, 1);
|
|
}
|
|
}
|
|
|
|
private Item getItemForTooltip()
|
|
{
|
|
Item item = null;
|
|
if (place == ItemPlace.BAG)
|
|
{
|
|
item = items[currentBag];
|
|
}
|
|
else
|
|
{
|
|
item = equip;
|
|
}
|
|
return item;
|
|
}
|
|
|
|
public void hideTooltip()
|
|
{
|
|
inventory.itemDisplay.transform.localScale = new Vector3(0,0,0);
|
|
}
|
|
|
|
public void OnMouseDrag()
|
|
{
|
|
inventory.setDrag(gameObject);
|
|
Item toMove;
|
|
if (place == ItemPlace.BAG)
|
|
{
|
|
toMove = items[currentBag];
|
|
}
|
|
else
|
|
{
|
|
toMove = equip;
|
|
}
|
|
if (toMove != null)
|
|
{
|
|
inventory.dragImage.GetComponent<RawImage>().color = toMove.rarityColor;
|
|
inventory.dragImage.GetComponent<RawImage>().texture = toMove.image;
|
|
inventory.dragImage.transform.position = new Vector3(Input.mousePosition.x - 50, Input.mousePosition.y - 50, 0);
|
|
}
|
|
}
|
|
|
|
public void OnMouseUp()
|
|
{
|
|
inventory.dragImage.GetComponent<RawImage>().color = new Color(0,0,0,0);
|
|
inventory.dragImage.GetComponent<RawImage>().texture = null;
|
|
inventory.dragImage.transform.position = new Vector3(0,0,0);
|
|
GameObject startDrag = inventory.getDrag();
|
|
Item item;
|
|
bool isSwap = false;
|
|
if (startDrag.GetComponent<InventorySlot>().place == ItemPlace.BAG)
|
|
{
|
|
item = startDrag.GetComponent<InventorySlot>().getItem(currentBag);
|
|
}
|
|
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)
|
|
{
|
|
if (startDrag.GetComponent<InventorySlot>().place == ItemPlace.BAG)
|
|
{
|
|
startDrag.GetComponent<InventorySlot>().setItem(items[currentBag], currentBag);
|
|
}
|
|
else
|
|
{
|
|
startDrag.GetComponent<InventorySlot>().setEquip(items[currentBag]);
|
|
inventory.calculateStatBoost(item.getAttributes(), false);
|
|
inventory.calculateStatBoost(items[currentBag].getAttributes(), true);
|
|
}
|
|
isSwap = true;
|
|
}
|
|
setItem(item, currentBag);
|
|
}
|
|
else
|
|
{
|
|
if (equip != null)
|
|
{
|
|
if (startDrag.GetComponent<InventorySlot>().place == ItemPlace.BAG)
|
|
{
|
|
startDrag.GetComponent<InventorySlot>().setItem(equip, currentBag);
|
|
isSwap = true;
|
|
inventory.calculateStatBoost(equip.getAttributes(), false);
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
inventory.calculateStatBoost(item.getAttributes(), true);
|
|
setEquip(item);
|
|
}
|
|
if (!isSwap)
|
|
{
|
|
if (startDrag.GetComponent<InventorySlot>().place != ItemPlace.BAG)
|
|
{
|
|
inventory.calculateStatBoost(startDrag.GetComponent<InventorySlot>().getEquip().getAttributes(), false);
|
|
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;
|
|
}
|
|
|
|
private void loadImages()
|
|
{
|
|
Texture image = null;
|
|
switch (place)
|
|
{
|
|
case ItemPlace.LEFTHAND:
|
|
image = Resources.Load<Texture>("Equipment/" + GameObject.Find("Player").GetComponent<Player>().getClass().classname + "/Inv_LeftHand");
|
|
break;
|
|
case ItemPlace.RIGHTHAND:
|
|
image = Resources.Load<Texture>("Equipment/" + GameObject.Find("Player").GetComponent<Player>().getClass().classname + "/Inv_RightHand");
|
|
break;
|
|
case ItemPlace.HELMET:
|
|
image = Resources.Load<Texture>("Equipment/Inv_Helmet");
|
|
break;
|
|
case ItemPlace.BOOTS:
|
|
image = Resources.Load<Texture>("Equipment/Inv_Boots");
|
|
break;
|
|
case ItemPlace.SHOULDER:
|
|
image = Resources.Load<Texture>("Equipment/Inv_Shoulders");
|
|
break;
|
|
case ItemPlace.AMULET:
|
|
image = Resources.Load<Texture>("Equipment/Inv_Amulet");
|
|
break;
|
|
case ItemPlace.RING:
|
|
image = Resources.Load<Texture>("Equipment/Inv_Ring");
|
|
break;
|
|
case ItemPlace.ARMOR:
|
|
image = Resources.Load<Texture>("Equipment/Inv_Chest");
|
|
break;
|
|
}
|
|
if (image != null)
|
|
{
|
|
gameObject.GetComponent<RawImage>().texture = image;
|
|
}
|
|
}
|
|
}
|
|
} |