evershade/Assets/Scripts/GameInit.cs

162 lines
4.6 KiB
C#

using FishNet.Connection;
using FishNet.Managing;
using FishNet.Object;
using FishNet.Transporting;
using Managers;
using UnityEngine;
using Utils;
public class GameInit : MonoBehaviour
{
[SerializeField] private GameObject systemsManagerPrefab;
[SerializeField] private GameObject networkManagerPrefab;
[SerializeField] private NetworkObject gameManagerPrefab;
[SerializeField] private GameObject notificationPrefab;
private NetworkManager _networkManager;
public static GameInit Instance { get; set; }
private void Awake()
{
InitializeSystemsManager();
InitializeNetworkManager();
_networkManager = FindObjectOfType<NetworkManager>();
if (_networkManager == null) GameLogger.LogError("NetworkManager not found");
// ensure this is singleton
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
if (_networkManager != null)
{
_networkManager.ServerManager.OnServerConnectionState += OnServerConnectionStateChange;
_networkManager.ClientManager.OnClientConnectionState += OnClientConnectionStateChange;
}
}
private void OnDestroy()
{
if (_networkManager != null)
{
_networkManager.ServerManager.OnServerConnectionState -= OnServerConnectionStateChange;
_networkManager.ClientManager.OnClientConnectionState -= OnClientConnectionStateChange;
_networkManager.ServerManager.OnRemoteConnectionState -= OnRemoteConnectionStateChange;
//_networkManager.ClientManager.OnRemot
}
}
private void OnRemoteConnectionStateChange(NetworkConnection arg1, RemoteConnectionStateArgs arg2)
{
}
private void OnServerConnectionStateChange(ServerConnectionStateArgs args)
{
GameLogger.Log("Server Connection State Changed " + args.ConnectionState);
// if (args.ConnectionState == LocalConnectionState.Started) InitializeGameManager();
}
private void OnClientConnectionStateChange(ClientConnectionStateArgs args)
{
// InitializeGameManager();
var message = "Client Connection State Changed " + args.ConnectionState;
GameLogger.Log(message);
ShowNotification(message);
}
private void ShowNotification(string message)
{
}
private void InitializeSystemsManager()
{
if (FindObjectOfType<SaveManager>() == null)
{
var systemsManagerInstance = Instantiate(systemsManagerPrefab);
DontDestroyOnLoad(systemsManagerInstance);
}
}
private void InitializeNetworkManager()
{
if (FindObjectOfType<NetworkManager>() == null)
{
var networkManagerInstance = Instantiate(networkManagerPrefab);
DontDestroyOnLoad(networkManagerInstance);
}
}
public void InitializeGameManager()
{
// we need to make sure that the network manager is initialized first
if (_networkManager == null)
{
GameLogger.LogError("NetworkManager not found");
return;
}
GameLogger.Log("Initializing GameManager...");
if (FindObjectOfType<GameManager>() == null)
{
// check if gameManagerPrefab is null
if (gameManagerPrefab == null)
{
GameLogger.LogError("GameManager prefab not found");
return;
}
GameLogger.Log("GameManager prefab found");
var gameManagerInstance = Instantiate(gameManagerPrefab);
_networkManager.ServerManager.Spawn(gameManagerInstance);
// check if instance was created
if (gameManagerInstance == null)
{
GameLogger.LogError("GameManager instance not found");
return;
}
var gameManager = gameManagerInstance.GetComponent<GameManager>();
if (gameManager == null)
{
GameLogger.LogError("GameManager component not found on prefab");
Destroy(gameManagerInstance);
return;
}
gameManager.Initialize(_networkManager);
// gameManagerInstance.SetActive(true);
}
}
public NetworkObject GetGameManagerPrefab()
{
if (gameManagerPrefab == null) GameLogger.LogError("GameManager prefab not found");
return gameManagerPrefab;
}
public NetworkManager GetNetworkManager()
{
return _networkManager;
}
}