mmorpg/Assets/Script/FarmManager.cs
2024-08-26 17:56:49 +05:30

63 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Mirror;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class FarmManager : MonoBehaviour
{
public InventoryManager inventory;
public RectTransform uiPopUp;
public Image fillupImage;
public FarmItem activeItem;
private void Awake() {
HidePopUp();
uiPopUp.GetComponentInChildren<Button>().onClick.AddListener(Interact);
}
public void ShowPopUp(FarmItem item){
activeItem = item;
uiPopUp.gameObject.SetActive(true);
Vector3 offsetUiPopUp = new Vector3(100, 50, 50);
uiPopUp.position = Camera.main.WorldToScreenPoint(item.transform.position) + offsetUiPopUp;
}
public void HidePopUp(){
uiPopUp.gameObject.SetActive(false);
fillupImage.fillAmount= 0f;
}
void Interact(){
if(activeItem ==null){Debug.LogError("Cant farm an item without having one"); return;}
if(!activeItem.isAvailable){Debug.Log("This item is already being farmed",gameObject); return;}
activeItem.Farm();
StartCoroutine(CoroutineFarm());
}
IEnumerator CoroutineFarm(){
float timer =0f;
uiPopUp.GetComponentInChildren<TMP_Text>().text = "Farming";
fillupImage.gameObject.SetActive(true);
while(timer < activeItem.farmingTime){
timer +=Time.deltaTime;
fillupImage.fillAmount = timer / activeItem.farmingTime;
uiPopUp.position = Camera.main.WorldToScreenPoint(activeItem.transform.position);
uiPopUp.gameObject.SetActive(true);
yield return null;
}
HidePopUp();
FarmingManager.instance.DestroyItem(activeItem.gameObject);
uiPopUp.GetComponentInChildren<TMP_Text>().text = "Farm";
inventory.AddInvItem(activeItem.lootDrop);
}
}