TalesOfNovariel/Assets/Scripts/InventorySlot.cs

279 lines
9.3 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];
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;
}
public Item getItem(int bag)
{
return items[bag];
}
public void removeItem()
{
items[currentBag] = null;
}
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);
InventorySlot startDrag = inventory.getDrag().GetComponent<InventorySlot>();
Item item;
bool isSwap = false;
if (startDrag.place == ItemPlace.BAG)
{
item = startDrag.getItem(startDrag.currentBag);
}
else
{
item = startDrag.getEquip();
}
if (item != null)
{
if (place != item.getPlace() && place != ItemPlace.BAG)
{
return;
}
if (place == ItemPlace.BAG)
{
if (items[currentBag] != null)
{
if (items[currentBag].getPlace() != startDrag.place)
{
return;
}
if (startDrag.place == ItemPlace.BAG)
{
startDrag.setItem(items[currentBag], currentBag);
}
else
{
startDrag.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.place == ItemPlace.BAG)
{
startDrag.setItem(equip, startDrag.currentBag);
isSwap = true;
inventory.calculateStatBoost(equip.getAttributes(), false);
}
else
{
return;
}
}
inventory.calculateStatBoost(item.getAttributes(), true);
setEquip(item);
}
if (!isSwap)
{
if (startDrag.place != ItemPlace.BAG)
{
inventory.calculateStatBoost(startDrag.getEquip().getAttributes(), false);
startDrag.removeEquip();
}
else
{
startDrag.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;
}
}
public string saveGame()
{
string result = "";
if (place == ItemPlace.BAG)
{
for (int i = 1; i <= items.Length;i++)
{
if (items[i-1] == null)
{
result = result + "\"bag" + i + "\": \"empty\"";
}
else
{
result = result + "\"bag" + i + "\": {\r\n";
result = result + items[i-1].saveGame();
result = result + "\r\n}";
}
if (i != 3)
{
result = result + ",\r\n";
}
}
}
else
{
result = result + equip.saveGame();
}
return result;
}
}
}