|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Networking.Client;
- using TMPro;
-
- public class LoginUIManager : MonoBehaviour
- {
- [Header("References")]
- [SerializeField]
- private ClientLoginManager clientManager;
- [SerializeField]
- private TextMeshProUGUI Title;
- [SerializeField]
- private GameObject Content;
-
- [Header("Connection")]
- [SerializeField]
- private string ConnectionTitle = "Connect to Server";
- [SerializeField]
- private GameObject ConnectionObject;
-
- [Header("PlayerDetails")]
- [SerializeField]
- private string DetailsTitle = "Player Details";
- [SerializeField]
- private GameObject DetailsObject;
-
- [Header("WaitForPlayers")]
- [SerializeField]
- private string WaitTitle = "Wait for Players";
- [SerializeField]
- private string SceneToLoad;
- [SerializeField]
- private GameObject WaitObject;
-
-
- private string serverAddress;
- private int serverPort;
- private string playerName;
- private string playerAnimal;
- private Color playerColor;
-
- bool animalSelected = false;
- bool colorSelected = false;
-
-
- #region Unity Functions
- private void OnEnable()
- {
- clientManager.OnConnectedToServer += OnConnect;
- clientManager.OnLoginSucess += OnLogin;
- clientManager.OnLoginFail += OnLoginFail;
- }
-
- private void OnDisable()
- {
- clientManager.OnConnectedToServer -= OnConnect;
- clientManager.OnLoginSucess -= OnLogin;
- clientManager.OnLoginFail -= OnLoginFail;
- }
-
- private void Start()
- {
- serverAddress = PlayerPrefs.GetString("LastUsedAddress", "");
- serverPort = PlayerPrefs.GetInt("LastUsedPort", 2222);
- playerName = PlayerPrefs.GetString("LastUsedName");
-
- OnLoginFail();
- }
-
-
- #endregion Unity Functions
-
-
- #region Event Functions
- public void OnConnect()
- {
- Title.text = DetailsTitle;
-
- CloseAll();
- DetailsObject.SetActive(true);
- }
-
- public void OnLogin()
- {
- Title.text = WaitTitle;
- CloseAll();
- WaitObject.SetActive(true);
- }
-
- public void OnLoginFail()
- {
- Title.text = ConnectionTitle;
-
- CloseAll();
- ConnectionObject.SetActive(true);
- }
- #endregion Event Functions
-
- #region UI Functionality
- public void OnChange_ServerAddress(string serverAddress)
- {
- this.serverAddress = serverAddress;
- }
-
- public void OnChange_ServerPort(string serverPort)
- {
- this.serverPort = int.Parse(serverPort);
- }
-
- public void OnClick_Back()
- {
- UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu Client");
- }
-
- public void OnClick_Connect()
- {
- clientManager.StartClient(serverAddress, serverPort);
- PlayerPrefs.SetString("LastUsedAddress", serverAddress);
- PlayerPrefs.SetInt("LastUsedPort", serverPort);
- PlayerPrefs.Save();
- }
-
- public void OnClick_Done()
- {
- if(colorSelected == true && animalSelected == true && playerName != ""){
- clientManager.SendPlayerDetails(playerName, playerColor, playerAnimal);
- PlayerPrefs.SetString("LastUsedName", playerName);
- }
- }
-
- public void OnClick_Color(Color playerColor)
- {
- this.playerColor = playerColor;
- }
-
- public void OnClick_Colour(string hex)
- {
- Color newColor = new Color();
- if (ColorUtility.TryParseHtmlString(hex, out newColor)){
- this.playerColor = newColor;
- colorSelected = true;
- }
-
- clientManager.SendPlayerCDetails(playerColor);
- }
-
- public void OnClick_Animal(string playerAnimal)
- {
- this.playerAnimal = playerAnimal;
- animalSelected = true;
- clientManager.SendPlayerADetails(playerAnimal);
- }
-
- public void OnChange_Name(string playerName)
- {
- this.playerName = playerName;
- }
-
- public void OnClick_Ready()
- {
- UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToLoad);
- }
- #endregion UI Functionality
-
- #region Helper Functions
-
- private void CloseAll()
- {
- foreach (Transform child in Content.transform)
- child.gameObject.SetActive(false);
- }
-
- #endregion Helper Functions
-
- }
|