Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. using System;
  6.  
  7.  
  8.  
  9. namespace Client
  10. {
  11. public class ClientHandle : MonoBehaviour
  12. {
  13. public static ClientHandle instance;
  14.  
  15. private delegate void NetPackets(byte[] data);
  16. private static Dictionary<int, NetPackets> _packets;
  17.  
  18. [Header("Scripts")]
  19. public MenuManager menuManager;
  20.  
  21.  
  22. [Header("Player Prefab")]
  23. public GameObject _playerPref;
  24.  
  25. public GameObject Char;
  26.  
  27. public Button button;
  28.  
  29. public static GameObject PlayerList;
  30.  
  31. public void InitializeNetworkPackages()
  32. {
  33. //Debug.Log("Initialize Network Packages");
  34. _packets = new Dictionary<int, NetPackets>
  35. {
  36. //LIST OF SERVER PACKAGES
  37. { (int)Packets.Message, HandleMessage },
  38. { (int)Packets.Spawn, SpawnCharacter },
  39. { (int)Packets.Login_Accept, HandleLoginSucces },
  40. { (int)Packets.Movement, HandleClientMovement },
  41. { (int)Packets.TEST, HandleTEST },
  42.  
  43. };
  44. }
  45.  
  46. public void Awake()
  47. {
  48. instance = this;
  49. InitializeNetworkPackages();
  50. PlayerList = new GameObject("PlayerList");
  51. }
  52.  
  53. public void HandleSocketData(byte[] data)
  54. {
  55. Buffer buffer = new Buffer();
  56. NetPackets _packet;
  57. buffer.WriteBytes(data);
  58. int packet = buffer.ReadInteger();
  59. buffer.Dispose();
  60.  
  61. if (packet == 0)
  62. return;
  63.  
  64. if (_packets.TryGetValue(packet, out _packet))
  65. {
  66. _packet.Invoke(data);
  67. }
  68. }
  69.  
  70. public void HandleMessage(byte[] data)
  71. {
  72. Buffer buffer = new Buffer();
  73. buffer.WriteBytes(data);
  74. int packet = buffer.ReadInteger();
  75. string AlertMsg = buffer.ReadString();
  76. Debug.Log(AlertMsg);
  77.  
  78. if (packet == 0)
  79. return;
  80. }
  81.  
  82. public void HandleLoginSucces(byte[] data)
  83. {
  84. Buffer buffer = new Buffer();
  85. buffer.WriteBytes(data);
  86. int packet = buffer.ReadInteger();
  87. string msg = buffer.ReadString();
  88. Debug.Log(msg);
  89. MenuManager.instance.ChangeMenu(2);
  90.  
  91. if (packet == 0)
  92. return;
  93. }
  94.  
  95. public void HandleClientMovement(byte[] data)
  96. {
  97. Buffer buffer = new Buffer();
  98. buffer.WriteBytes(data);
  99. int packet = buffer.ReadInteger();
  100.  
  101. // Reciving Player Information
  102. int index = buffer.ReadInteger();
  103.  
  104. // Reciving Player Position
  105. float x = buffer.ReadFloat();
  106. float y = buffer.ReadFloat();
  107. float z = buffer.ReadFloat();
  108.  
  109. // Reciving Player Rotation
  110. float rotX = buffer.ReadFloat();
  111. float rotY = buffer.ReadFloat();
  112. float rotZ = buffer.ReadFloat();
  113. float rotW = buffer.ReadFloat();
  114.  
  115. // Find Player and move it.
  116. GameObject _netPlayer;
  117. if (_netPlayer = GameObject.Find("Player (" + index + ")"))
  118. {
  119. _netPlayer.GetComponent<Transform>().position = new Vector3(x, y, z);
  120. _netPlayer.GetComponent<Transform>().rotation = new Quaternion(rotX, rotY, rotZ, rotW);
  121. }
  122.  
  123. if (packet == 0) return;
  124. }
  125.  
  126.  
  127.  
  128. //public void HandleTEST(byte[] data)
  129. //{
  130. // int i = 0;
  131.  
  132. // Buffer buffer = new Buffer();
  133. // buffer.WriteBytes(data);
  134. // int packet = buffer.ReadInteger();
  135. // Character[] charArray = new Character[5];
  136. // for (int j = 0; j < 5; j++)
  137. // {
  138. // charArray[j] = new Character();
  139. // // charArray[j].ID = 0;
  140. // }
  141. // while (buffer.Length() > 0)
  142. // {
  143. // Debug.Log("Aye");
  144. // charArray[i].ID = buffer.ReadInteger();
  145. // charArray[i].Name = buffer.ReadString();
  146. // i++;
  147. // }
  148.  
  149. // for (int j = 0; j < 5; j++)
  150. // {
  151. // if (charArray[j].ID > 0)
  152. // {
  153. // Debug.Log("ID: " + charArray[j].ID + " Name: " + charArray[j].Name);
  154. // }
  155. // else
  156. // {
  157. // Debug.Log("No characters");
  158. // }
  159.  
  160. // }
  161.  
  162. // if (packet == 0) return;
  163.  
  164. // }
  165.  
  166.  
  167. public void HandleTEST(byte[] data)
  168. {
  169. int i = 0;
  170.  
  171. Buffer buffer = new Buffer();
  172. buffer.WriteBytes(data);
  173. int packet = buffer.ReadInteger();
  174. Character[] charArray = new Character[5];
  175. for (int j = 0; j < 5; j++)
  176. {
  177. charArray[j] = new Character();
  178. charArray[j].ID = 0;
  179. }
  180.  
  181. Debug.Log("Before While");
  182. while (buffer.Length() > 0)
  183. {
  184. Debug.Log("In While" + buffer.GetReadPos());
  185. charArray[i].ID = buffer.ReadInteger();
  186. charArray[i].Name = buffer.ReadString();
  187. i++;
  188. Debug.Log("End While");
  189. }
  190. Debug.Log("After While");
  191.  
  192. for (int j = 0; j < 5; j++)
  193. {
  194. if (charArray[j].ID >= 0)
  195. {
  196. Debug.Log("ID: " + charArray[j].ID + " Name: " + charArray[j].Name);
  197. }
  198. else
  199. {
  200. Debug.Log("No characters");
  201. }
  202.  
  203. }
  204.  
  205. if (packet == 0) return;
  206.  
  207. }
  208.  
  209. public void SpawnCharacter(byte[] data)
  210. {
  211. Buffer buffer = new Buffer();
  212. buffer.WriteBytes(data);
  213. int packet = buffer.ReadInteger();
  214. int PlayerID = buffer.ReadInteger();
  215. int CharID = buffer.ReadInteger();
  216.  
  217. if (Globals.Client_ID <= 1) { Globals.Client_ID = PlayerID; }
  218.  
  219. // MENU WINDOW DOWN
  220. MenuManager.instance.ChangeMenu(4);
  221.  
  222. Character.character[PlayerID].ID = CharID;
  223. //General.instance.SetPlayerX(PlayerID, buffer.ReadFloat());
  224. //General.instance.SetPlayerY(PlayerID, buffer.ReadFloat());
  225. //General.instance.SetPlayerZ(PlayerID, buffer.ReadFloat());
  226. General.instance.SetVector3(PlayerID, buffer.ReadFloat(), buffer.ReadFloat(), buffer.ReadFloat());
  227.  
  228. Character.character[PlayerID].Name = buffer.ReadString();
  229.  
  230. PlayerInfo.instance.Name.text = Character.character[PlayerID].Name;
  231.  
  232. Spawn(PlayerID);
  233.  
  234. if (packet == 0) return;
  235. }
  236.  
  237.  
  238. void Spawn(int PlayerID)
  239. {
  240. // Instantiate the other players on their position.
  241. _playerPref = Instantiate(_playerPref, new Vector3(General.instance.GetPlayerX(PlayerID), General.instance.GetPlayerY(PlayerID), General.instance.GetPlayerZ(PlayerID)), Quaternion.identity);
  242. _playerPref.GetComponent<NetPlayer>().Index = PlayerID;
  243. _playerPref.name = "Player (" + PlayerID + ")";
  244. _playerPref.transform.parent = GameObject.Find("PlayerList").transform;
  245. }
  246.  
  247. }
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement