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.

158 lines
4.7 KiB

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