TalesOfNovariel/Assets/Scripts/InventorySlot.cs

224 lines
7.3 KiB
C#

using Newtonsoft.Json.Linq;
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;
Equipment 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 int getCurrentBag()
{
return 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 removeItem(int bag)
{
items[bag] = 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();
inventory.itemDisplay.transform.Find("itemStats").GetComponent<Text>().text = item.getInformation();
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 Equipment getEquip()
{
return equip;
}
public void setEquip(Equipment 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;
}
public void loadSlot(JToken json, int bag)
{
if (bag == -1)
{
equip = new Equipment(json);
}
else
{
if(json["place"] != null){
items[bag] = new Equipment(json);
}
else{
items[bag] = new Item(json);
}
}
}
public void showSlotOptions(){
GameObject current = EventSystem.current.currentSelectedGameObject;
GameObject.Find("Inventory").GetComponent<Inventory>().currentSlot = current;
GameObject.Find("btnEquip").transform.Find("Text").GetComponent<Text>().text = "Equip";
GameObject.Find("btnEquip").GetComponent<Button>().interactable = true;
if(equip != null || items[currentBag] != null){
if(equip != null){
GameObject.Find("btnEquip").transform.Find("Text").GetComponent<Text>().text = "Unequip";
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnEquip"));
}
if(items[currentBag] != null){
if(items[currentBag] is Equipment){
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnEquip"));
}
else{
GameObject.Find("btnEquip").GetComponent<Button>().interactable = false;
EventSystem.current.SetSelectedGameObject(GameObject.Find("btnTrash"));
}
}
GameObject.Find("pnlInventoryActions").transform.position = new Vector3(current.transform.position.x + 125, current.transform.position.y, 0);
GameObject.Find("pnlInventoryActions").transform.localScale = new Vector3(1,1,1);
}
}
}
}