Advertisement
evelynshilosky

CraftingSystem - Part 9

Jun 3rd, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class CraftingSystem : MonoBehaviour
  7. {
  8.     public GameObject craftingScreenUI;
  9.     public GameObject toolsScreenUI;
  10.     public List<string> inventoryItemList = new List<string> ();
  11.     //Category Buttons
  12.     Button toolsBTN;
  13.     //Craft Buttons
  14.     Button craftAxeBTN;
  15.     //Requirement Text
  16.     Text AxeReq1, AxeReq2;
  17.     public bool isOpen;
  18.     //All Blueprints
  19.     public Blueprint AxeBLP = new Blueprint ("Axe", 2, "Stone", 2, "Stick", 1);
  20.     public static CraftingSystem Instance { get; set; }
  21.     private void Awake()
  22.     {
  23.         if (Instance !=null && Instance !=this)
  24.         {
  25.             Destroy(gameObject);
  26.         }
  27.         else
  28.         {
  29.             Instance = this;
  30.         }
  31.     }
  32.     // Start is called before the first frame update
  33.     void Start()
  34.     {
  35.         isOpen = false;
  36.         toolsBTN = craftingScreenUI.transform.Find("ToolsButton").GetComponent<Button> ();
  37.         toolsBTN.onClick.AddListener(delegate { OpenToolsCategory(); });
  38.         // AXE
  39.         AxeReq1 = toolsScreenUI.transform.Find("Axe").transform.Find("req1").GetComponent<Text>();
  40.         AxeReq2 = toolsScreenUI.transform.Find("Axe").transform.Find("req2").GetComponent<Text>();
  41.         craftAxeBTN = toolsScreenUI.transform.Find("Axe").transform.Find("Button").GetComponent<Button>();
  42.         craftAxeBTN.onClick.AddListener(delegate { CraftAnyItem(AxeBLP); });
  43.     }  
  44.     void OpenToolsCategory()
  45.     {
  46.         craftingScreenUI.SetActive (false);
  47.         toolsScreenUI.SetActive (true);
  48.     }
  49.     void CraftAnyItem(Blueprint blueprintToCraft)
  50.     {
  51.         //add item into inventory
  52.         InventorySystem.Instance.AddToInventory(blueprintToCraft.itemName);
  53.         //remove resources from inventory
  54.         if (blueprintToCraft.numOfRequirements == 1)
  55.         {
  56.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  57.         }
  58.         else if (blueprintToCraft.numOfRequirements == 2)
  59.         {
  60.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  61.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
  62.         }
  63.         // refresh list
  64.         StartCoroutine(calculate());
  65.     }
  66.     public IEnumerator calculate()
  67.     {
  68.         yield return 0;        // yield return new WaitForSeconds(1f); //To wait to do something
  69.         InventorySystem.Instance.ReCalculateList();
  70.         RefreshNeededItems();
  71.     }
  72.     // Update is called once per frame
  73.     void Update()
  74.     {
  75.        if (Input.GetKeyDown(KeyCode.C) && !isOpen)
  76.         {
  77.             craftingScreenUI.SetActive(true);
  78.             Cursor.lockState = CursorLockMode.None;
  79.             isOpen = true;
  80.         }
  81.         else if (Input.GetKeyDown(KeyCode.C) && isOpen)
  82.         {
  83.             craftingScreenUI.SetActive(false);
  84.             toolsScreenUI.SetActive(false);
  85.  
  86.             if (!InventorySystem.Instance.isOpen) {
  87.                 Cursor.lockState = CursorLockMode.Locked;
  88.             }
  89.             isOpen = false;
  90.         }      
  91.     }
  92.     public void RefreshNeededItems()
  93.     {
  94.         int stone_count = 0;
  95.         int stick_count = 0;
  96.         inventoryItemList = InventorySystem.Instance.itemList;
  97.         foreach (string itemName in inventoryItemList)
  98.         {
  99.             switch (itemName)
  100.             {
  101.                 case "Stone":
  102.                     stone_count++;
  103.                     break;
  104.                 case "Stick":
  105.                     stick_count++;
  106.                     break;
  107.             }
  108.         }
  109.         // Axe
  110.         AxeReq1.text = "2 Stone [" + stone_count + "/2]";
  111.         AxeReq2.text = "1 Stick [" + stick_count + "/1]";
  112.  
  113.         if (stone_count >= 2 && stick_count >=1)
  114.         {
  115.             craftAxeBTN.gameObject.SetActive(true);
  116.         }
  117.         else
  118.         {
  119.             craftAxeBTN.gameObject.SetActive(false);
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement