183 lines
5.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Assets.Scripts
{
public class Inventory : MonoBehaviour
{
public GameObject head;
public GameObject chest;
public GameObject shoulders;
public GameObject feet;
public GameObject ring;
public GameObject amulet;
public GameObject leftHand;
public GameObject rightHand;
public GameObject[] bags;
public GameObject[] slots;
public int currentBag = -1;
GameObject startDrag;
// Start is called before the first frame update
void Start()
{
changeCurrentBag(0);
}
// Update is called once per frame
void Update()
{
checkInventoryColors();
checkEquipColors();
}
public void addItem(Item item)
{
if (item != null)
{
GameObject.Find("UIHandler").GetComponent<UIHandler>().showMessage("SUCCESS;You got an item!");
bool itemAdded = false;
for (int i = 0; i < bags.Length; i++)
{
for (int j = 0; j < slots.Length; j++)
{
if (slots[j].GetComponent<InventorySlot>().getItem() == null)
{
slots[j].GetComponent<InventorySlot>().setItem(item);
itemAdded = true;
slots[j].GetComponent<RawImage>().color = item.rarityColor;
slots[j].GetComponent<RawImage>().texture = item.image;
break;
}
}
if (itemAdded)
{
break;
}
}
}
}
public void changeCurrentBag(int index)
{
if (currentBag != -1)
{
bags[currentBag].transform.Find("Selected").GetComponent<RawImage>().color = Color.white;
}
currentBag = index;
bags[currentBag].transform.Find("Selected").GetComponent<RawImage>().color = Color.cyan;
checkInventoryColors();
foreach(GameObject slot in slots)
{
slot.GetComponent<InventorySlot>().updateCurrentBag(currentBag);
}
}
private void checkInventoryColors()
{
Item item;
for (int i = 0; i < slots.Length; i++)
{
item = slots[i].GetComponent<InventorySlot>().getItem();
if (item != null)
{
slots[i].GetComponent<RawImage>().color = item.rarityColor;
slots[i].GetComponent<RawImage>().texture = item.image;
}
else
{
slots[i].GetComponent<RawImage>().color = Color.white;
slots[i].GetComponent<RawImage>().texture = null;
}
}
}
private void checkEquipColors()
{
GameObject slot = head;
Item item;
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0:
slot = head;
break;
case 1:
slot = rightHand;
break;
case 2:
slot = leftHand;
break;
case 3:
slot = amulet;
break;
case 4:
slot = feet;
break;
case 5:
slot = shoulders;
break;
case 6:
slot = chest;
break;
case 7:
slot = ring;
break;
}
item = slot.GetComponent<InventorySlot>().getEquip();
if (item != null)
{
slot.GetComponent<RawImage>().color = item.rarityColor;
slot.GetComponent<RawImage>().texture = item.image;
}
else
{
slot.GetComponent<RawImage>().color = Color.white;
}
}
}
public void setDrag(GameObject slot)
{
startDrag = slot;
}
public GameObject getDrag()
{
return startDrag;
}
public Item getEquip(ItemPlace place)
{
switch (place)
{
case ItemPlace.LEFTHAND:
return leftHand.GetComponent<InventorySlot>().getEquip();
case ItemPlace.RIGHTHAND:
return rightHand.GetComponent<InventorySlot>().getEquip();
case ItemPlace.HELMET:
return head.GetComponent<InventorySlot>().getEquip();
case ItemPlace.BOOTS:
return feet.GetComponent<InventorySlot>().getEquip();
case ItemPlace.SHOULDER:
return shoulders.GetComponent<InventorySlot>().getEquip();
case ItemPlace.AMULET:
return amulet.GetComponent<InventorySlot>().getEquip();
case ItemPlace.RING:
return ring.GetComponent<InventorySlot>().getEquip();
case ItemPlace.ARMOR:
return chest.GetComponent<InventorySlot>().getEquip();
default:
return null;
}
}
}
}