128 lines
2.9 KiB
C#
128 lines
2.9 KiB
C#
using HurricaneVR.Framework.Core.UI;
|
|
using Sirenix.OdinInspector;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Zenject;
|
|
|
|
public class LobbyMenuUI : MonoBehaviour
|
|
{
|
|
[Title("Debug")]
|
|
[Inject]
|
|
[ReadOnly]
|
|
private HVRInputModule uiInput;
|
|
|
|
[Inject]
|
|
[ReadOnly]
|
|
private SceneManager sceneManager;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
[Inject]
|
|
private GameManager gameManager;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
private Canvas canvas;
|
|
|
|
[Title("Join or Host")]
|
|
[SerializeField]
|
|
private GameObject joinOrHost;
|
|
|
|
[SerializeField]
|
|
private TMP_InputField roomCodeInput;
|
|
|
|
[SerializeField]
|
|
private Button joinButton;
|
|
|
|
[SerializeField]
|
|
private Button hostButton;
|
|
|
|
[Title("Connected")]
|
|
[SerializeField]
|
|
private GameObject connected;
|
|
|
|
[SerializeField]
|
|
private PlayerItemUI[] playerItems;
|
|
|
|
[SerializeField]
|
|
private Button leaveButton;
|
|
|
|
private bool isConnected => gameManager.IsMultiplayer;
|
|
|
|
private IReadOnlyList<NetworkClient> networkClients = new List<NetworkClient>();
|
|
|
|
private void Start()
|
|
{
|
|
if (uiInput == null) return;
|
|
|
|
canvas = GetComponent<Canvas>();
|
|
uiInput?.AddCanvas(canvas);
|
|
|
|
joinButton.onClick.AddListener(() => JoinClicked());
|
|
hostButton.onClick.AddListener(() => HostClicked());
|
|
leaveButton.onClick.AddListener(() => LeaveClicked());
|
|
gameManager.OnConnected.AddListener(() => UpdateUI());
|
|
gameManager.OnDisconnected.AddListener(() => UpdateUI());
|
|
|
|
gameManager.OnClientsChanged.AddListener((clients) =>
|
|
{
|
|
networkClients = clients;
|
|
UpdateUI();
|
|
});
|
|
|
|
UpdateUI();
|
|
}
|
|
|
|
private void UpdateUI()
|
|
{
|
|
joinOrHost.SetActive(!isConnected);
|
|
connected.SetActive(isConnected);
|
|
joinButton.interactable = true;
|
|
hostButton.interactable = true;
|
|
|
|
for (int i = 0; i < playerItems.Length; i++)
|
|
{
|
|
var item = playerItems[i];
|
|
|
|
if (i > networkClients.Count -1)
|
|
{
|
|
item.Setup(null);
|
|
}
|
|
else
|
|
{
|
|
var client = networkClients[i];
|
|
|
|
var playerInfo = new PlayerInfo()
|
|
{
|
|
Name = client.ClientId.ToString(),
|
|
isLocalPlayer = client.ClientId == gameManager.LocalClient.ClientId
|
|
};
|
|
|
|
item.Setup(playerInfo);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void JoinClicked()
|
|
{
|
|
gameManager.JoinGame(roomCodeInput.text);
|
|
joinButton.interactable = false;
|
|
hostButton.interactable = false;
|
|
}
|
|
|
|
private void HostClicked()
|
|
{
|
|
gameManager.HostGame();
|
|
joinButton.interactable = false;
|
|
hostButton.interactable = false;
|
|
}
|
|
|
|
private void LeaveClicked()
|
|
{
|
|
gameManager.LeaveGame();
|
|
}
|
|
}
|