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.

244 lines
5.8 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 = false;
  38. bool colorSelected = false;
  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_Animal(string playerAnimal)
  108. {
  109. this.playerAnimal = playerAnimal;
  110. animalSelected = true;
  111. getPlayerColour(playerAnimal);
  112. clientManager.SendPlayerADetails(playerAnimal);
  113. }
  114. public void OnChange_Name(string playerName)
  115. {
  116. this.playerName = playerName;
  117. }
  118. public void OnClick_Ready()
  119. {
  120. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToLoad);
  121. }
  122. public void getPlayerColour(string playerAnimal)
  123. {
  124. Color newColor = new Color();
  125. string hex = "#000000";
  126. switch (playerAnimal)
  127. {
  128. case "Bear":
  129. //red
  130. hex = "#FF0000";
  131. break;
  132. case "Cat":
  133. //sky blue
  134. hex = "#008BFF";
  135. break;
  136. case "Cow":
  137. //yellow
  138. hex = "#FFFF00";
  139. break;
  140. case "Deer":
  141. //purpe/pink
  142. hex = "#B200FF";
  143. break;
  144. case "Dog":
  145. //purple
  146. hex = "#6700FF";
  147. break;
  148. case "Fox":
  149. //orange
  150. hex = "#FF7B00";
  151. break;
  152. case "Goat":
  153. //cyan
  154. hex = "#00FFFF";
  155. break;
  156. case "Horse":
  157. //dark blue
  158. hex = "#0000FF";
  159. break;
  160. case "Lion":
  161. //lime
  162. hex = "#00FF00";
  163. break;
  164. case "Lizard":
  165. //light green
  166. hex = "#7AFF00";
  167. break;
  168. case "Panda":
  169. //salmon
  170. hex = "#FFA07A";
  171. break;
  172. case "Pig":
  173. //pink
  174. hex = "#FF00DA";
  175. break;
  176. case "Sheep":
  177. //maroon
  178. hex = "#800000";
  179. break;
  180. case "Turtle":
  181. //dark green
  182. hex = "#008000";
  183. break;
  184. case "Wolf":
  185. //grey
  186. hex = "#808080";
  187. break;
  188. case "Zebra":
  189. //black
  190. hex = "#000000";
  191. break;
  192. default:
  193. break;
  194. }
  195. if (ColorUtility.TryParseHtmlString(hex, out newColor))
  196. {
  197. this.playerColor = newColor;
  198. colorSelected = true;
  199. }
  200. }
  201. #endregion UI Functionality
  202. #region Helper Functions
  203. private void CloseAll()
  204. {
  205. foreach (Transform child in Content.transform)
  206. child.gameObject.SetActive(false);
  207. }
  208. #endregion Helper Functions
  209. }