|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Networking.Client;
- using TMPro;
-
- public class LoginUIManager : MonoBehaviour
- {
- public int conn;
- public int maxPlayers = 2;
- ConnectedClients clients;
- [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;
-
- [Header("TooManyPlayerDetails")]
- [SerializeField]
- private GameObject tooManyObject;
-
- 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 Update()
- {
- conn = GetComponent<animalSelection>().count;
- }
- 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
- IEnumerator DisplayTooManyCoroutine()
- {
- tooManyObject.SetActive(true);
- yield return new WaitForSeconds(5.0f);
- tooManyObject.SetActive(false);
- }
-
- public void OnConnect()
- {
- Title.text = DetailsTitle;
- CloseAll();
- DetailsObject.SetActive(true);
- }
-
- public void OnLogin()
- {
- Title.text = WaitTitle;
- CloseAll();
- WaitObject.SetActive(true);
- }
-
- public void OnLoginFail2Many()
- {
- Title.text = ConnectionTitle;
- CloseAll();
- StartCoroutine(DisplayTooManyCoroutine());
- ConnectionObject.SetActive(true);
- }
-
- public void OnLoginFail()
- {
- Title.text = ConnectionTitle;
- Debug.Log("Failure");
- 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(conn <= maxPlayers)
- {
- if (colorSelected == true && animalSelected == true && playerName != "")
- {
- clientManager.SendPlayerDetails(playerName, playerColor, playerAnimal);
- PlayerPrefs.SetString("LastUsedName", playerName);
- }
- }
- else
- {
- OnLoginFail2Many();
- }
- }
-
- public void OnClick_Animal(string playerAnimal)
- {
- this.playerAnimal = playerAnimal;
- animalSelected = true;
- getPlayerColour(playerAnimal);
- clientManager.SendPlayerADetails(playerAnimal);
- }
-
- public void OnChange_Name(string playerName)
- {
- this.playerName = playerName;
- }
-
- public void OnClick_Ready()
- {
- UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToLoad);
- }
-
- public void getPlayerColour(string playerAnimal)
- {
- Color newColor = new Color();
- string hex = "#000000";
- switch (playerAnimal)
- {
- case "Bear":
- //red
- hex = "#FF0000";
- break;
- case "Cat":
- //sky blue
- hex = "#008BFF";
- break;
- case "Cow":
- //yellow
- hex = "#FFFF00";
- break;
- case "Deer":
- //purpe/pink
- hex = "#B200FF";
- break;
- case "Dog":
- //purple
- hex = "#6700FF";
- break;
- case "Fox":
- //orange
- hex = "#FF7B00";
- break;
- case "Goat":
- //cyan
- hex = "#00FFFF";
- break;
- case "Horse":
- //dark blue
- hex = "#0000FF";
- break;
- case "Lion":
- //lime
- hex = "#00FF00";
- break;
- case "Lizard":
- //light green
- hex = "#7AFF00";
- break;
- case "Panda":
- //salmon
- hex = "#FFA07A";
- break;
- case "Pig":
- //pink
- hex = "#FF00DA";
- break;
- case "Sheep":
- //maroon
- hex = "#800000";
- break;
- case "Turtle":
- //dark green
- hex = "#008000";
- break;
- case "Wolf":
- //grey
- hex = "#808080";
- break;
- case "Zebra":
- //black
- hex = "#000000";
- break;
-
- default:
-
- break;
- }
- if (ColorUtility.TryParseHtmlString(hex, out newColor))
- {
- this.playerColor = newColor;
- colorSelected = true;
- }
- }
- #endregion UI Functionality
-
- #region Helper Functions
-
- private void CloseAll()
- {
- foreach (Transform child in Content.transform)
- child.gameObject.SetActive(false);
- }
-
- #endregion Helper Functions
-
- }
|