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.

243 lines
5.7 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. bool animalSelected = true;
  38. bool colorSelected = true;
  39. #region Unity Functions
  40. private void OnEnable()
  41. {
  42. clientManager.OnConnectedToServer += OnConnect;
  43. clientManager.OnLoginSucess += OnLogin;
  44. clientManager.OnLoginFail += OnLoginFail;
  45. }
  46. private void OnDisable()
  47. {
  48. clientManager.OnConnectedToServer -= OnConnect;
  49. clientManager.OnLoginSucess -= OnLogin;
  50. clientManager.OnLoginFail -= OnLoginFail;
  51. }
  52. private void Start()
  53. {
  54. serverAddress = PlayerPrefs.GetString("LastUsedAddress", "");
  55. serverPort = PlayerPrefs.GetInt("LastUsedPort", 2222);
  56. playerName = PlayerPrefs.GetString("LastUsedName");
  57. OnLoginFail();
  58. }
  59. #endregion Unity Functions
  60. #region Event Functions
  61. public void OnConnect()
  62. {
  63. Title.text = DetailsTitle;
  64. CloseAll();
  65. DetailsObject.SetActive(true);
  66. }
  67. public void OnLogin()
  68. {
  69. Title.text = WaitTitle;
  70. CloseAll();
  71. WaitObject.SetActive(true);
  72. }
  73. public void OnLoginFail()
  74. {
  75. Title.text = ConnectionTitle;
  76. CloseAll();
  77. ConnectionObject.SetActive(true);
  78. }
  79. #endregion Event Functions
  80. #region UI Functionality
  81. public void OnChange_ServerAddress(string serverAddress)
  82. {
  83. this.serverAddress = serverAddress;
  84. }
  85. public void OnChange_ServerPort(string serverPort)
  86. {
  87. this.serverPort = int.Parse(serverPort);
  88. }
  89. public void OnClick_Back()
  90. {
  91. UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu Client");
  92. }
  93. public void OnClick_Connect()
  94. {
  95. clientManager.StartClient(serverAddress, serverPort);
  96. PlayerPrefs.SetString("LastUsedAddress", serverAddress);
  97. PlayerPrefs.SetInt("LastUsedPort", serverPort);
  98. PlayerPrefs.Save();
  99. }
  100. public void OnClick_Done()
  101. {
  102. if(colorSelected == true && animalSelected == true && playerName != ""){
  103. clientManager.SendPlayerDetails(playerName, playerColor, playerAnimal);
  104. PlayerPrefs.SetString("LastUsedName", playerName);
  105. }
  106. }
  107. public void OnClick_Color(Color playerColor)
  108. {
  109. this.playerColor = playerColor;
  110. }
  111. public void OnClick_Colour(string hex)
  112. {
  113. Color newColor = new Color();
  114. if (ColorUtility.TryParseHtmlString(hex, out newColor)){
  115. this.playerColor = newColor;
  116. colorSelected = true;
  117. }
  118. clientManager.SendPlayerCDetails(playerColor);
  119. }
  120. public void OnClick_Animal(string playerAnimal)
  121. {
  122. this.playerAnimal = playerAnimal;
  123. animalSelected = true;
  124. clientManager.SendPlayerADetails(playerAnimal);
  125. }
  126. public void OnChange_Name(string playerName)
  127. {
  128. this.playerName = playerName;
  129. }
  130. public void OnClick_Ready()
  131. {
  132. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToLoad);
  133. }
  134. public void getPlayerColour(string playerAnimal)
  135. {
  136. Color newColor = new Color();
  137. string hex = "#000000";
  138. switch (playerAnimal)
  139. {
  140. case "Bear":
  141. hex = "#C62828";
  142. break;
  143. case "Cat":
  144. hex = "#1565C0";
  145. break;
  146. case "Cow":
  147. hex = "#EC407A";
  148. break;
  149. case "Deer":
  150. hex = "#1565C0";
  151. break;
  152. case "Dog":
  153. hex = "#1565C0";
  154. break;
  155. case "Fox":
  156. hex = "#1565C0";
  157. break;
  158. case "Goat":
  159. hex = "#1565C0";
  160. break;
  161. case "Horse":
  162. hex = "#1565C0";
  163. break;
  164. case "Lion":
  165. hex = "#1565C0";
  166. break;
  167. case "Lizard":
  168. hex = "#1565C0";
  169. break;
  170. case "Panda":
  171. hex = "#1565C0";
  172. break;
  173. case "Pig":
  174. hex = "#1565C0";
  175. break;
  176. case "Sheep":
  177. hex = "#1565C0";
  178. break;
  179. case "Turtle":
  180. hex = "#1565C0";
  181. break;
  182. case "Wolf":
  183. hex = "#1565C0";
  184. break;
  185. case "Zebra":
  186. hex = "#1565C0";
  187. break;
  188. default:
  189. break;
  190. }
  191. if (ColorUtility.TryParseHtmlString(hex, out newColor))
  192. {
  193. this.playerColor = newColor;
  194. colorSelected = true;
  195. }
  196. }
  197. #endregion UI Functionality
  198. #region Helper Functions
  199. private void CloseAll()
  200. {
  201. foreach (Transform child in Content.transform)
  202. child.gameObject.SetActive(false);
  203. }
  204. #endregion Helper Functions
  205. }