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.

149 lines
4.3 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. private string PlayerAnimal;
  28. [SerializeField]
  29. [Tooltip("Player Color")]
  30. private 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("A list of all connected clients")]
  43. public ConnectedClients ConnectedClients;
  44. #endregion Inspector Fields
  45. #region ReadOnly Variables
  46. public NetworkClient client { get; private set; }
  47. public bool isConnected { get { return client != null && client.isConnected; } }
  48. #endregion ReadOnly Variables
  49. public void Connect(string serverAddress,int port)
  50. {
  51. Debug.Log("Connecting to server: " + serverAddress + ", " + port);
  52. this.ServerIP = serverAddress;
  53. this.Port = port;
  54. client = new NetworkClient();
  55. client.Configure(TransportConfigure.CreateConfigure(), 1);
  56. ConnectedClients.SetUp(this);
  57. client.Connect(serverAddress, port);
  58. Inventory.Reset();
  59. client.RegisterHandler(LogicProtocols.SendInventory, RecieveInventory);
  60. client.RegisterHandler(LoginProtocols.SceneChange, RecieveSceneChange);
  61. client.RegisterHandler(MsgType.Disconnect, OnDisconnect);
  62. client.RegisterHandler(LogicProtocols.SendRoundTime, UpdateTime);
  63. client.RegisterHandler(LogicProtocols.SendScore, UpdateScore);
  64. }
  65. public void Stop()
  66. {
  67. this.Port = 0;
  68. this.ServerIP = "";
  69. client.Disconnect();
  70. }
  71. public void UpdatePlayerDetails(string DisplayName, Color PlayerColor, string PlayerAnimal)
  72. {
  73. this.PlayerAnimal = PlayerAnimal;
  74. this.DisplayName = DisplayName;
  75. this.PlayerColor = PlayerColor;
  76. }
  77. public void RecieveInventory(NetworkMessage msg)
  78. {
  79. Debug.Log("Recieving Inventory");
  80. LogicProtocols.InventoryMsg inventoryMsg;
  81. if (!msg.TryRead(out inventoryMsg))
  82. return;
  83. Inventory.SetItems(inventoryMsg.bagItems);
  84. }
  85. public void RecieveSceneChange(NetworkMessage msg)
  86. {
  87. LoginProtocols.SceneMsg sceneMsg;
  88. if (!msg.TryRead(out sceneMsg))
  89. return;
  90. //Additive or single
  91. UnityEngine.SceneManagement.LoadSceneMode mode = sceneMsg.Additive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : UnityEngine.SceneManagement.LoadSceneMode.Single;
  92. UnityEngine.SceneManagement.SceneManager.LoadScene(sceneMsg.Scene, mode);
  93. }
  94. public void OnDisconnect(NetworkMessage msg)
  95. {
  96. UnityEngine.SceneManagement.SceneManager.LoadScene(DisconnectScene);
  97. ConnectedClients.Reset();
  98. }
  99. public void UpdateTime(NetworkMessage msg)
  100. {
  101. LogicProtocols.FloatMsg floatMsg;
  102. if (!msg.TryRead(out floatMsg))
  103. return;
  104. RoundTime = floatMsg.Float;
  105. }
  106. public void UpdateScore(NetworkMessage msg)
  107. {
  108. LogicProtocols.FloatMsg floatMsg;
  109. if (!msg.TryRead(out floatMsg))
  110. return;
  111. RoundScore = floatMsg.Float;
  112. }
  113. }
  114. }