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.

134 lines
3.6 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
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. namespace Networking.Client
  4. {
  5. public class ClientLoginManager : MonoBehaviour
  6. {
  7. #region Inspector Fields
  8. [Header("Connection Settings")]
  9. [SerializeField]
  10. [Tooltip("Server Address to Connect to")]
  11. public string ServerIP;
  12. [SerializeField]
  13. [Tooltip("Port to connect on")]
  14. public int Port;
  15. [SerializeField]
  16. [Tooltip("Try and connect on Play")]
  17. private bool StartClientOnAwake;
  18. [Header("Player Settings")]
  19. [SerializeField]
  20. [Tooltip("Player Name")]
  21. private string DisplayName;
  22. [SerializeField]
  23. [Tooltip("Player Animal")]
  24. private string PlayerAnimal;
  25. [SerializeField]
  26. [Tooltip("Player Color")]
  27. private Color PlayerColor;
  28. [Header("References")]
  29. [SerializeField]
  30. [Tooltip("Reference to actual Client")]
  31. protected ClientObject Client;
  32. [Header("Debug")]
  33. [SerializeField]
  34. [Tooltip("Send prefilled Player settings after connecting")]
  35. private bool IdentifyOnConnect = false;
  36. ConnectedClients clients;
  37. #endregion Inspector Fields
  38. #region Events
  39. public System.Action OnConnectedToServer;
  40. public System.Action OnLoginSucess;
  41. public System.Action OnLoginFail;
  42. #endregion Events
  43. private void Awake()
  44. {
  45. if (StartClientOnAwake)
  46. StartClient(ServerIP, Port);
  47. }
  48. public void OnEnable()
  49. {
  50. if (Client.isConnected)
  51. RegisterHandlers();
  52. }
  53. public void OnDisable()
  54. {
  55. if(Client.client != null)
  56. Client.client.UnregisterHandler(LoginProtocols.RequestLoginDetails);
  57. }
  58. public void StartClient(string ipAddress, int port)
  59. {
  60. Client.Connect(ipAddress, Port);
  61. RegisterHandlers();
  62. }
  63. public void RegisterHandlers()
  64. {
  65. Client.client.RegisterHandler(LoginProtocols.RequestLoginDetails, ConnectedToServer);
  66. Client.client.RegisterHandler(LoginProtocols.LoginSuccess, LoginSucess);
  67. Client.client.RegisterHandler(LoginProtocols.LoginFail, LoginFail);
  68. }
  69. public void ConnectedToServer(NetworkMessage msg)
  70. {
  71. Debug.Log("Connected to Server.");
  72. OnConnectedToServer.Invoke();
  73. if (IdentifyOnConnect)
  74. SendPlayerDetails(DisplayName,PlayerColor, PlayerAnimal);
  75. }
  76. public void SendPlayerNDetails(string name)
  77. {
  78. DisplayName = name;
  79. }
  80. public void SendPlayerDetails(string name, Color color, string animal)
  81. {
  82. Debug.Log("Sending player Details.");
  83. DisplayName = name;
  84. PlayerColor = color;
  85. PlayerAnimal = animal;
  86. Client.client.Send(LoginProtocols.SendingLoginDetails, new LoginProtocols.LoginMsg(DisplayName, PlayerColor, PlayerAnimal));
  87. }
  88. public void SendPlayerADetails(string animal)
  89. {
  90. PlayerAnimal = animal;
  91. }
  92. public void SendPlayerCDetails(Color color)
  93. {
  94. PlayerColor = color;
  95. }
  96. public void LoginSucess(NetworkMessage msg)
  97. {
  98. Debug.Log("Log in successful!");
  99. OnLoginSucess.Invoke();
  100. Client.UpdatePlayerDetails(DisplayName, PlayerColor, PlayerAnimal);
  101. }
  102. public void LoginFail(NetworkMessage msg)
  103. {
  104. Debug.Log("Log in failed!");
  105. OnLoginFail.Invoke();
  106. Client.Stop();
  107. }
  108. }
  109. }