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.

154 lines
4.7 KiB

  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. namespace Networking.Client
  4. {
  5. [CreateAssetMenu(menuName = "Major Project/Networking/ClientObject", order = 150)]
  6. public class ClientObject : ScriptableObject
  7. {
  8. #region Inspector Fields
  9. [Header("Connection Settings")]
  10. [SerializeField]
  11. [Tooltip("Server Address to Connect to")]
  12. private string ServerIP;
  13. [SerializeField]
  14. [Tooltip("Port to connect on")]
  15. private int Port;
  16. [SerializeField]
  17. [Tooltip("Scene to return to if disconnected from server")]
  18. private string DisconnectScene;
  19. [Header("Player Settings")]
  20. [SerializeField]
  21. [Tooltip("Player Name")]
  22. private string DisplayName;
  23. [SerializeField]
  24. [Tooltip("Player Animal")]
  25. public string PlayerAnimal;
  26. [SerializeField]
  27. [Tooltip("Player Color")]
  28. public Color PlayerColor;
  29. [Header("Run Time Settings")]
  30. [SerializeField]
  31. [Tooltip("Client Inventory")]
  32. protected Inventory Inventory;
  33. [SerializeField]
  34. [Tooltip("Current Time in Round")]
  35. public float RoundTime;
  36. [SerializeField]
  37. [Tooltip("Current Score in Round")]
  38. public float RoundScore;
  39. [SerializeField]
  40. [Tooltip("Remaining Lives")]
  41. public float RemainingLives;
  42. [SerializeField]
  43. [Tooltip("A list of all connected clients")]
  44. public ConnectedClients ConnectedClients;
  45. #endregion Inspector Fields
  46. #region ReadOnly Variables
  47. public NetworkClient client { get; private set; }
  48. public bool isConnected { get { return client != null && client.isConnected; } }
  49. #endregion ReadOnly Variables
  50. public void Connect(string serverAddress,int port)
  51. {
  52. Debug.Log("Connecting to server: " + serverAddress + ", " + port);
  53. this.ServerIP = serverAddress;
  54. this.Port = port;
  55. client = new NetworkClient();
  56. client.Configure(TransportConfigure.CreateConfigure(), 1);
  57. ConnectedClients.SetUp(this);
  58. client.Connect(serverAddress, port);
  59. Inventory.Reset();
  60. client.RegisterHandler(LogicProtocols.SendInventory, RecieveInventory);
  61. client.RegisterHandler(LoginProtocols.SceneChange, RecieveSceneChange);
  62. client.RegisterHandler(MsgType.Disconnect, OnDisconnect);
  63. client.RegisterHandler(LogicProtocols.SendRoundTime, UpdateTime);
  64. client.RegisterHandler(LogicProtocols.SendScore, UpdateScore);
  65. client.RegisterHandler(LogicProtocols.SendLives, UpdateLives);
  66. }
  67. public void Stop()
  68. {
  69. this.Port = 0;
  70. this.ServerIP = "";
  71. client.Disconnect();
  72. }
  73. public void UpdatePlayerDetails(string DisplayName, Color PlayerColor, string PlayerAnimal)
  74. {
  75. this.PlayerAnimal = PlayerAnimal;
  76. this.DisplayName = DisplayName;
  77. this.PlayerColor = PlayerColor;
  78. }
  79. public void RecieveInventory(NetworkMessage msg)
  80. {
  81. Debug.Log("Recieving Inventory");
  82. LogicProtocols.InventoryMsg inventoryMsg;
  83. if (!msg.TryRead(out inventoryMsg))
  84. return;
  85. Inventory.SetItems(inventoryMsg.bagItems);
  86. }
  87. public void RecieveSceneChange(NetworkMessage msg)
  88. {
  89. LoginProtocols.SceneMsg sceneMsg;
  90. if (!msg.TryRead(out sceneMsg))
  91. return;
  92. //Additive or single
  93. UnityEngine.SceneManagement.LoadSceneMode mode = sceneMsg.Additive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : UnityEngine.SceneManagement.LoadSceneMode.Single;
  94. UnityEngine.SceneManagement.SceneManager.LoadScene(sceneMsg.Scene, mode);
  95. }
  96. public void OnDisconnect(NetworkMessage msg)
  97. {
  98. UnityEngine.SceneManagement.SceneManager.LoadScene(DisconnectScene);
  99. ConnectedClients.Reset();
  100. }
  101. public void UpdateTime(NetworkMessage msg)
  102. {
  103. LogicProtocols.FloatMsg floatMsg;
  104. if (!msg.TryRead(out floatMsg))
  105. return;
  106. RoundTime = floatMsg.Float;
  107. }
  108. public void UpdateScore(NetworkMessage msg)
  109. {
  110. LogicProtocols.FloatMsg floatMsg;
  111. if (!msg.TryRead(out floatMsg))
  112. return;
  113. RoundScore = floatMsg.Float;
  114. }
  115. public void UpdateLives(NetworkMessage msg)
  116. {
  117. LogicProtocols.FloatMsg floatMsg;
  118. if (!msg.TryRead(out floatMsg))
  119. return;
  120. RemainingLives = floatMsg.Float;
  121. }
  122. }
  123. }