12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class CraftManager : MonoBehaviour
- {
- public InventoryManager inventoryManager;
- public RectTransform CraftUI;
- public List<CraftingEntry> craftingEntries;
- public static CraftManager instance;
-
- public void ShowCraftUI()
- {
- Debug.Log("Crafting UI is shown");
- CraftUI.gameObject.SetActive(true);
- }
- public void HideCraftUI()
- {
- Debug.Log("Crafting UI is hidden");
- CraftUI.gameObject.SetActive(false);
- }
- void Awake(){
- instance = this;
- foreach (var entry in craftingEntries)
- {
- entry.btn.onClick.AddListener(() => OnClickRecipe(entry.recipe));
- }
- }
-
- void OnClickRecipe(resipies_so recipe){
- foreach(var entry in craftingEntries){
- if(entry.recipe == recipe){
- entry.panel.SetActive(true);
- }else{
- entry.panel.SetActive(false);
- }
- }
- }
- public void OnCraftButtonClicked(){
- foreach(var entry in craftingEntries){
- if(entry.panel.activeSelf){
- bool canCraft = true;
- foreach(var ingredient in entry.recipe.ingredients){
- if(inventoryManager.GetStock(ingredient.item.name) < ingredient.count){
- canCraft = false;
- Debug.Log("Not enough " + ingredient.item.name);
- break;
- }
- // if(ingredient.count <= 0){
- // canCraft = false;
- // break;
- // }
- }
- if(canCraft){
- Debug.Log("Crafting item: " + entry.recipe.output.name);
- inventoryManager.AddInvItem(entry.recipe.output);
- foreach(RecipeIngredientEntry ingredient in entry.recipe.ingredients){
- inventoryManager.RemoveItem(ingredient.item.name, ingredient.count);
- }
- }else{
- Debug.Log("Cannot craft item: " + entry.recipe.output.name);
- }
- }
- }
- }
- }
- [System.Serializable]
- public class CraftingEntry{
- public resipies_so recipe;
- public Button btn;
- public GameObject panel;
- }
|