mmorpg2d/Assets/Script/CraftManager.cs
2025-06-07 23:53:54 +05:30

83 lines
2.3 KiB
C#

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;
}