3DSurvivalGame / Assets /Scripts /CraftingSystem.cs
doc2txt's picture
1
3497d64
raw
history blame contribute delete
No virus
8.49 kB
using System;
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,craftFoundationBTN, craftWallBTN;
// Requirements Text
Text AxeReq1, AxeReq2, PlankReq1,FoundationReq1,WallReq1;
public bool isOpen;
// All BLueprint
public BluePrint AxeBLP = new BluePrint("Axe", 1, 2, "Stone", 3, "Stick", 3);
public BluePrint PlankBLP = new BluePrint("Plank",2, 1, "Log", 1, "", 0);
public BluePrint FoundationBLP = new BluePrint("Foundation",1, 1, "Plank", 4, "", 0);
public BluePrint WallBLP = new BluePrint("Wall",1, 1, "Plank", 2, "", 0);
public static CraftingSystem Instance { get; set; }
private void Awake()
{
if (Instance != null && Instance != this)
{
DestroyObject(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); });
// 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); });
// foundation
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);
toolsScreenUI.SetActive(true);
}
void OpenSurvivalCategory()
{
craftingScreenUI.SetActive(false);
survivalScreenUI.SetActive(true);
}
void OpenRefineCategory()
{
craftingScreenUI.SetActive(false);
refineScreenUI.SetActive(true);
}
void OpenConstructionCategory()
{
craftingScreenUI.SetActive(false);
constructionScreenUI.SetActive(true);
}
private void CraftAnyItem(BluePrint blueprintToCraft)
{
SoundManager.Instance.PlaySound(SoundManager.Instance.dropItemSound);
for (int i = 0; i <blueprintToCraft.numberOfItemsToProduce; i++) {
InventorySystem.Instance.AddToInventory(blueprintToCraft.itemName);
}
InventorySystem.Instance.RemoveItem(blueprintToCraft.Req1, blueprintToCraft.Req1amount);
if (blueprintToCraft.numberOfRequiredItems > 1)
InventorySystem.Instance.RemoveItem(blueprintToCraft.Req2, blueprintToCraft.Req2amount);
StartCoroutine(calculate());
}
public IEnumerator calculate()
{
yield return 0; //new WaitForSeconds(1f);
InventorySystem.Instance.ReCalculateList();
RefreshNeededItems();
}
public void RefreshNeededItems()
{
int stone_count = 0;
int stick_count = 0;
int log_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 "Plank":
plank_count++;
break;
}
}
// Axe
AxeReq1.text = AxeBLP.Req1amount + " Stone [" + stone_count + "]";
AxeReq2.text = AxeBLP.Req2amount + " Stick [" + stick_count + "]";
if (stick_count >= AxeBLP.Req1amount && stick_count >= AxeBLP.Req2amount
&& InventorySystem.Instance.CheckSlotsAvailable(AxeBLP.numberOfItemsToProduce))
{
craftAxeBTN.gameObject.SetActive(true);
}
else
{
craftAxeBTN.gameObject.SetActive(false);
}
// Plank
PlankReq1.text = PlankBLP.Req1amount + " Log [" + log_count + "]";
if (log_count >= PlankBLP.Req1amount
&& InventorySystem.Instance.CheckSlotsAvailable(PlankBLP.numberOfItemsToProduce))
{
craftPlankBTN.gameObject.SetActive(true);
}
else
{
craftPlankBTN.gameObject.SetActive(false);
}
// Foundation
FoundationReq1.text = FoundationBLP.Req1amount + " Plank [" + plank_count + "]";
if (plank_count >= FoundationBLP.Req1amount
&& InventorySystem.Instance.CheckSlotsAvailable(FoundationBLP.numberOfItemsToProduce))
{
craftFoundationBTN.gameObject.SetActive(true);
}
else
{
craftFoundationBTN.gameObject.SetActive(false);
}
// Wall
WallReq1.text = WallBLP.Req1amount + " Plank [" + plank_count + "]";
if (plank_count >= WallBLP.Req1amount
&& InventorySystem.Instance.CheckSlotsAvailable(WallBLP.numberOfItemsToProduce))
{
craftWallBTN.gameObject.SetActive(true);
}
else
{
craftWallBTN.gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
if (!isOpen)
{
craftingScreenUI.SetActive(true);
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
SelectionManager.Instance.DisableSelection();
SelectionManager.Instance.GetComponent<SelectionManager>().enabled = false;
isOpen = true;
}
else
{
craftingScreenUI.SetActive(false);
toolsScreenUI.SetActive(false);
survivalScreenUI.SetActive(false);
refineScreenUI.SetActive(false);
constructionScreenUI.SetActive(false);
isOpen = false;
if (!InventorySystem.Instance.isOpen)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
SelectionManager.Instance.EnableSelection();
SelectionManager.Instance.GetComponent<SelectionManager>().enabled = true;
}
}
}
}
}