106 lines
3.0 KiB
C#
106 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class FarmManager : NetworkBehaviour
|
|
{
|
|
public InventoryManager inventory;
|
|
public RectTransform uiPopUp;
|
|
|
|
public Image fillupImage;
|
|
public FarmItem activeItem;
|
|
public FarmItem farmingItem;
|
|
|
|
private void Awake()
|
|
{
|
|
HidePopUp();
|
|
|
|
uiPopUp.GetComponentInChildren<Button>().onClick.AddListener(Interact);
|
|
}
|
|
|
|
public void ShowPopUp(FarmItem item)
|
|
{
|
|
if (farmingItem != null && farmingItem != item) { return; }
|
|
if (!item.isAvailable && farmingItem != item) { return; }
|
|
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; }
|
|
if (isServer)
|
|
{
|
|
activeItem.isAvailable = false;
|
|
}
|
|
else
|
|
{
|
|
CmdDeactivateItem(activeItem.gameObject);
|
|
}
|
|
farmingItem = activeItem;
|
|
StartCoroutine(CoroutineFarm());
|
|
}
|
|
|
|
[Command]
|
|
void CmdDeactivateItem(GameObject item)
|
|
{
|
|
item.GetComponent<FarmItem>().isAvailable = false;
|
|
}
|
|
|
|
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);
|
|
DestroyItem(farmingItem.GetComponent<NetworkIdentity>().netId);
|
|
Debug.Log("Requesting to delete this item ", farmingItem);
|
|
farmingItem = null;
|
|
|
|
uiPopUp.GetComponentInChildren<TMP_Text>().text = "Farm";
|
|
inventory.AddInvItem(activeItem.lootDrop);
|
|
}
|
|
|
|
void DestroyItem(uint itemNetId)
|
|
{
|
|
if (isServer)
|
|
{
|
|
FarmingManager.instance.DestroyItemByID(itemNetId);
|
|
}
|
|
else
|
|
{
|
|
CmdDestroyItem(itemNetId);
|
|
}
|
|
}
|
|
|
|
[Command]
|
|
void CmdDestroyItem(uint itemNetId)
|
|
{
|
|
FarmingManager.instance.DestroyItemByID(itemNetId);
|
|
}
|
|
}
|