Advertisement
evelynshilosky

CraftingSystem - Part 33

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