Advertisement
evelynshilosky

CraftingSystem - Part 19

Oct 20th, 2023 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.14 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.  
  174. void Update()
  175.     {
  176.  
  177.         if (Input.GetKeyDown(KeyCode.C) && !isOpen && !ConstructionManager.Instance.inConstructionMode)
  178.         {
  179.  
  180.             craftingScreenUI.SetActive(true);
  181.             Cursor.lockState = CursorLockMode.None;
  182.             Cursor.visible = true;
  183.  
  184.             SelectionManager.Instance.DisableSelection();
  185.             SelectionManager.Instance.GetComponent<SelectionManager>().enabled = false;
  186.  
  187.             isOpen = true;
  188.  
  189.         }
  190.         else if (Input.GetKeyDown(KeyCode.C) && isOpen)
  191.         {
  192.             craftingScreenUI.SetActive(false);
  193.             toolsScreenUI.SetActive(false);
  194.             survivalScreenUI.SetActive(false);
  195.             refineScreenUI.SetActive(false);
  196.             constructionScreenUI.SetActive(false);
  197.  
  198.  
  199.             if (!InventorySystem.Instance.isOpen)
  200.             {
  201.                 Cursor.lockState = CursorLockMode.Locked;
  202.                 Cursor.visible = false;
  203.  
  204.                 SelectionManager.Instance.EnableSelection();
  205.                 SelectionManager.Instance.GetComponent<SelectionManager>().enabled = true;
  206.             }
  207.  
  208.  
  209.  
  210.             isOpen = false;
  211.         }
  212.        
  213.     }
  214.  
  215.  
  216.  
  217.     public void RefreshNeededItems()
  218.     {
  219.  
  220.         int stone_count = 0;
  221.         int stick_count = 0;
  222.         int log_count = 0;
  223.         int treeBranch_count = 0;
  224.         int plank_count = 0;
  225.  
  226.         inventoryItemList = InventorySystem.Instance.itemList;
  227.  
  228.         foreach (string itemName in inventoryItemList)
  229.         {
  230.  
  231.             switch (itemName)
  232.             {
  233.                 case "Stone":
  234.                     stone_count++;
  235.                     break;
  236.                 case "Stick":
  237.                     stick_count++;
  238.                     break;
  239.                 case "Log":
  240.                     log_count++;
  241.                     break;
  242.                 case "Tree Branch":
  243.                     treeBranch_count++;
  244.                     break;
  245.                 case "Plank":
  246.                     plank_count++;
  247.                     break;
  248.             }
  249.         }
  250.  
  251.  
  252.  
  253.         // Axe
  254.  
  255.         AxeReq1.text = "Stone [" + stone_count + "/2]";
  256.         AxeReq2.text = "Stick [" + stick_count + "/1]";
  257.  
  258.         if (stone_count >= 2 && stick_count >=1 && InventorySystem.Instance.CheckSlotsAvailable(1))
  259.         {
  260.  
  261.             craftAxeBTN.gameObject.SetActive(true);
  262.         }
  263.         else
  264.         {
  265.             craftAxeBTN.gameObject.SetActive(false);
  266.         }
  267.  
  268.  
  269.         // Plank
  270.  
  271.         PlankReq1.text = "Log [" + log_count + "/1]";
  272.  
  273.         if (log_count >= 1 && InventorySystem.Instance.CheckSlotsAvailable(2))
  274.         {
  275.             craftPlankBTN.gameObject.SetActive(true);
  276.         }
  277.         else
  278.         {
  279.             craftPlankBTN.gameObject.SetActive(false);
  280.         }
  281.  
  282.         // Stick
  283.  
  284.         StickReq1.text = "Branch [" + treeBranch_count + "/1]";
  285.  
  286.         if (treeBranch_count >= 1 && InventorySystem.Instance.CheckSlotsAvailable(1))
  287.         {
  288.             craftStickBTN.gameObject.SetActive(true);
  289.         }
  290.         else
  291.         {
  292.             craftStickBTN.gameObject.SetActive(false);
  293.         }
  294.  
  295.         // Foundation
  296.  
  297.         FoundationReq1.text = "Plank [" + plank_count + "/4]";
  298.  
  299.         if (plank_count >= 4 && InventorySystem.Instance.CheckSlotsAvailable(1))
  300.         {
  301.             craftFoundationBTN.gameObject.SetActive(true);
  302.         }
  303.         else
  304.         {
  305.             craftFoundationBTN.gameObject.SetActive(false);
  306.         }
  307.  
  308.         // Wall
  309.  
  310.         WallReq1.text = "Plank [" + plank_count + "/5]";
  311.  
  312.         if (plank_count >= 5 && InventorySystem.Instance.CheckSlotsAvailable(1))
  313.         {
  314.             craftWallBTN.gameObject.SetActive(true);
  315.         }
  316.         else
  317.         {
  318.             craftWallBTN.gameObject.SetActive(false);
  319.         }
  320.  
  321.     }
  322.  
  323.  
  324.  
  325.  
  326. }
  327.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement