Untitled
unknown
plain_text
2 years ago
7.5 kB
11
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using TMPro;
public class Inventory : MonoBehaviour
{
[HideInInspector] public int heldItemIndex = 0;
public GameObject equippedObject;
public Item selectedObject;
public Item emptyObjectTemplate;
[Header("Inventory Values")]
[HideInInspector] public List<Item> inventoryItems = new List<Item>();
[HideInInspector] public List<GameObject> inventoryObjects = new List<GameObject>();
public List<GameObject> slotList = new List<GameObject>();
public bool inventoryFull = false;
[Header("UI Values")]
public TextMeshProUGUI itemName;
public TextMeshProUGUI itemDescription;
public float spinSpeed;
public Transform spinOrigin;
private bool canSpin = true;
private void Start()
{
Initialise();
}
private void Initialise()
{
heldItemIndex = 0;
}
private void Update()
{
if (equippedObject != null && inventoryObjects.Count > 0)
{
if (equippedObject.activeSelf == false)
equippedObject.SetActive(true);
foreach (GameObject _object in inventoryObjects)
{
if (_object != equippedObject)
_object.SetActive(false);
}
}
else if (selectedObject == emptyObjectTemplate)
{
equippedObject = null;
}
if (inventoryObjects.Count > 0)
selectedObject = inventoryItems[heldItemIndex];
else if (inventoryObjects.Count == 0)
selectedObject = emptyObjectTemplate;
itemName.text = selectedObject.displayName;
itemDescription.text = selectedObject.description;
if (inventoryObjects.Count == 4)
inventoryFull = true;
else
inventoryFull = false;
_Input();
}
public void AddItem(Item newItem, GameObject newObject)
{
equippedObject = newObject;
inventoryItems.Add(newItem);
inventoryObjects.Add(newObject);
for (int i = 0; i < slotList.Count; i++)
{
if (slotList[i].transform.childCount == 0)
{
Instantiate(newItem.inventoryPrefab, slotList[i].transform.position, transform.rotation, slotList[i].transform);
break;
}
}
}
public void RemoveItem(Item item, GameObject _object)
{
if (inventoryItems.Contains(item) && inventoryObjects.Contains(_object))
{
for (int i = 0; i < slotList.Count; i++)
{
if (slotList[i].transform.childCount > 0)
{
Destroy(slotList[i].transform.GetChild(0).gameObject);
break;
}
}
int nextIndex = (heldItemIndex + 1) % inventoryItems.Count;
if (inventoryItems[nextIndex] != emptyObjectTemplate)
{
if (nextIndex < 4 && nextIndex > -1)
{
heldItemIndex = (heldItemIndex + 1) % inventoryItems.Count;
equippedObject = inventoryObjects[heldItemIndex];
Spin(90f, false);
}
}
inventoryItems.Remove(item);
inventoryObjects.Remove(_object);
if (inventoryObjects.Count > 0)
{
heldItemIndex = Mathf.Clamp(heldItemIndex, 0, inventoryObjects.Count - 1);
equippedObject = inventoryObjects[heldItemIndex];
equippedObject.SetActive(true);
}
else
{
equippedObject = null;
}
}
}
private void _Input()
{
var inventoryUI = GetComponentInChildren<InventoryUI>();
if (inventoryUI.isOpen == true && canSpin)
{
if (inventoryObjects.Count > 1)
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (inventoryFull)
{
int nextIndex = (heldItemIndex - 1 + inventoryItems.Count) % inventoryItems.Count;
if (inventoryItems[nextIndex] != emptyObjectTemplate)
{
if (nextIndex < 4 && nextIndex > -1)
{
heldItemIndex = (heldItemIndex - 1 + inventoryItems.Count) % inventoryItems.Count;
equippedObject = inventoryObjects[heldItemIndex];
Spin(-90f, true);
}
}
}
else if (heldItemIndex > 0 && !inventoryFull)
{
int nextIndex = (heldItemIndex - 1 + inventoryItems.Count) % inventoryItems.Count;
if (inventoryItems[nextIndex] != emptyObjectTemplate)
{
if (nextIndex < 4 && nextIndex > -1)
{
heldItemIndex = (heldItemIndex - 1 + inventoryItems.Count) % inventoryItems.Count;
equippedObject = inventoryObjects[heldItemIndex];
Spin(-90f, true);
}
}
}
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (inventoryFull)
{
int nextIndex = (heldItemIndex + 1) % inventoryItems.Count;
if (inventoryItems[nextIndex] != emptyObjectTemplate)
{
if (nextIndex < 4 && nextIndex > -1)
{
heldItemIndex = (heldItemIndex + 1) % inventoryItems.Count;
equippedObject = inventoryObjects[heldItemIndex];
Spin(90f, true);
}
}
}
else if (heldItemIndex < inventoryItems.Count - 1 && !inventoryFull)
{
int nextIndex = (heldItemIndex + 1) % inventoryItems.Count;
if (inventoryItems[nextIndex] != emptyObjectTemplate)
{
if (nextIndex < 4 && nextIndex > -1)
{
heldItemIndex = (heldItemIndex + 1) % inventoryItems.Count;
equippedObject = inventoryObjects[heldItemIndex];
Spin(90f, true);
}
}
}
}
}
}
}
public void Spin(float direction, bool withSound)
{
GetComponentInChildren<InventoryUI>().SwitchSound();
canSpin = false;
spinOrigin.DORotate(new Vector3(direction, 0, 0), spinSpeed, RotateMode.LocalAxisAdd).SetEase(Ease.OutCubic).OnComplete(() => SpinDone());
}
private void SpinDone()
{
canSpin = true;
}
}Editor is loading...
Leave a Comment