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.

139 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. client.Disconnect();
  64. }
  65. public void UpdatePlayerDetails(string DisplayName, Color PlayerColor, string PlayerAnimal)
  66. {
  67. this.PlayerAnimal = PlayerAnimal;
  68. this.DisplayName = DisplayName;
  69. this.PlayerColor = PlayerColor;
  70. }
  71. public void RecieveInventory(NetworkMessage msg)
  72. {
  73. Debug.Log("Recieving Inventory");
  74. LogicProtocols.InventoryMsg inventoryMsg;
  75. if (!msg.TryRead(out inventoryMsg))
  76. return;
  77. Inventory.SetItems(inventoryMsg.bagItems);
  78. }
  79. public void RecieveSceneChange(NetworkMessage msg)
  80. {
  81. LoginProtocols.SceneMsg sceneMsg;
  82. if (!msg.TryRead(out sceneMsg))
  83. return;
  84. //Additive or single
  85. UnityEngine.SceneManagement.LoadSceneMode mode = sceneMsg.Additive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : UnityEngine.SceneManagement.LoadSceneMode.Single;
  86. UnityEngine.SceneManagement.SceneManager.LoadScene(sceneMsg.Scene, mode);
  87. }
  88. public void OnDisconnect(NetworkMessage msg)
  89. {
  90. UnityEngine.SceneManagement.SceneManager.LoadScene(DisconnectScene);
  91. }
  92. public void UpdateTime(NetworkMessage msg)
  93. {
  94. LogicProtocols.FloatMsg floatMsg;
  95. if (!msg.TryRead(out floatMsg))
  96. return;
  97. RoundTime = floatMsg.Float;
  98. }
  99. public void UpdateScore(NetworkMessage msg)
  100. {
  101. LogicProtocols.FloatMsg floatMsg;
  102. if (!msg.TryRead(out floatMsg))
  103. return;
  104. RoundScore = floatMsg.Float;
  105. }
  106. }
  107. }