2024-03-16 21:11:37 +03:00

66 lines
1.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class CellSpawner : NetworkBehaviour
{
public GameObject prefab;
public float spawnTime;
public int _capacity;
private CustomNetworkManager _networkManager;
private GameObject _instance;
private bool _spawnStarted = false;
private float _timer;
void Start()
{
if (!isServer) return;
_networkManager = NetworkManager.singleton.GetComponent<CustomNetworkManager>();
_capacity = _networkManager.maxConnections;
Console.WriteLine(_capacity);
SpawnPref();
}
void FixedUpdate()
{
if (!isServer || _capacity <= 0) return;
if (_instance == null && _spawnStarted == false)
{
_spawnStarted = true;
}
if (_spawnStarted)
{
_timer += Time.fixedDeltaTime;
}
if (_timer >= 10)
{
_timer = 0;
SpawnPref();
}
}
// IEnumerator RespawnTimer()
// {
// spawnStarted = true;
// yield return new WaitForSeconds();
// SpawnPref();
// }
void SpawnPref()
{
_instance = Instantiate(prefab, transform.position + transform.up * 1.2f, Quaternion.identity); //* 1.8f
NetworkServer.Spawn(_instance);
_spawnStarted = false;
_capacity--;
}
}