239 lines
7.3 KiB
C#
239 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 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;
|
|
if (startDrag.place == ItemPlace.BAG)
|
|
{
|
|
item = startDrag.getItem(startDrag.currentBag);
|
|
}
|
|
else
|
|
{
|
|
item = startDrag.getEquip();
|
|
}
|
|
if (item != null)
|
|
{
|
|
item.move(startDrag, inventory, this);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
} |