Advertisement
evelynshilosky

CraftingSystem - Tutorial Part 17

Oct 20th, 2023 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.13 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, survivalScreenUI, refineScreenUI, constructionScreenUI;
  11.  
  12.     public List<string> inventoryItemList = new List<string>();
  13.  
  14.     //Category Buttons
  15.     Button toolsBTN, survivalBTN, refineBTN, constructionBTN;
  16.  
  17.     //Craft Buttons
  18.     Button craftAxeBTN, craftPlankBTN, craftStickBTN, craftFoundationBTN, craftWallBTN;
  19.  
  20.     //Requirement Text
  21.     Text AxeReq1, AxeReq2, PlankReq1, StickReq1, FoundationReq1, WallReq1;
  22.  
  23.     public bool isOpen;
  24.  
  25.     //All Blueprints
  26.     public Blueprint AxeBLP = new Blueprint("Axe", 1, 2, "Stone", 2, "Stick", 1);
  27.     public Blueprint PlankBLP = new Blueprint("Plank", 2, 1, "Log", 1, "", 0);
  28.     public Blueprint StickBLP = new Blueprint("Stick", 1, 1, "Tree Branch", 1, "", 0);
  29.     public Blueprint FoundationBLP = new Blueprint("Foundation", 1, 1, "Plank", 4, "", 0);
  30.     public Blueprint WallBLP = new Blueprint("Wall", 1, 1, "Plank", 5, "", 0);
  31.  
  32.     public static CraftingSystem Instance { get; set; }
  33.  
  34.  
  35.     private void Awake()
  36.     {
  37.         if (Instance != null && Instance != this)
  38.         {
  39.             Destroy(gameObject);
  40.         }
  41.         else
  42.         {
  43.             Instance = this;
  44.         }
  45.     }
  46.  
  47.  
  48.     // Start is called before the first frame update
  49.     void Start()
  50.     {
  51.  
  52.         isOpen = false;
  53.  
  54.         toolsBTN = craftingScreenUI.transform.Find("ToolsButton").GetComponent<Button>();
  55.         toolsBTN.onClick.AddListener(delegate { OpenToolsCategory(); });
  56.  
  57.         survivalBTN = craftingScreenUI.transform.Find("SurvivalButton").GetComponent<Button>();
  58.         survivalBTN.onClick.AddListener(delegate { OpenSurvivalCategory(); });
  59.  
  60.         refineBTN = craftingScreenUI.transform.Find("RefineButton").GetComponent<Button>();
  61.         refineBTN.onClick.AddListener(delegate { OpenRefineCategory(); });
  62.  
  63.         constructionBTN = craftingScreenUI.transform.Find("ConstructionButton").GetComponent<Button>();
  64.         constructionBTN.onClick.AddListener(delegate { OpenConstructionCategory(); });
  65.  
  66.         // AXE
  67.         AxeReq1 = toolsScreenUI.transform.Find("Axe").transform.Find("req1").GetComponent<Text>();
  68.         AxeReq2 = toolsScreenUI.transform.Find("Axe").transform.Find("req2").GetComponent<Text>();
  69.  
  70.         craftAxeBTN = toolsScreenUI.transform.Find("Axe").transform.Find("Button").GetComponent<Button>();
  71.         craftAxeBTN.onClick.AddListener(delegate { CraftAnyItem(AxeBLP); });
  72.  
  73.         // Plank
  74.         PlankReq1 = refineScreenUI.transform.Find("Plank").transform.Find("req1").GetComponent<Text>();
  75.  
  76.         craftPlankBTN = refineScreenUI.transform.Find("Plank").transform.Find("Button").GetComponent<Button>();
  77.         craftPlankBTN.onClick.AddListener(delegate { CraftAnyItem(PlankBLP); });
  78.  
  79.         // Stick
  80.         StickReq1 = refineScreenUI.transform.Find("Stick").transform.Find("req1").GetComponent<Text>();
  81.  
  82.         craftStickBTN = refineScreenUI.transform.Find("Stick").transform.Find("Button").GetComponent<Button>();
  83.         craftStickBTN.onClick.AddListener(delegate { CraftAnyItem(StickBLP); });
  84.  
  85.         // Foundation
  86.         FoundationReq1 = constructionScreenUI.transform.Find("Foundation").transform.Find("req1").GetComponent<Text>();
  87.  
  88.         craftFoundationBTN = constructionScreenUI.transform.Find("Foundation").transform.Find("Button").GetComponent<Button>();
  89.         craftFoundationBTN.onClick.AddListener(delegate { CraftAnyItem(FoundationBLP); });
  90.  
  91.         // Wall
  92.         WallReq1 = constructionScreenUI.transform.Find("Wall").transform.Find("req1").GetComponent<Text>();
  93.  
  94.         craftWallBTN = constructionScreenUI.transform.Find("Wall").transform.Find("Button").GetComponent<Button>();
  95.         craftWallBTN.onClick.AddListener(delegate { CraftAnyItem(WallBLP); });
  96.     }
  97.     void OpenToolsCategory()
  98.     {
  99.         craftingScreenUI.SetActive(false);
  100.         survivalScreenUI.SetActive(false);
  101.         refineScreenUI.SetActive(false);
  102.         constructionScreenUI.SetActive(false);
  103.  
  104.         toolsScreenUI.SetActive(true);
  105.     }
  106.     void OpenSurvivalCategory()
  107.     {
  108.         craftingScreenUI.SetActive(false);
  109.         toolsScreenUI.SetActive(false);
  110.         refineScreenUI.SetActive(false);
  111.  
  112.         survivalScreenUI.SetActive(true);
  113.         constructionScreenUI.SetActive(false);
  114.     }
  115.     void OpenRefineCategory()
  116.     {
  117.         craftingScreenUI.SetActive(false);
  118.         toolsScreenUI.SetActive(false);
  119.         survivalScreenUI.SetActive(false);
  120.         constructionScreenUI.SetActive(false);
  121.  
  122.         refineScreenUI.SetActive(true);
  123.     }
  124.  
  125.     void OpenConstructionCategory()
  126.     {
  127.         craftingScreenUI.SetActive(false);
  128.         toolsScreenUI.SetActive(false);
  129.         survivalScreenUI.SetActive(false);
  130.         refineScreenUI.SetActive(false);
  131.  
  132.         constructionScreenUI.SetActive(true);
  133.     }
  134.     void CraftAnyItem(Blueprint blueprintToCraft)
  135.     {
  136.  
  137.         SoundManager.Instance.PlaySound(SoundManager.Instance.craftingSound);
  138.  
  139.         StartCoroutine(craftedDelayForSound(blueprintToCraft));
  140.  
  141.         if (blueprintToCraft.numOfRequirements == 1) //remove resources from inventory
  142.         {
  143.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  144.         }
  145.         else if (blueprintToCraft.numOfRequirements == 2)
  146.         {
  147.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  148.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
  149.         }
  150.         else if (blueprintToCraft.numOfRequirements == 3) //remove resources from inventory
  151.         {
  152.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  153.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
  154.         }
  155.         else if (blueprintToCraft.numOfRequirements == 4) //remove resources from inventory
  156.         {
  157.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  158.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
  159.         }
  160.         else if (blueprintToCraft.numOfRequirements == 5) //remove resources from inventory
  161.         {
  162.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
  163.             InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
  164.         }
  165.         StartCoroutine(calculate()); // refresh list
  166.     }
  167.  
  168.     public IEnumerator calculate()
  169.     {
  170.         yield return 0;        // yield return new WaitForSeconds(1f); //To wait to do something
  171.         InventorySystem.Instance.ReCalculateList();
  172.         RefreshNeededItems();
  173.     }
  174.  
  175.     IEnumerator craftedDelayForSound(Blueprint blueprintToCraft)
  176.     {
  177.  
  178.         yield return new WaitForSeconds(10f); //changed time due to my audio clip
  179.         SoundManager.Instance.craftingSound.Stop(); //stops the audio clip from continuing because it was too long
  180.         // Produce the amount of items according to the blueprint
  181.         for (var i = 0; i < blueprintToCraft.numberOfItemsToProduce; i++)
  182.         {
  183.             InventorySystem.Instance.AddToInventory(blueprintToCraft.itemName); //add item into inventory
  184.         }
  185.     }
  186.  
  187.  
  188.  
  189. void Update()
  190.     {
  191.  
  192.  
  193.  
  194.         if (Input.GetKeyDown(KeyCode.C) && !isOpen)
  195.         {
  196.  
  197.             craftingScreenUI.SetActive(true);
  198.             Cursor.lockState = CursorLockMode.None;
  199.             Cursor.visible = true;
  200.  
  201.             SelectionManager.Instance.DisableSelection();
  202.             SelectionManager.Instance.GetComponent<SelectionManager>().enabled = false;
  203.  
  204.             isOpen = true;
  205.  
  206.         }
  207.         else if (Input.GetKeyDown(KeyCode.C) && isOpen)
  208.         {
  209.             craftingScreenUI.SetActive(false);
  210.             toolsScreenUI.SetActive(false);
  211.             survivalScreenUI.SetActive(false);
  212.             refineScreenUI.SetActive(false);
  213.             constructionScreenUI.SetActive(false);
  214.  
  215.  
  216.             if (!InventorySystem.Instance.isOpen)
  217.             {
  218.                 Cursor.lockState = CursorLockMode.Locked;
  219.                 Cursor.visible = false;
  220.  
  221.                 SelectionManager.Instance.EnableSelection();
  222.                 SelectionManager.Instance.GetComponent<SelectionManager>().enabled = true;
  223.             }
  224.  
  225.  
  226.  
  227.             isOpen = false;
  228.         }
  229.        
  230.     }
  231.  
  232.  
  233.  
  234.     public void RefreshNeededItems()
  235.     {
  236.  
  237.         int stone_count = 0;
  238.         int stick_count = 0;
  239.         int log_count = 0;
  240.         int treeBranch_count = 0;
  241.         int plank_count = 0;
  242.  
  243.         inventoryItemList = InventorySystem.Instance.itemList;
  244.  
  245.         foreach (string itemName in inventoryItemList)
  246.         {
  247.  
  248.             switch (itemName)
  249.             {
  250.                 case "Stone":
  251.                     stone_count++;
  252.                     break;
  253.                 case "Stick":
  254.                     stick_count++;
  255.                     break;
  256.                 case "Log":
  257.                     log_count++;
  258.                     break;
  259.                 case "Tree Branch":
  260.                     treeBranch_count++;
  261.                     break;
  262.                 case "Plank":
  263.                     plank_count++;
  264.                     break;
  265.             }
  266.         }
  267.  
  268.  
  269.  
  270.         // Axe
  271.  
  272.         AxeReq1.text = "Stone [" + stone_count + "/2]";
  273.         AxeReq2.text = "Stick [" + stick_count + "/1]";
  274.  
  275.         if (stone_count >= 2 && stick_count >=1 && InventorySystem.Instance.CheckSlotsAvailable(1))
  276.         {
  277.  
  278.             craftAxeBTN.gameObject.SetActive(true);
  279.         }
  280.         else
  281.         {
  282.             craftAxeBTN.gameObject.SetActive(false);
  283.         }
  284.  
  285.  
  286.         // Plank
  287.  
  288.         PlankReq1.text = "Log [" + log_count + "/1]";
  289.  
  290.         if (log_count >= 1 && InventorySystem.Instance.CheckSlotsAvailable(2))
  291.         {
  292.             craftPlankBTN.gameObject.SetActive(true);
  293.         }
  294.         else
  295.         {
  296.             craftPlankBTN.gameObject.SetActive(false);
  297.         }
  298.  
  299.         // Stick
  300.  
  301.         StickReq1.text = "Branch [" + treeBranch_count + "/1]";
  302.  
  303.         if (treeBranch_count >= 1 && InventorySystem.Instance.CheckSlotsAvailable(1))
  304.         {
  305.             craftStickBTN.gameObject.SetActive(true);
  306.         }
  307.         else
  308.         {
  309.             craftStickBTN.gameObject.SetActive(false);
  310.         }
  311.  
  312.         // Foundation
  313.  
  314.         FoundationReq1.text = "Plank [" + plank_count + "/4]";
  315.  
  316.         if (plank_count >= 4 && InventorySystem.Instance.CheckSlotsAvailable(1))
  317.         {
  318.             craftFoundationBTN.gameObject.SetActive(true);
  319.         }
  320.         else
  321.         {
  322.             craftFoundationBTN.gameObject.SetActive(false);
  323.         }
  324.  
  325.         // Wall
  326.  
  327.         WallReq1.text = "Plank [" + plank_count + "/5]";
  328.  
  329.         if (plank_count >= 5 && InventorySystem.Instance.CheckSlotsAvailable(1))
  330.         {
  331.             craftWallBTN.gameObject.SetActive(true);
  332.         }
  333.         else
  334.         {
  335.             craftWallBTN.gameObject.SetActive(false);
  336.         }
  337.  
  338.     }
  339.  
  340.  
  341.  
  342.  
  343. }
  344.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement