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.

144 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. Client.client.UnregisterHandler(LoginProtocols.RequestLoginDetails);
  60. }
  61. public void StartClient(string ipAddress, int port)
  62. {
  63. Client.Connect(ipAddress, port);
  64. RegisterHandlers();
  65. }
  66. public void RegisterHandlers()
  67. {
  68. Client.client.RegisterHandler(LoginProtocols.RequestLoginDetails, ConnectedToServer);
  69. Client.client.RegisterHandler(LoginProtocols.LoginSuccess, LoginSucess);
  70. Client.client.RegisterHandler(LoginProtocols.LoginFail, LoginFail);
  71. }
  72. public void ConnectedToServer(NetworkMessage msg)
  73. {
  74. Debug.Log("Connected to Server.");
  75. OnConnectedToServer.Invoke();
  76. if (IdentifyOnConnect)
  77. SendPlayerDetails(DisplayName,PlayerColor, PlayerAnimal);
  78. }
  79. public void SendPlayerNDetails(string name)
  80. {
  81. DisplayName = name;
  82. }
  83. public void SendPlayerDetails(string name, Color color, string animal)
  84. {
  85. Debug.Log("Sending player Details.");
  86. DisplayName = name;
  87. PlayerColor = color;
  88. PlayerAnimal = animal;
  89. Client.client.Send(LoginProtocols.SendingLoginDetails, new LoginProtocols.LoginMsg(DisplayName, PlayerColor, PlayerAnimal));
  90. }
  91. public void SendPlayerADetails(string animal)
  92. {
  93. PlayerAnimal = animal;
  94. }
  95. public void SendPlayerCDetails(Color color)
  96. {
  97. PlayerColor = color;
  98. }
  99. public void LoginSucess(NetworkMessage msg)
  100. {
  101. Debug.Log("Log in successful!");
  102. OnLoginSucess.Invoke();
  103. Client.UpdatePlayerDetails(DisplayName, PlayerColor, PlayerAnimal);
  104. }
  105. public void LoginFail(NetworkMessage msg)
  106. {
  107. Debug.Log("Log in failed!");
  108. OnLoginFail.Invoke();
  109. Client.Stop();
  110. }
  111. }
  112. }