farming items network
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Mirror;
|
||||
using UnityEngine;
|
||||
|
||||
public class FarmItem : MonoBehaviour
|
||||
public class FarmItem : NetworkBehaviour
|
||||
{
|
||||
public string lootDrop;
|
||||
public float farmingTime = 30f;
|
||||
[SyncVar]
|
||||
public bool isAvailable = true;
|
||||
private void OnTriggerStay2D(Collider2D other) {
|
||||
if(other.tag == "Player"){
|
||||
@@ -18,8 +20,9 @@ public class FarmItem : MonoBehaviour
|
||||
if(other.tag == "Player"){
|
||||
//
|
||||
Debug.Log("GameObject : " + gameObject);
|
||||
if(other.GetComponent<FarmManager>()==null){return;}
|
||||
|
||||
Debug.Log("active item : "+ other.GetComponent<FarmManager>().activeItem);
|
||||
|
||||
if(other.GetComponent<FarmManager>().activeItem.gameObject == gameObject){
|
||||
other.GetComponent<FarmManager>().HidePopUp();
|
||||
Debug.Log("**** player exit from farm item ! ****");
|
||||
@@ -28,6 +31,28 @@ public class FarmItem : MonoBehaviour
|
||||
}
|
||||
|
||||
public void Farm(){
|
||||
isAvailable = false;
|
||||
|
||||
CmdSetAvailability(false);
|
||||
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdSetAvailability(bool val){
|
||||
Debug.Log("This farming item is being used", gameObject);
|
||||
|
||||
isAvailable = val;
|
||||
}
|
||||
|
||||
public void DestroySelf(){
|
||||
if(isServer){
|
||||
NetworkServer.Destroy(gameObject);
|
||||
}else{
|
||||
CmdDestroySelf();
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdDestroySelf(){
|
||||
NetworkServer.Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,18 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Mirror;
|
||||
using TMPro;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class FarmManager : MonoBehaviour
|
||||
public class FarmManager : NetworkBehaviour
|
||||
{
|
||||
public InventoryManager inventory;
|
||||
public RectTransform uiPopUp;
|
||||
|
||||
public Image fillupImage;
|
||||
public FarmItem activeItem;
|
||||
public FarmItem farmingItem;
|
||||
|
||||
private void Awake() {
|
||||
HidePopUp();
|
||||
@@ -20,6 +22,8 @@ public class FarmManager : MonoBehaviour
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -35,12 +39,20 @@ public class FarmManager : MonoBehaviour
|
||||
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();
|
||||
|
||||
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";
|
||||
@@ -53,10 +65,26 @@ public class FarmManager : MonoBehaviour
|
||||
uiPopUp.gameObject.SetActive(true);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
HidePopUp();
|
||||
FarmingManager.instance.DestroyItem(activeItem.gameObject);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class FarmingManager : NetworkBehaviour
|
||||
FarmingPositionEntry item = farmingItems[index];
|
||||
GameObject spawn = Instantiate(item.prefab , item.spawnLocation.position , Quaternion.identity);
|
||||
farmingItems[index].spawnedItem = spawn;
|
||||
NetworkServer.Spawn(spawn);
|
||||
NetworkServer.Spawn(spawn, NetworkConnectionToServer.LocalConnectionId);
|
||||
}
|
||||
|
||||
public void DestroyItem(GameObject item){
|
||||
@@ -64,10 +64,26 @@ public class FarmingManager : NetworkBehaviour
|
||||
|
||||
farmingItems[targetIndex].destroyedTime = Time.time;
|
||||
farmingItems[targetIndex].spawnedItem = null;
|
||||
|
||||
|
||||
NetworkServer.Destroy(item);
|
||||
}
|
||||
|
||||
public void DestroyItemByID(uint itemNetId){
|
||||
Debug.Log("Destroying farming item " + itemNetId);
|
||||
int targetIndex = 0;
|
||||
for(int i =0; i < farmingItems.Count; i++){
|
||||
if(farmingItems[i].spawnedItem == null){continue;}
|
||||
if(farmingItems[i].spawnedItem.GetComponent<NetworkIdentity>().netId == itemNetId){
|
||||
targetIndex =i;
|
||||
farmingItems[i].spawnedItem.GetComponent<FarmItem>().DestroySelf();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
farmingItems[targetIndex].destroyedTime = Time.time;
|
||||
farmingItems[targetIndex].spawnedItem = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
|
||||
Reference in New Issue
Block a user