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.

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