Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class CraftingSystem : MonoBehaviour
- {
- public GameObject craftingScreenUI;
- public GameObject toolsScreenUI, survivalScreenUI, refineScreenUI, constructionScreenUI;
- public List<string> inventoryItemList = new List<string>();
- //Category Buttons
- Button toolsBTN, survivalBTN, refineBTN, constructionBTN;
- //Craft Buttons
- Button craftAxeBTN, craftPlankBTN, craftStickBTN, craftFoundationBTN, craftWallBTN;
- //Requirement Text
- Text AxeReq1, AxeReq2, PlankReq1, StickReq1, FoundationReq1, WallReq1;
- public bool isOpen;
- //All Blueprints
- public Blueprint AxeBLP = new Blueprint("Axe", 1, 2, "Stone", 2, "Stick", 1);
- public Blueprint PlankBLP = new Blueprint("Plank", 2, 1, "Log", 1, "", 0);
- public Blueprint StickBLP = new Blueprint("Stick", 1, 1, "Tree Branch", 1, "", 0);
- public Blueprint FoundationBLP = new Blueprint("Foundation", 1, 1, "Plank", 4, "", 0);
- public Blueprint WallBLP = new Blueprint("Wall", 1, 1, "Plank", 5, "", 0);
- public static CraftingSystem Instance { get; set; }
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- }
- else
- {
- Instance = this;
- }
- }
- // Start is called before the first frame update
- void Start()
- {
- isOpen = false;
- toolsBTN = craftingScreenUI.transform.Find("ToolsButton").GetComponent<Button>();
- toolsBTN.onClick.AddListener(delegate { OpenToolsCategory(); });
- survivalBTN = craftingScreenUI.transform.Find("SurvivalButton").GetComponent<Button>();
- survivalBTN.onClick.AddListener(delegate { OpenSurvivalCategory(); });
- refineBTN = craftingScreenUI.transform.Find("RefineButton").GetComponent<Button>();
- refineBTN.onClick.AddListener(delegate { OpenRefineCategory(); });
- constructionBTN = craftingScreenUI.transform.Find("ConstructionButton").GetComponent<Button>();
- constructionBTN.onClick.AddListener(delegate { OpenConstructionCategory(); });
- // AXE
- AxeReq1 = toolsScreenUI.transform.Find("Axe").transform.Find("req1").GetComponent<Text>();
- AxeReq2 = toolsScreenUI.transform.Find("Axe").transform.Find("req2").GetComponent<Text>();
- craftAxeBTN = toolsScreenUI.transform.Find("Axe").transform.Find("Button").GetComponent<Button>();
- craftAxeBTN.onClick.AddListener(delegate { CraftAnyItem(AxeBLP); });
- // Plank
- PlankReq1 = refineScreenUI.transform.Find("Plank").transform.Find("req1").GetComponent<Text>();
- craftPlankBTN = refineScreenUI.transform.Find("Plank").transform.Find("Button").GetComponent<Button>();
- craftPlankBTN.onClick.AddListener(delegate { CraftAnyItem(PlankBLP); });
- // Stick
- StickReq1 = refineScreenUI.transform.Find("Stick").transform.Find("req1").GetComponent<Text>();
- craftStickBTN = refineScreenUI.transform.Find("Stick").transform.Find("Button").GetComponent<Button>();
- craftStickBTN.onClick.AddListener(delegate { CraftAnyItem(StickBLP); });
- // Foundation
- FoundationReq1 = constructionScreenUI.transform.Find("Foundation").transform.Find("req1").GetComponent<Text>();
- craftFoundationBTN = constructionScreenUI.transform.Find("Foundation").transform.Find("Button").GetComponent<Button>();
- craftFoundationBTN.onClick.AddListener(delegate { CraftAnyItem(FoundationBLP); });
- // Wall
- WallReq1 = constructionScreenUI.transform.Find("Wall").transform.Find("req1").GetComponent<Text>();
- craftWallBTN = constructionScreenUI.transform.Find("Wall").transform.Find("Button").GetComponent<Button>();
- craftWallBTN.onClick.AddListener(delegate { CraftAnyItem(WallBLP); });
- }
- void OpenToolsCategory()
- {
- craftingScreenUI.SetActive(false);
- survivalScreenUI.SetActive(false);
- refineScreenUI.SetActive(false);
- constructionScreenUI.SetActive(false);
- toolsScreenUI.SetActive(true);
- }
- void OpenSurvivalCategory()
- {
- craftingScreenUI.SetActive(false);
- toolsScreenUI.SetActive(false);
- refineScreenUI.SetActive(false);
- constructionScreenUI.SetActive(false);
- survivalScreenUI.SetActive(true);
- }
- void OpenRefineCategory()
- {
- craftingScreenUI.SetActive(false);
- toolsScreenUI.SetActive(false);
- survivalScreenUI.SetActive(false);
- constructionScreenUI.SetActive(false);
- refineScreenUI.SetActive(true);
- }
- void OpenConstructionCategory()
- {
- craftingScreenUI.SetActive(false);
- toolsScreenUI.SetActive(false);
- survivalScreenUI.SetActive(false);
- refineScreenUI.SetActive(false);
- constructionScreenUI.SetActive(true);
- }
- void CraftAnyItem(Blueprint blueprintToCraft)
- {
- SoundManager.Instance.PlaySound(SoundManager.Instance.craftingSound);
- StartCoroutine(craftedDelayForSound(blueprintToCraft));
- if (blueprintToCraft.numOfRequirements == 1) //remove resources from inventory
- {
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
- }
- else if (blueprintToCraft.numOfRequirements == 2)
- {
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
- }
- else if (blueprintToCraft.numOfRequirements == 3) //remove resources from inventory
- {
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
- }
- else if (blueprintToCraft.numOfRequirements == 4) //remove resources from inventory
- {
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
- }
- else if (blueprintToCraft.numOfRequirements == 5) //remove resources from inventory
- {
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
- InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
- }
- StartCoroutine(calculate()); // refresh list
- }
- public IEnumerator calculate()
- {
- yield return 0; // yield return new WaitForSeconds(1f); //To wait to do something
- InventorySystem.Instance.ReCalculateList();
- RefreshNeededItems();
- }
- IEnumerator craftedDelayForSound(Blueprint blueprintToCraft)
- {
- yield return new WaitForSeconds(10f); //changed time due to my audio clip
- SoundManager.Instance.craftingSound.Stop(); //stops the audio clip from continuing because it was too long
- // Produce the amount of items according to the blueprint
- for (var i = 0; i < blueprintToCraft.numberOfItemsToProduce; i++)
- {
- InventorySystem.Instance.AddToInventory(blueprintToCraft.itemName); //add item into inventory
- }
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.C) && !isOpen && !ConstructionManager.Instance.inConstructionMode)
- {
- craftingScreenUI.SetActive(true);
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- SelectionManager.Instance.DisableSelection();
- SelectionManager.Instance.GetComponent<SelectionManager>().enabled = false;
- isOpen = true;
- }
- else if (Input.GetKeyDown(KeyCode.C) && isOpen)
- {
- craftingScreenUI.SetActive(false);
- toolsScreenUI.SetActive(false);
- survivalScreenUI.SetActive(false);
- refineScreenUI.SetActive(false);
- constructionScreenUI.SetActive(false);
- if (!InventorySystem.Instance.isOpen)
- {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- SelectionManager.Instance.EnableSelection();
- SelectionManager.Instance.GetComponent<SelectionManager>().enabled = true;
- }
- isOpen = false;
- }
- }
- public void RefreshNeededItems()
- {
- int stone_count = 0;
- int stick_count = 0;
- int log_count = 0;
- int treeBranch_count = 0;
- int plank_count = 0;
- inventoryItemList = InventorySystem.Instance.itemList;
- foreach (string itemName in inventoryItemList)
- {
- switch (itemName)
- {
- case "Stone":
- stone_count++;
- break;
- case "Stick":
- stick_count++;
- break;
- case "Log":
- log_count++;
- break;
- case "Tree Branch":
- treeBranch_count++;
- break;
- case "Plank":
- plank_count++;
- break;
- }
- }
- // Axe
- AxeReq1.text = "Stone [" + stone_count + "/2]";
- AxeReq2.text = "Stick [" + stick_count + "/1]";
- if (stone_count >= 2 && stick_count >=1 && InventorySystem.Instance.CheckSlotsAvailable(1))
- {
- craftAxeBTN.gameObject.SetActive(true);
- }
- else
- {
- craftAxeBTN.gameObject.SetActive(false);
- }
- // Plank
- PlankReq1.text = "Log [" + log_count + "/1]";
- if (log_count >= 1 && InventorySystem.Instance.CheckSlotsAvailable(2))
- {
- craftPlankBTN.gameObject.SetActive(true);
- }
- else
- {
- craftPlankBTN.gameObject.SetActive(false);
- }
- // Stick
- StickReq1.text = "Branch [" + treeBranch_count + "/1]";
- if (treeBranch_count >= 1 && InventorySystem.Instance.CheckSlotsAvailable(1))
- {
- craftStickBTN.gameObject.SetActive(true);
- }
- else
- {
- craftStickBTN.gameObject.SetActive(false);
- }
- // Foundation
- FoundationReq1.text = "Plank [" + plank_count + "/4]";
- if (plank_count >= 4 && InventorySystem.Instance.CheckSlotsAvailable(1))
- {
- craftFoundationBTN.gameObject.SetActive(true);
- }
- else
- {
- craftFoundationBTN.gameObject.SetActive(false);
- }
- // Wall
- WallReq1.text = "Plank [" + plank_count + "/5]";
- if (plank_count >= 5 && InventorySystem.Instance.CheckSlotsAvailable(1))
- {
- craftWallBTN.gameObject.SetActive(true);
- }
- else
- {
- craftWallBTN.gameObject.SetActive(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement