74 lines
1.8 KiB
C#
74 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
|
|
public class FarmingManager : NetworkBehaviour
|
|
{
|
|
public static FarmingManager instance;
|
|
void Awake(){
|
|
instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if(!isServer){
|
|
Destroy(this);
|
|
}
|
|
|
|
for(int i = 0; i<farmingItems.Count; i++ ){
|
|
farmingItems[i].destroyedTime = -timeToReSpawn;
|
|
}
|
|
}
|
|
|
|
public float timeToReSpawn = 180f;
|
|
public List<FarmingPositionEntry> farmingItems;
|
|
|
|
void Update()
|
|
{
|
|
for(int i = 0; i<farmingItems.Count; i++ ){
|
|
FarmingPositionEntry farmingItem = farmingItems[i];
|
|
if(farmingItem.spawnedItem == null ){
|
|
if(Time.time - farmingItem.destroyedTime > timeToReSpawn ){
|
|
Spawn(i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Spawn(int index){
|
|
FarmingPositionEntry item = farmingItems[index];
|
|
GameObject spawn = Instantiate(item.prefab , item.spawnLocation.position , Quaternion.identity);
|
|
farmingItems[index].spawnedItem = spawn;
|
|
NetworkServer.Spawn(spawn);
|
|
}
|
|
|
|
public void DestroyItem(GameObject item){
|
|
int targetIndex =0;
|
|
for(int i=0; i < farmingItems.Count; i++){
|
|
if(farmingItems[i].spawnedItem == item){
|
|
targetIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
farmingItems[targetIndex].destroyedTime = Time.time;
|
|
farmingItems[targetIndex].spawnedItem = null;
|
|
|
|
NetworkServer.Destroy(item);
|
|
}
|
|
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class FarmingPositionEntry{
|
|
|
|
public Transform spawnLocation;
|
|
public GameObject prefab;
|
|
public GameObject spawnedItem;
|
|
public float destroyedTime;
|
|
|
|
|
|
|
|
}
|