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.

128 lines
3.3 KiB

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 Color")]
  26. private Color PlayerColor;
  27. [Header("References")]
  28. [SerializeField]
  29. [Tooltip("Reference to actual Client")]
  30. protected ClientObject Client;
  31. [Header("Debug")]
  32. [SerializeField]
  33. [Tooltip("Send prefilled Player settings after connecting")]
  34. private bool IdentifyOnConnect = false;
  35. #endregion Inspector Fields
  36. #region Events
  37. public System.Action OnConnectedToServer;
  38. public System.Action OnLoginSucess;
  39. public System.Action OnLoginFail;
  40. #endregion Events
  41. #region Private variables
  42. #endregion Private variables
  43. // Start is called before the first frame update
  44. private void Awake()
  45. {
  46. if (StartClientOnAwake)
  47. StartClient(ServerIP, Port);
  48. }
  49. public void OnEnable()
  50. {
  51. if (Client.isConnected)
  52. RegisterHandlers();
  53. }
  54. public void OnDisable()
  55. {
  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);
  75. }
  76. public void SendPlayerDetails(string name, Color color)
  77. {
  78. Debug.Log("Sending player Details.");
  79. DisplayName = name;
  80. PlayerColor = color;
  81. Client.client.Send(LoginProtocols.SendingLoginDetails, new LoginProtocols.LoginMsg(DisplayName, PlayerColor));
  82. }
  83. public void LoginSucess(NetworkMessage msg)
  84. {
  85. Debug.Log("Log in successful!");
  86. OnLoginSucess.Invoke();
  87. Client.UpdatePlayerDetails(DisplayName, PlayerColor);
  88. }
  89. public void LoginFail(NetworkMessage msg)
  90. {
  91. Debug.Log("Log in failed!");
  92. OnLoginFail.Invoke();
  93. Client.Stop();
  94. }
  95. }
  96. }