235 lines
7.6 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 dragImage;
public GameObject itemDisplay;
public GameObject[] bags;
public GameObject[] slots;
Dictionary<string, int> statBoost;
public int currentBag = -1;
GameObject startDrag;
TooltipHandler tooltip;
// Start is called before the first frame update
void Start()
{
statBoost = new Dictionary<string, int>();
statBoost.Add("HP", 0);
statBoost.Add("MP", 0);
statBoost.Add("HPR", 0);
statBoost.Add("MPR", 0);
statBoost.Add("STR", 0);
statBoost.Add("DEX", 0);
statBoost.Add("INT", 0);
statBoost.Add("LCK", 0);
tooltip = GameObject.Find("TooltipHandler").GetComponent<TooltipHandler>();
itemDisplay.transform.localScale = new Vector3(0,0,0);
changeCurrentBag(0);
}
// Update is called once per frame
void Update()
{
checkInventoryColors();
checkEquipColors();
}
public void addItem(Item item)
{
if (item != null)
{
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(i) == null)
{
slots[j].GetComponent<InventorySlot>().setItem(item, i);
itemAdded = true;
slots[j].GetComponent<RawImage>().color = item.rarityColor;
slots[j].GetComponent<RawImage>().texture = item.image;
break;
}
}
if (itemAdded)
{
GameObject.Find("UIHandler").GetComponent<UIHandler>().showMessage("SUCCESS;You got an item!");
break;
}
}
if (!itemAdded)
{
GameObject.Find("UIHandler").GetComponent<UIHandler>().showMessage("ERROR;No inventory space left.");
}
}
}
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(currentBag);
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;
}
}
public void calculateStatBoost(Dictionary<string, int> attributes, bool isAddition)
{
foreach (string key in attributes.Keys)
{
if (isAddition)
{
statBoost[key] = statBoost[key] + attributes[key];
}
else
{
statBoost[key] = statBoost[key] - attributes[key];
}
}
}
public Dictionary<string, int> getEquipmentBonus()
{
if (statBoost == null)
{
statBoost = new Dictionary<string, int>();
statBoost.Add("HP", 0);
statBoost.Add("MP", 0);
statBoost.Add("HPR", 0);
statBoost.Add("MPR", 0);
statBoost.Add("STR", 0);
statBoost.Add("DEX", 0);
statBoost.Add("INT", 0);
statBoost.Add("LCK", 0);
}
return statBoost;
}
}
}