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.

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