3DSurvivalGame / Assets /Scripts /InventorySystem.cs
doc2txt's picture
1
3497d64
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventorySystem : MonoBehaviour
{
public GameObject ItemInfoUI;
public static InventorySystem Instance { get; set; }
public GameObject inventoryScreenUI;
public bool isOpen;
public bool isFull;
public List<GameObject> slotList = new List<GameObject>();
public List<string> itemList = new List<string>();
private GameObject itemToAdd;
private GameObject whatSlotToEquip;
public GameObject pickupAlert;
public Text pickupName;
public Image pickupImage;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}
void Start()
{
isOpen = false;
isFull = false;
PopulateSlotList();
Cursor.visible = false;
}
private void PopulateSlotList()
{
foreach (Transform child in inventoryScreenUI.transform)
{
if (child.CompareTag("Slot"))
{
slotList.Add(child.gameObject);
}
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.I) && !isOpen)
{
inventoryScreenUI.SetActive(true);
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
SelectionManager.Instance.DisableSelection();
SelectionManager.Instance.GetComponent<SelectionManager>().enabled = false;
isOpen = true;
}
else if (Input.GetKeyDown(KeyCode.I) && isOpen)
{
inventoryScreenUI.SetActive(false);
isOpen = false;
if (!CraftingSystem.Instance.isOpen)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Cursor.visible = false;
SelectionManager.Instance.EnableSelection();
SelectionManager.Instance.GetComponent<SelectionManager>().enabled = true;
}
}
}
public void AddToInventory(string itemName)
{
SoundManager.Instance.PlaySound(SoundManager.Instance.dropItemSound);
whatSlotToEquip = FindNextEmptySlot();
itemToAdd = Instantiate(Resources.Load<GameObject>(itemName), whatSlotToEquip.transform.position, whatSlotToEquip.transform.rotation);
itemToAdd.transform.SetParent(whatSlotToEquip.transform);
itemList.Add(itemName);
TriggerPickupPopup(itemName, itemToAdd.GetComponent<Image>().sprite);
ReCalculateList();
CraftingSystem.Instance.RefreshNeededItems();
}
void TriggerPickupPopup(string itemName, Sprite itemSprite)
{
pickupAlert.SetActive(true);
pickupName.text = itemName;
pickupImage.sprite = itemSprite;
StartCoroutine(closePopup());
}
IEnumerator closePopup()
{
yield return new WaitForSeconds(5);
pickupAlert.SetActive(false);
}
private GameObject FindNextEmptySlot()
{
foreach (GameObject slot in slotList)
{
if (slot.transform.childCount == 0)
{
return slot;
}
}
return new GameObject();
}
public bool CheckSlotsAvailable(int emptyNeeded)
{
int fullSlots = 0;
foreach (GameObject slot in slotList)
{
if (slot.transform.childCount > 0)
{
fullSlots++;
}
}
return fullSlots < slotList.Count - emptyNeeded;
}
public void RemoveItem(string nameToRemove, int amountToRemove)
{
int counter = amountToRemove;
for (var i = slotList.Count - 1; i >= 0; i--)
// foreach (GameObject slot in slotList)
{
if (slotList[i].transform.childCount > 0)
{
if (slotList[i].transform.GetChild(0).name == nameToRemove + "(Clone)" && counter != 0)
{
DestroyImmediate(slotList[i].transform.GetChild(0).gameObject);
counter--;
}
}
}
ReCalculateList();
CraftingSystem.Instance.RefreshNeededItems();
}
public void ReCalculateList()
{
itemList.Clear();
foreach (GameObject slot in slotList)
{
if (slot.transform.childCount > 0)
{
string name = slot.transform.GetChild(0).name;
itemList.Add(name.Replace("(Clone)", ""));
}
}
}
}