You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using Unity.Services.Authentication;
using Unity.Services.Lobbies.Models;
using UnityEngine;
using UnityEngine.UI;
namespace LobbyScripts
{
public class LobbyUI : MonoBehaviour
{
[Header("Panels")]
[SerializeField] private GameObject _mainPanel;
[SerializeField] private GameObject _lobbyPanel;
[SerializeField] private GameObject _playerLobby;
[SerializeField] private GameObject _progressPanel;
[SerializeField] private GameObject _failedPanel;
[SerializeField] private GameObject _enterCodePanel;
[SerializeField] private GameObject _enterLobbyNamePanel;
[SerializeField] private GameObject _lobbyListPanel;
[Header("Buttons")]
[SerializeField] private Button _createSession;
[SerializeField] private Button _joinSession;
[SerializeField] private Button _leaveSession;
[SerializeField] private Button _hostLobby;
[SerializeField] private Button _continueHostLobby;
[SerializeField] private Button _joinLobby;
[SerializeField] private Button _startGameBtn;
[SerializeField] private Button _continueBtn;
[SerializeField] private Button _leaveLobby;
[SerializeField] private Button _lobbyListBtn;
[Header("References")]
[SerializeField] private Canvas _lobbyCanvas;
[SerializeField] private GraphicRaycaster _lobbyRaycaster;
[SerializeField] private TMP_InputField _inputField;
[SerializeField] private TMP_InputField _lobbyNameinputField;
[SerializeField] private TextMeshProUGUI _code;
[SerializeField] private TextMeshProUGUI _progress;
[SerializeField] private TextMeshProUGUI _playersCount;
[Header("Players")]
[SerializeField] private LobbyPlayerList _lobby;
private bool _shouldBePrivate;
private bool _isLobby;
private string _gameCode;
public static event Action<LobbyData, bool> DoHostLobby;
public static event Action<string, bool> DoJoinPrivateLobbyByCode;
public static event Action<string, bool> DoJoinPrivateLobbyByID;
public static event Action DoJoinPublicLobby;
public static event Action DoStartTheGame;
public static event Action DoLeaveLobby;
public static event Action<int> DoCreateASession;
public static event Action<string> DoJoinASession;
private void OnEnable()
{
GameLobby.OnLobbyChanged += UpdateLobby;
GameLobby.OnBasicLobbyChanged += UpdateBasicLobby;
GameLobby.OnLobbyLeaved += ToggleToHostLobby;
GameLobby.OnHostLeftLobby += ToggleToHostLobby;
GameLobby.OnLobbyFailedToJoin += (_,_) => ShowFailedPanel();
GameLobby.OnWaitingForPLayers += WaitingForPlayers;
GameLobby.OnRelayStarted += CreatingRelay;
GameLobby.OnRelayFailed += RelayToCreateFailed;
GameLobby.OnRelayCompleted += RelayCreated;
GameRelay.DelegateUI += ToggleLobbyCanvasOff;
GameRelay.OnRelayFailedToJoined += ShowFailedPanel;
GameLobby.OnUpdateLobby += UpdatingLobby;
GameLobby.OnLobbyUpdated += LobbyUpdated;
GameLobby.OnUpdateLobbyFailed += LobbyFailedToUpdate;
GameLobby.OnGameStarted += ToggleLobbyCanvasOff;
// PlayerController.OnClientDisconnected += EnableLobbyUI;
}
private void OnDisable()
{
GameLobby.OnLobbyChanged -= UpdateLobby;
GameLobby.OnBasicLobbyChanged -= UpdateBasicLobby;
GameLobby.OnWaitingForPLayers -= WaitingForPlayers;
GameLobby.OnLobbyLeaved -= ToggleToHostLobby;
GameLobby.OnHostLeftLobby -= ToggleToHostLobby;
GameLobby.OnRelayStarted -= CreatingRelay;
GameLobby.OnRelayFailed -= RelayToCreateFailed;
GameLobby.OnRelayCompleted -= RelayCreated;
GameRelay.DelegateUI -= ToggleLobbyCanvasOff;
GameRelay.OnRelayFailedToJoined -= ShowFailedPanel;
GameLobby.OnUpdateLobby -= UpdatingLobby;
GameLobby.OnLobbyUpdated -= LobbyUpdated;
GameLobby.OnUpdateLobbyFailed -= LobbyFailedToUpdate;
GameLobby.OnGameStarted -= ToggleLobbyCanvasOff;
// PlayerController.OnClientDisconnected -= EnableLobbyUI;
}
private void Start()
{
_hostLobby.onClick.AddListener(() =>//Get Lobby name from player before making lobby
{
ShowLobbyNamePanel(true);
});
_continueHostLobby.onClick.AddListener(() =>//Create a lobby and host it
{
ShowLobbyNamePanel(false);
var lobbyData = new LobbyData
{
LobbyName = _lobbyNameinputField.text,
MaxPlayers = 20,
IsPrivate = _shouldBePrivate
};
DoHostLobby?.Invoke(lobbyData, true);
_continueHostLobby.interactable = false;
});
_joinLobby.onClick.AddListener(() =>//should join an ongoing lobby
{
_isLobby = true;
ShowCodePanel(true);
_joinLobby.interactable = false;
_hostLobby.interactable = false;
});
_startGameBtn.onClick.AddListener(() =>
{
DoStartTheGame?.Invoke();
});
_createSession.onClick.AddListener(() =>
{
DoCreateASession?.Invoke(20);
});
_joinSession.onClick.AddListener(() =>
{
//To work on join session here
_joinSession.interactable = false;
_isLobby = false;
ShowCodePanel(true);
});
_continueBtn.onClick.AddListener(() =>
{
if (_isLobby)
{
JoinLobby();
}
else
{
DoJoinASession?.Invoke(_gameCode);
}
});
_leaveLobby.onClick.AddListener(() =>
{
DoLeaveLobby?.Invoke();
});
_lobbyListBtn.onClick.AddListener(() =>
{
_lobbyListPanel.SetActive(true);
});
}
public void ShouldBePrivateLobby(bool shouldBePrivate)
{
_shouldBePrivate = shouldBePrivate;
_mainPanel.SetActive(false);
_lobbyPanel.SetActive(true);
}
private void UpdateBasicLobby(GameLobby gameLobby, Lobby lobby)
{
if (gameLobby.IsLobbyHost() && lobby.Players.Count > 1)
{
_joinSession.gameObject.SetActive(false);
_leaveSession.gameObject.SetActive(false);
}
else if (gameLobby.IsLobbyHost() && lobby.Players.Count == 1)
{
_joinSession.gameObject.SetActive(true);
_joinSession.interactable = true;
_leaveSession.gameObject.SetActive(false);
}
if (!gameLobby.IsLobbyHost() && lobby.Players.Count > 1)
{
_joinSession.gameObject.SetActive(false);
_leaveSession.gameObject.SetActive(true);
_leaveSession.interactable = true;
}
}
public void ShowCodePanel(bool active) => _enterCodePanel.SetActive(active);
public void ShowLobbyNamePanel(bool active) => _enterLobbyNamePanel.SetActive(active);
public void JoinLobby() => DoJoinPrivateLobbyByCode?.Invoke(_gameCode, true);
public void JoinLobby(string lobbyID)
{
Debug.Log("Calling JoinLobby(string gameCode)" + lobbyID);
DoJoinPrivateLobbyByID?.Invoke(lobbyID, true);
}
public void SetLobbyCode()
{
_gameCode = _inputField.text;
ShowCodePanel(false);
}
private void UpdateLobby(GameLobby gameLobby, Lobby lobby)
{
if (_lobbyPanel.activeInHierarchy)
_lobbyPanel.SetActive(false);
if(!_playerLobby.activeInHierarchy)
_playerLobby.SetActive(true);
if(_playersCount.gameObject.activeInHierarchy)
_playersCount.gameObject.SetActive(false);
_startGameBtn.gameObject.SetActive(gameLobby.IsLobbyHost());
_code.SetText($"{lobby.LobbyCode}");
HandleStartButton(1, lobby.Players.Count);
if(_lobby.PlayerList.Count == lobby.Players.Count) return;
switch (_lobby.PlayerList.Count < lobby.Players.Count)
{
case true:
RefreshLobby(lobby);
break;
case false:
_lobby.RemovePlayer(lobby.Data["PLAYER_TO_KICK"].Value);
RefreshLobby(lobby);
break;
}
}
private void RefreshLobby(Lobby lobby)
{
_lobby.ClearLobby();
foreach (var player in lobby.Players)
{
_lobby.PlayerJoined(player, lobby, player.Id == lobby.HostId);
}
}
public void HandleStartButton(int maxPlayer, int playerCount) =>
_startGameBtn.interactable = playerCount >= maxPlayer;
private void ShowFailedPanel()
{
_failedPanel.SetActive(true);
_joinLobby.interactable = true;
_hostLobby.interactable = true;
}
private void ToggleLobbyCanvasOff()
{
_lobbyCanvas.enabled = false;
_lobbyRaycaster.enabled = false;
}
private void EnableLobbyUI()
{
if (_lobbyPanel.activeInHierarchy)
_lobbyPanel.SetActive(false);
if(_playerLobby.activeInHierarchy)
_playerLobby.SetActive(false);
if( !_mainPanel.activeInHierarchy)
_mainPanel.SetActive(true);
_joinLobby.interactable = true;
_hostLobby.interactable = true;
_lobbyCanvas.enabled = true;
_lobbyRaycaster.enabled = true;
}
private void UpdatingLobby ()
{
if(_playerLobby.activeInHierarchy)
_playerLobby.SetActive(false);
if(!_progressPanel.activeInHierarchy)
_progressPanel.SetActive(true);
if(_playersCount.gameObject.activeInHierarchy)
_playersCount.gameObject.SetActive(false);
_progress.SetText("Updating Lobby...");
}
private void LobbyFailedToUpdate () => _progress.SetText("Lobby Failed To Update.");
private void LobbyUpdated () => _progress.SetText("Lobby Updated.");
private void WaitingForPlayers(Lobby lobby)
{
if(_progress.gameObject.activeInHierarchy)
_progress.gameObject.SetActive(false);
_playersCount.SetText($"Waiting For Players : {lobby.Players} / {lobby.MaxPlayers}");
}
private void CreatingRelay ()
{
if(_playerLobby.activeInHierarchy)
_playerLobby.SetActive(false);
if(!_progressPanel.activeInHierarchy)
_progressPanel.SetActive(true);
_progress.SetText("Creating Lobby...");
}
private void RelayToCreateFailed () => _progress.SetText("Relay Failed.");
private void RelayCreated () => _progress.SetText("Relay Created.");
private void ToggleToHostLobby(GameLobby gameLobby, Lobby lobby)
{
_playerLobby.SetActive(false);
_mainPanel.SetActive(false);
_lobbyPanel.SetActive(true);
_hostLobby.interactable = true;
_joinLobby.interactable = true;
}
}
}