Advertisement
evelynshilosky

CraftingSystem - Part 8

Jun 2nd, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 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.  
  9.     public GameObject craftingScreenUI;
  10.     public GameObject toolsScreenUI;
  11.  
  12.     public List<string> inventoryItemList = new List<string> ();
  13.  
  14.     //Category Buttons
  15.     Button toolsBTN;
  16.  
  17.     //Craft Buttons
  18.     Button craftAxeBTN;
  19.  
  20.     //Requirement Text
  21.     Text AxeReq1, AxeReq2;
  22.  
  23.     public bool isOpen;
  24.  
  25.     //All Blueprints
  26.     public Blueprint AxeBLP = new Blueprint ("Axe", 2, "Stone", 2, "Stick", 1);
  27.  
  28.  
  29.     public static CraftingSystem Instance { get; set; }
  30.  
  31.  
  32.     private void Awake()
  33.     {
  34.         if (Instance !=null && Instance !=this)
  35.         {
  36.             Destroy(gameObject);
  37.         }
  38.         else
  39.         {
  40.             Instance = this;
  41.         }
  42.     }
  43.  
  44.  
  45.     // Start is called before the first frame update
  46.     void Start()
  47.     {
  48.  
  49.         isOpen = false;
  50.  
  51.         toolsBTN = craftingScreenUI.transform.Find("ToolsButton").GetComponent<Button> ();
  52.         toolsBTN.onClick.AddListener(delegate { OpenToolsCategory(); });
  53.  
  54.         // AXE
  55.         AxeReq1 = toolsScreenUI.transform.Find("Axe").transform.Find("req1").GetComponent<Text>();
  56.         AxeReq2 = toolsScreenUI.transform.Find("Axe").transform.Find("req2").GetComponent<Text>();
  57.  
  58.         craftAxeBTN = toolsScreenUI.transform.Find("Axe").transform.Find("Button").GetComponent<Button>();
  59.         craftAxeBTN.onClick.AddListener(delegate { CraftAnyItem(AxeBLP); });
  60.  
  61.     }
  62.  
  63.    
  64.     void OpenToolsCategory()
  65.     {
  66.         craftingScreenUI.SetActive (false);
  67.         toolsScreenUI.SetActive (true);
  68.     }
  69.  
  70.  
  71.     void CraftAnyItem(Blueprint blueprintToCraft)
  72.     {
  73.  
  74.         //add item into inventory
  75.         InventorySystem.Instance.AddToInventory(blueprintToCraft.itemName);
  76.  
  77.         //remove resources from inventory
  78.         if (blueprintToCraft.numOfRequirements == 1)
  79.         {
  80.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  81.         }
  82.         else if (blueprintToCraft.numOfRequirements == 2)
  83.         {
  84.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  85.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
  86.         }
  87.  
  88.  
  89.         // refresh list
  90.         StartCoroutine(calculate());
  91.  
  92.         RefreshNeededItems();
  93.  
  94.     }
  95.  
  96.     public IEnumerator calculate()
  97.     {
  98.         yield return new WaitForSeconds(1f);
  99.  
  100.         InventorySystem.Instance.ReCalculateList();
  101.     }
  102.  
  103.     // Update is called once per frame
  104.     void Update()
  105.     {
  106.  
  107.         RefreshNeededItems();
  108.  
  109.  
  110.         if (Input.GetKeyDown(KeyCode.C) && !isOpen)
  111.         {
  112.  
  113.             craftingScreenUI.SetActive(true);
  114.             Cursor.lockState = CursorLockMode.None;
  115.             isOpen = true;
  116.  
  117.         }
  118.         else if (Input.GetKeyDown(KeyCode.C) && isOpen)
  119.         {
  120.             craftingScreenUI.SetActive(false);
  121.             toolsScreenUI.SetActive(false);
  122.  
  123.  
  124.             if (!InventorySystem.Instance.isOpen) {
  125.                 Cursor.lockState = CursorLockMode.Locked;
  126.             }
  127.  
  128.  
  129.  
  130.             isOpen = false;
  131.         }
  132.        
  133.     }
  134.  
  135.  
  136.  
  137.     private void RefreshNeededItems()
  138.     {
  139.  
  140.         int stone_count = 0;
  141.         int stick_count = 0;
  142.  
  143.         inventoryItemList = InventorySystem.Instance.itemList;
  144.  
  145.         foreach (string itemName in inventoryItemList)
  146.         {
  147.  
  148.             switch (itemName)
  149.             {
  150.                 case "Stone":
  151.                     stone_count++;
  152.                     break;
  153.                 case "Stick":
  154.                     stick_count++;
  155.                     break;
  156.             }
  157.         }
  158.  
  159.  
  160.  
  161.         // Axe
  162.  
  163.         AxeReq1.text = "2 Stone [" + stone_count + "/2]";
  164.         AxeReq2.text = "1 Stick [" + stick_count + "/1]";
  165.  
  166.         if (stone_count >= 2 && stick_count >=1)
  167.         {
  168.  
  169.             craftAxeBTN.gameObject.SetActive(true);
  170.         }
  171.         else
  172.         {
  173.             craftAxeBTN.gameObject.SetActive(false);
  174.         }
  175.  
  176.  
  177.     }
  178.  
  179.  
  180.  
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement