evelynshilosky

CraftingSystem - Part 31

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