Added items, inventory, basic inv mechanics and art, added item comparison, v1.3.0
This commit is contained in:
181
Assets/Scripts/Inventory.cs
Normal file
181
Assets/Scripts/Inventory.cs
Normal file
@@ -0,0 +1,181 @@
|
||||
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 hands;
|
||||
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 = Color.red;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (itemAdded)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void changeCurrentBag(int index)
|
||||
{
|
||||
if (currentBag != -1)
|
||||
{
|
||||
bags[currentBag].GetComponent<RawImage>().color = Color.white;
|
||||
}
|
||||
currentBag = index;
|
||||
bags[currentBag].GetComponent<RawImage>().color = Color.cyan;
|
||||
checkInventoryColors();
|
||||
foreach(GameObject slot in slots)
|
||||
{
|
||||
slot.GetComponent<InventorySlot>().updateCurrentBag(currentBag);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkInventoryColors()
|
||||
{
|
||||
for (int i = 0; i < slots.Length; i++)
|
||||
{
|
||||
if (slots[i].GetComponent<InventorySlot>().getItem() != null)
|
||||
{
|
||||
slots[i].GetComponent<RawImage>().color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
slots[i].GetComponent<RawImage>().color = Color.white;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkEquipColors()
|
||||
{
|
||||
GameObject slot = head;
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
slot = head;
|
||||
break;
|
||||
case 1:
|
||||
slot = rightHand;
|
||||
break;
|
||||
case 2:
|
||||
slot = leftHand;
|
||||
break;
|
||||
case 3:
|
||||
slot = hands;
|
||||
break;
|
||||
case 4:
|
||||
slot = amulet;
|
||||
break;
|
||||
case 5:
|
||||
slot = feet;
|
||||
break;
|
||||
case 6:
|
||||
slot = shoulders;
|
||||
break;
|
||||
case 7:
|
||||
slot = chest;
|
||||
break;
|
||||
case 8:
|
||||
slot = ring;
|
||||
break;
|
||||
}
|
||||
if (slot.GetComponent<InventorySlot>().getEquip() != null)
|
||||
{
|
||||
slot.GetComponent<RawImage>().color = Color.red;
|
||||
}
|
||||
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.SHIELD:
|
||||
return leftHand.GetComponent<InventorySlot>().getEquip();
|
||||
case ItemPlace.WEAPON:
|
||||
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();
|
||||
case ItemPlace.GLOVES:
|
||||
return hands.GetComponent<InventorySlot>().getEquip();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user