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.

145 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. #endregion Inspector Fields
  39. #region Events
  40. public System.Action OnConnectedToServer;
  41. public System.Action OnLoginSucess;
  42. public System.Action OnLoginFail;
  43. #endregion Events
  44. #region Private variables
  45. #endregion Private variables
  46. // Start is called before the first frame update
  47. private void Awake()
  48. {
  49. if (StartClientOnAwake)
  50. StartClient(ServerIP, Port);
  51. }
  52. public void OnEnable()
  53. {
  54. if (Client.isConnected)
  55. RegisterHandlers();
  56. }
  57. public void OnDisable()
  58. {
  59. if(Client.client != null)
  60. Client.client.UnregisterHandler(LoginProtocols.RequestLoginDetails);
  61. }
  62. public void StartClient(string ipAddress, int port)
  63. {
  64. Client.Connect(ipAddress, port);
  65. RegisterHandlers();
  66. }
  67. public void RegisterHandlers()
  68. {
  69. Client.client.RegisterHandler(LoginProtocols.RequestLoginDetails, ConnectedToServer);
  70. Client.client.RegisterHandler(LoginProtocols.LoginSuccess, LoginSucess);
  71. Client.client.RegisterHandler(LoginProtocols.LoginFail, LoginFail);
  72. }
  73. public void ConnectedToServer(NetworkMessage msg)
  74. {
  75. Debug.Log("Connected to Server.");
  76. OnConnectedToServer.Invoke();
  77. if (IdentifyOnConnect)
  78. SendPlayerDetails(DisplayName,PlayerColor, PlayerAnimal);
  79. }
  80. public void SendPlayerNDetails(string name)
  81. {
  82. DisplayName = name;
  83. }
  84. public void SendPlayerDetails(string name, Color color, string animal)
  85. {
  86. Debug.Log("Sending player Details.");
  87. DisplayName = name;
  88. PlayerColor = color;
  89. PlayerAnimal = animal;
  90. Client.client.Send(LoginProtocols.SendingLoginDetails, new LoginProtocols.LoginMsg(DisplayName, PlayerColor, PlayerAnimal));
  91. }
  92. public void SendPlayerADetails(string animal)
  93. {
  94. PlayerAnimal = animal;
  95. }
  96. public void SendPlayerCDetails(Color color)
  97. {
  98. PlayerColor = color;
  99. }
  100. public void LoginSucess(NetworkMessage msg)
  101. {
  102. Debug.Log("Log in successful!");
  103. OnLoginSucess.Invoke();
  104. Client.UpdatePlayerDetails(DisplayName, PlayerColor, PlayerAnimal);
  105. }
  106. public void LoginFail(NetworkMessage msg)
  107. {
  108. Debug.Log("Log in failed!");
  109. OnLoginFail.Invoke();
  110. Client.Stop();
  111. }
  112. }
  113. }