221 lines
7.3 KiB
C#
221 lines
7.3 KiB
C#
using System.Collections.Generic;
|
|
using FishNet.Managing;
|
|
using FishNet.Transporting;
|
|
using Managers;
|
|
using Types;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using Utils;
|
|
|
|
namespace UI
|
|
{
|
|
public class NetworkUIController : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject hostNewButton;
|
|
[SerializeField] private GameObject hostSaveButton;
|
|
[SerializeField] private GameObject connectButton;
|
|
[SerializeField] private SpriteRenderer serverIndicator;
|
|
[SerializeField] private SpriteRenderer clientIndicator;
|
|
[SerializeField] private readonly Dictionary<int, GamePlayer> _playerList = new();
|
|
|
|
private LocalConnectionState _clientState = LocalConnectionState.Stopped;
|
|
|
|
private EventSystem _eventSystem;
|
|
|
|
private NetworkManager _networkManager;
|
|
private LocalConnectionState _serverState = LocalConnectionState.Stopped;
|
|
|
|
public static NetworkUIController Instance { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_playerList.Clear();
|
|
_networkManager = FindObjectOfType<NetworkManager>();
|
|
if (_networkManager == null)
|
|
{
|
|
GameLogger.LogError("NetworkManager not found, HUD will not function.");
|
|
}
|
|
else
|
|
{
|
|
// UpdateColor(LocalConnectionState.Stopped, ref serverIndicator);
|
|
// UpdateColor(LocalConnectionState.Stopped, ref clientIndicator);
|
|
_networkManager.ServerManager.OnServerConnectionState += OnServerConnectionState;
|
|
_networkManager.ClientManager.OnClientConnectionState += OnClientConnectionState;
|
|
}
|
|
|
|
// host.GetComponent<Button>().onClick.AddListener(OnClick_Client);
|
|
// serverButton.GetComponent<Button>().onClick.AddListener(OnClick_Server);
|
|
// hostNewButton.GetComponent<Button>().onClick.AddListener(OnClick_HostNew);
|
|
// hostSaveButton.GetComponent<Button>().onClick.AddListener(OnClick_HostSave);
|
|
// connectButton.GetComponent<Button>().onClick.AddListener(OnClick_ConnectClient);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_networkManager == null)
|
|
return;
|
|
_networkManager.ServerManager.OnServerConnectionState -= OnServerConnectionState;
|
|
_networkManager.ClientManager.OnClientConnectionState -= OnClientConnectionState;
|
|
}
|
|
|
|
public void OnClick_HidePanel()
|
|
{
|
|
if (gameObject.activeSelf)
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnServerConnectionState(ServerConnectionStateArgs obj)
|
|
{
|
|
_serverState = obj.ConnectionState;
|
|
// UpdateColor(obj.ConnectionState, ref serverIndicator);
|
|
}
|
|
|
|
private void UpdateColor(LocalConnectionState state, ref SpriteRenderer img)
|
|
{
|
|
Color c;
|
|
if (state == LocalConnectionState.Started)
|
|
c = Color.green;
|
|
else if (state == LocalConnectionState.Stopped)
|
|
c = Color.red;
|
|
else
|
|
c = Color.yellow;
|
|
img.color = c;
|
|
}
|
|
|
|
private void OnClientConnectionState(ClientConnectionStateArgs obj)
|
|
{
|
|
_clientState = obj.ConnectionState;
|
|
// UpdateColor(obj.ConnectionState, ref clientIndicator);
|
|
|
|
// add player to connected players list
|
|
/*if (_clientState == LocalConnectionState.Started)
|
|
// _playerList = GameManager.Instance.GetPlayers();
|
|
else if (_clientState == LocalConnectionState.Stopped)
|
|
// InstanceFinder.ClientManager.UnregisterBroadcast<ChatBroadcast>(OnChatBroadcast);
|
|
_playerList.Clear();*/
|
|
}
|
|
|
|
|
|
public void OnClick_HostNew()
|
|
{
|
|
GameLogger.Log("Clicked host new");
|
|
if (_networkManager == null)
|
|
{
|
|
GameLogger.LogWarning("NetworkManager not found");
|
|
// find it
|
|
_networkManager = FindObjectOfType<NetworkManager>();
|
|
// return;
|
|
if (_networkManager == null)
|
|
{
|
|
GameLogger.LogError("NetworkManager not found");
|
|
return;
|
|
}
|
|
}
|
|
|
|
// attach prefab with SystemsManager to root
|
|
|
|
var gameManagerPrefab = Resources.Load<GameObject>("Prefabs/GameManager");
|
|
|
|
/*if (systemsManagerPrefab == null)
|
|
{
|
|
GameLogger.LogError("SystemsManager prefab not found");
|
|
return;
|
|
}*/
|
|
|
|
if (gameManagerPrefab == null)
|
|
{
|
|
GameLogger.LogError("GameManager prefab not found");
|
|
return;
|
|
}
|
|
|
|
// wait for scene to load before starting server
|
|
OnClick_Server();
|
|
OnClick_Client();
|
|
|
|
var saveManager = FindObjectOfType<SaveManager>();
|
|
if (saveManager == null) GameLogger.LogError("SaveManager not found, playerData will be null");
|
|
|
|
// check if save manager is loaded
|
|
if (saveManager.GameState == null) GameLogger.LogError("GameState is null, playerData will be null");
|
|
|
|
|
|
// TODO: Go to NewGamePanel, and flag for MP.
|
|
GameLogger.Log("Completed host new click flow");
|
|
}
|
|
|
|
public void OnClick_HostSave()
|
|
{
|
|
GameLogger.Log("Clicked host save");
|
|
if (_networkManager == null)
|
|
return;
|
|
|
|
OnClick_Server();
|
|
OnClick_Client();
|
|
// TODO: Don't start reroute to SavePanel to pick a save file, flag for MP.
|
|
}
|
|
|
|
public void OnClick_Server()
|
|
{
|
|
GameLogger.Log("Clicked server");
|
|
if (_networkManager == null)
|
|
return;
|
|
|
|
if (_serverState != LocalConnectionState.Stopped)
|
|
_networkManager.ServerManager.StopConnection(true);
|
|
else
|
|
_networkManager.ServerManager.StartConnection();
|
|
|
|
DeselectButtons();
|
|
}
|
|
|
|
public void OnClick_Client()
|
|
{
|
|
GameLogger.Log("Clicked client");
|
|
if (_networkManager == null)
|
|
return;
|
|
|
|
if (_clientState != LocalConnectionState.Stopped)
|
|
_networkManager.ClientManager.StopConnection();
|
|
else
|
|
_networkManager.ClientManager.StartConnection();
|
|
|
|
DeselectButtons();
|
|
}
|
|
|
|
public void OnClick_ConnectClient()
|
|
{
|
|
OnClick_Client();
|
|
// get playermanager
|
|
|
|
// _networkManager.ClientManager.StartConnection("127.0.0.1", 7770);
|
|
}
|
|
|
|
private void SetEventSystem()
|
|
{
|
|
if (_eventSystem != null)
|
|
return;
|
|
_eventSystem = FindObjectOfType<EventSystem>();
|
|
if (_eventSystem == null)
|
|
_eventSystem = gameObject.AddComponent<EventSystem>();
|
|
}
|
|
|
|
private void DeselectButtons()
|
|
{
|
|
SetEventSystem();
|
|
_eventSystem?.SetSelectedGameObject(null);
|
|
}
|
|
}
|
|
} |