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

168 lines
3.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Networking.Client;
  5. using TMPro;
  6. public class LoginUIManager : MonoBehaviour
  7. {
  8. [Header("References")]
  9. [SerializeField]
  10. private ClientLoginManager clientManager;
  11. [SerializeField]
  12. private TextMeshProUGUI Title;
  13. [SerializeField]
  14. private GameObject Content;
  15. [Header("Connection")]
  16. [SerializeField]
  17. private string ConnectionTitle = "Connect to Server";
  18. [SerializeField]
  19. private GameObject ConnectionObject;
  20. [Header("PlayerDetails")]
  21. [SerializeField]
  22. private string DetailsTitle = "Player Details";
  23. [SerializeField]
  24. private GameObject DetailsObject;
  25. [Header("WaitForPlayers")]
  26. [SerializeField]
  27. private string WaitTitle = "Wait for Players";
  28. [SerializeField]
  29. private string SceneToLoad;
  30. [SerializeField]
  31. private GameObject WaitObject;
  32. private string serverAddress;
  33. private int serverPort;
  34. private string playerName;
  35. private string playerAnimal;
  36. private Color playerColor;
  37. #region Unity Functions
  38. private void OnEnable()
  39. {
  40. clientManager.OnConnectedToServer += OnConnect;
  41. clientManager.OnLoginSucess += OnLogin;
  42. clientManager.OnLoginFail += OnLoginFail;
  43. }
  44. private void OnDisable()
  45. {
  46. clientManager.OnConnectedToServer -= OnConnect;
  47. clientManager.OnLoginSucess -= OnLogin;
  48. clientManager.OnLoginFail -= OnLoginFail;
  49. }
  50. private void Start()
  51. {
  52. serverAddress = PlayerPrefs.GetString("LastUsedAddress", "");
  53. serverPort = PlayerPrefs.GetInt("LastUsedPort", 2222);
  54. playerName = PlayerPrefs.GetString("LastUsedName");
  55. OnLoginFail();
  56. }
  57. #endregion Unity Functions
  58. #region Event Functions
  59. public void OnConnect()
  60. {
  61. Title.text = DetailsTitle;
  62. CloseAll();
  63. DetailsObject.SetActive(true);
  64. }
  65. public void OnLogin()
  66. {
  67. Title.text = WaitTitle;
  68. CloseAll();
  69. WaitObject.SetActive(true);
  70. }
  71. public void OnLoginFail()
  72. {
  73. Title.text = ConnectionTitle;
  74. CloseAll();
  75. ConnectionObject.SetActive(true);
  76. }
  77. #endregion Event Functions
  78. #region UI Functionality
  79. public void OnChange_ServerAddress(string serverAddress)
  80. {
  81. this.serverAddress = serverAddress;
  82. }
  83. public void OnChange_ServerPort(string serverPort)
  84. {
  85. this.serverPort = int.Parse(serverPort);
  86. }
  87. public void OnClick_Back()
  88. {
  89. UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu Client");
  90. }
  91. public void OnClick_Connect()
  92. {
  93. clientManager.StartClient(serverAddress, serverPort);
  94. PlayerPrefs.SetString("LastUsedAddress", serverAddress);
  95. PlayerPrefs.SetInt("LastUsedPort", serverPort);
  96. PlayerPrefs.Save();
  97. }
  98. public void OnClick_Done()
  99. {
  100. clientManager.SendPlayerDetails(playerName, playerColor, playerAnimal);
  101. PlayerPrefs.SetString("LastUsedName", playerName);
  102. }
  103. public void OnClick_Color(Color playerColor)
  104. {
  105. this.playerColor = playerColor;
  106. }
  107. public void OnClick_Colour(string hex)
  108. {
  109. Color newColor = new Color();
  110. if (ColorUtility.TryParseHtmlString(hex, out newColor))
  111. this.playerColor = newColor;
  112. clientManager.SendPlayerCDetails(playerColor);
  113. }
  114. public void OnClick_Animal(string playerAnimal)
  115. {
  116. this.playerAnimal = playerAnimal;
  117. clientManager.SendPlayerADetails(playerAnimal);
  118. }
  119. public void OnChange_Name(string playerName)
  120. {
  121. this.playerName = playerName;
  122. }
  123. public void OnClick_Ready()
  124. {
  125. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToLoad);
  126. }
  127. #endregion UI Functionality
  128. #region Helper Functions
  129. private void CloseAll()
  130. {
  131. foreach (Transform child in Content.transform)
  132. child.gameObject.SetActive(false);
  133. }
  134. #endregion Helper Functions
  135. }