CraftManager.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class CraftManager : MonoBehaviour
  6. {
  7. public InventoryManager inventoryManager;
  8. public RectTransform CraftUI;
  9. public List<CraftingEntry> craftingEntries;
  10. public static CraftManager instance;
  11. public void ShowCraftUI()
  12. {
  13. Debug.Log("Crafting UI is shown");
  14. CraftUI.gameObject.SetActive(true);
  15. }
  16. public void HideCraftUI()
  17. {
  18. Debug.Log("Crafting UI is hidden");
  19. CraftUI.gameObject.SetActive(false);
  20. }
  21. void Awake(){
  22. instance = this;
  23. foreach (var entry in craftingEntries)
  24. {
  25. entry.btn.onClick.AddListener(() => OnClickRecipe(entry.recipe));
  26. }
  27. }
  28. void OnClickRecipe(resipies_so recipe){
  29. foreach(var entry in craftingEntries){
  30. if(entry.recipe == recipe){
  31. entry.panel.SetActive(true);
  32. }else{
  33. entry.panel.SetActive(false);
  34. }
  35. }
  36. }
  37. public void OnCraftButtonClicked(){
  38. foreach(var entry in craftingEntries){
  39. if(entry.panel.activeSelf){
  40. bool canCraft = true;
  41. foreach(var ingredient in entry.recipe.ingredients){
  42. if(inventoryManager.GetStock(ingredient.item.name) < ingredient.count){
  43. canCraft = false;
  44. Debug.Log("Not enough " + ingredient.item.name);
  45. break;
  46. }
  47. // if(ingredient.count <= 0){
  48. // canCraft = false;
  49. // break;
  50. // }
  51. }
  52. if(canCraft){
  53. Debug.Log("Crafting item: " + entry.recipe.output.name);
  54. inventoryManager.AddInvItem(entry.recipe.output);
  55. foreach(RecipeIngredientEntry ingredient in entry.recipe.ingredients){
  56. inventoryManager.RemoveItem(ingredient.item.name, ingredient.count);
  57. }
  58. }else{
  59. Debug.Log("Cannot craft item: " + entry.recipe.output.name);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. [System.Serializable]
  66. public class CraftingEntry{
  67. public resipies_so recipe;
  68. public Button btn;
  69. public GameObject panel;
  70. }