FarmManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Mirror;
  4. using TMPro;
  5. using Unity.VisualScripting;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class FarmManager : NetworkBehaviour
  9. {
  10. public InventoryManager inventory;
  11. public RectTransform uiPopUp;
  12. public Image fillupImage;
  13. public FarmItem activeItem;
  14. public FarmItem farmingItem;
  15. private void Awake()
  16. {
  17. HidePopUp();
  18. uiPopUp.GetComponentInChildren<Button>().onClick.AddListener(Interact);
  19. }
  20. public void ShowPopUp(FarmItem item)
  21. {
  22. if (farmingItem != null && farmingItem != item) { return; }
  23. if (!item.isAvailable && farmingItem != item) { return; }
  24. activeItem = item;
  25. uiPopUp.gameObject.SetActive(true);
  26. Vector3 offsetUiPopUp = new Vector3(100, 50, 50);
  27. uiPopUp.position = Camera.main.WorldToScreenPoint(item.transform.position) + offsetUiPopUp;
  28. }
  29. public void HidePopUp()
  30. {
  31. uiPopUp.gameObject.SetActive(false);
  32. fillupImage.fillAmount = 0f;
  33. }
  34. void Interact()
  35. {
  36. if (activeItem == null) { Debug.LogError("Cant farm an item without having one"); return; }
  37. if (!activeItem.isAvailable) { Debug.Log("This item is already being farmed", gameObject); return; }
  38. if (isServer)
  39. {
  40. activeItem.isAvailable = false;
  41. }
  42. else
  43. {
  44. CmdDeactivateItem(activeItem.gameObject);
  45. }
  46. farmingItem = activeItem;
  47. StartCoroutine(CoroutineFarm());
  48. }
  49. [Command]
  50. void CmdDeactivateItem(GameObject item)
  51. {
  52. item.GetComponent<FarmItem>().isAvailable = false;
  53. }
  54. IEnumerator CoroutineFarm()
  55. {
  56. float timer = 0f;
  57. uiPopUp.GetComponentInChildren<TMP_Text>().text = "Farming";
  58. fillupImage.gameObject.SetActive(true);
  59. while (timer < activeItem.farmingTime)
  60. {
  61. timer += Time.deltaTime;
  62. fillupImage.fillAmount = timer / activeItem.farmingTime;
  63. uiPopUp.position = Camera.main.WorldToScreenPoint(activeItem.transform.position);
  64. uiPopUp.gameObject.SetActive(true);
  65. yield return null;
  66. }
  67. HidePopUp();
  68. // FarmingManager.instance.DestroyItem(activeItem.gameObject);
  69. DestroyItem(farmingItem.GetComponent<NetworkIdentity>().netId);
  70. Debug.Log("Requesting to delete this item ", farmingItem);
  71. farmingItem = null;
  72. uiPopUp.GetComponentInChildren<TMP_Text>().text = "Farm";
  73. inventory.AddInvItem(activeItem.lootDrop);
  74. }
  75. void DestroyItem(uint itemNetId)
  76. {
  77. if (isServer)
  78. {
  79. FarmingManager.instance.DestroyItemByID(itemNetId);
  80. }
  81. else
  82. {
  83. CmdDestroyItem(itemNetId);
  84. }
  85. }
  86. [Command]
  87. void CmdDestroyItem(uint itemNetId)
  88. {
  89. FarmingManager.instance.DestroyItemByID(itemNetId);
  90. }
  91. }