Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class InventorySystem : MonoBehaviour
- {
- public static InventorySystem Instance { get; set; }
- public GameObject inventoryScreenUI;
- public List<GameObject> slotList = new List<GameObject>();
- public List<string> itemList = new List<string>();
- private GameObject itemToAdd;
- private GameObject whatSlotToEquip;
- public bool isOpen;
- //public bool isFull;
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- }
- else
- {
- Instance = this;
- }
- }
- void Start()
- {
- isOpen = false;
- PopulateSlotList();
- }
- 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;
- isOpen = true;
- }
- else if (Input.GetKeyDown(KeyCode.I) && isOpen)
- {
- inventoryScreenUI.SetActive(false);
- if (!CraftingSystem.Instance.isOpen) {
- Cursor.lockState = CursorLockMode.Locked;
- }
- isOpen = false;
- }
- }
- public void AddToInventory(string itemName)
- {
- whatSlotToEquip = FindNextEmptySlot();
- itemToAdd = Instantiate(Resources.Load<GameObject>(itemName), whatSlotToEquip.transform.position, whatSlotToEquip.transform.rotation);
- itemToAdd.transform.SetParent(whatSlotToEquip.transform);
- itemList.Add(itemName);
- }
- private GameObject FindNextEmptySlot()
- {
- foreach(GameObject slot in slotList)
- {
- if (slot.transform.childCount == 0)
- {
- return slot;
- }
- }
- return new GameObject();
- }
- public bool CheckIfFull()
- {
- int counter = 0;
- foreach (GameObject slot in slotList)
- {
- if (slot.transform.childCount > 0)
- {
- counter++;
- }
- }
- if (counter == 21)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public void RemoveItem(string nameToRemove, int amountToRemove)
- {
- int counter = amountToRemove;
- for (var i = slotList.Count - 1; i>=0; i--)
- {
- if (slotList[i].transform.childCount>0)
- {
- if (slotList[i].transform.GetChild(0).name == nameToRemove+"(Clone)" && counter!=0)
- {
- Destroy(slotList[i].transform.GetChild(0).gameObject);
- counter--;
- }
- }
- }
- }
- public void ReCalculateList()
- {
- itemList.Clear();
- foreach (GameObject slot in slotList)
- {
- if (slot.transform.childCount>0)
- {
- string name = slot.transform.GetChild(0).name; //Stone (Clone)
- string str2 = "(Clone)";
- string result = name.Replace(str2, "");
- itemList.Add(result); //Stone
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement