FarmManager.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Mirror;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class FarmManager : MonoBehaviour
  8. {
  9. public InventoryManager inventory;
  10. public RectTransform uiPopUp;
  11. public Image fillupImage;
  12. public FarmItem activeItem;
  13. private void Awake() {
  14. HidePopUp();
  15. uiPopUp.GetComponentInChildren<Button>().onClick.AddListener(Interact);
  16. }
  17. public void ShowPopUp(FarmItem item){
  18. activeItem = item;
  19. uiPopUp.gameObject.SetActive(true);
  20. Vector3 offsetUiPopUp = new Vector3(100, 50, 50);
  21. uiPopUp.position = Camera.main.WorldToScreenPoint(item.transform.position) + offsetUiPopUp;
  22. }
  23. public void HidePopUp(){
  24. uiPopUp.gameObject.SetActive(false);
  25. fillupImage.fillAmount= 0f;
  26. }
  27. void Interact(){
  28. if(activeItem ==null){Debug.LogError("Cant farm an item without having one"); return;}
  29. if(!activeItem.isAvailable){Debug.Log("This item is already being farmed",gameObject); return;}
  30. activeItem.Farm();
  31. StartCoroutine(CoroutineFarm());
  32. }
  33. IEnumerator CoroutineFarm(){
  34. float timer =0f;
  35. uiPopUp.GetComponentInChildren<TMP_Text>().text = "Farming";
  36. fillupImage.gameObject.SetActive(true);
  37. while(timer < activeItem.farmingTime){
  38. timer +=Time.deltaTime;
  39. fillupImage.fillAmount = timer / activeItem.farmingTime;
  40. uiPopUp.position = Camera.main.WorldToScreenPoint(activeItem.transform.position);
  41. uiPopUp.gameObject.SetActive(true);
  42. yield return null;
  43. }
  44. HidePopUp();
  45. FarmingManager.instance.DestroyItem(activeItem.gameObject);
  46. uiPopUp.GetComponentInChildren<TMP_Text>().text = "Farm";
  47. inventory.AddInvItem(activeItem.lootDrop);
  48. }
  49. }