Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.Networking;
  4.  
  5. public class clientController : NetworkBehaviour
  6. {
  7.  
  8. public GameObject clientCCPrefab;
  9. public CharacterController clientCC;
  10.  
  11. private serverController serverControllerScript;
  12. private playerInput playerInputScript;
  13.  
  14. public Vector2 inputVector;
  15. public int inputModifier;
  16.  
  17. public int curSpeed;
  18.  
  19. private Dictionary<int, Vector2> clientInputs = new Dictionary<int, Vector2>();
  20. private Dictionary<int, int> clientModifierInputs = new Dictionary<int, int>();
  21. private Dictionary<int, Vector2> clientPositions = new Dictionary<int, Vector2>();
  22.  
  23. public List<Vector4> clientPacket = new List<Vector4>();
  24. public Vector4[] clientPacketArray = new Vector4[5];
  25.  
  26. public Vector2 serverResultPos;
  27. public int serverResultTick;
  28.  
  29. public int clientTick;
  30.  
  31. public override void OnStartLocalPlayer()
  32. {
  33. playerInputScript = GetComponentInChildren<playerInput>();
  34. }
  35.  
  36. void Start()
  37. {
  38. serverControllerScript = GetComponent<serverController>();
  39.  
  40. if (isLocalPlayer)
  41. {
  42. GameObject cloneCC = (GameObject)Instantiate(clientCCPrefab, Vector2.zero, transform.rotation);
  43. clientCC = cloneCC.GetComponent<CharacterController>();
  44. }
  45.  
  46. curSpeed = 2;
  47. }
  48.  
  49. void FixedUpdate()
  50. {
  51. clientTick++;
  52.  
  53. if (isLocalPlayer)
  54. {
  55. //Send packet to server every 0.1s;
  56.  
  57. //Fixed a bug where packets went missing because of the if statement around this and inside SendPacketToServer itself
  58. SendPacketToServer();
  59.  
  60. //Grab inputs from input script;
  61. inputVector = playerInputScript.inputVector;
  62. inputModifier = playerInputScript.shift ? 1 : 0;
  63.  
  64. LogClientPlayer(inputVector, inputModifier);
  65. LocalMove();
  66. }
  67. }
  68.  
  69. //Create Packets
  70. void LogClientPlayer(Vector2 input, int modifier)
  71. {
  72. clientInputs.Add(clientTick, input);
  73. clientModifierInputs.Add(clientTick, modifier);
  74. clientPositions.Add(clientTick, new Vector2(clientCC.gameObject.transform.position.x, clientCC.gameObject.transform.position.y));
  75.  
  76. if (clientPacket.Count < 5)
  77. {
  78. Vector4 curClientTickInput = new Vector4(input.x, input.y, modifier, clientTick);
  79. clientPacket.Add(curClientTickInput);
  80. }
  81. else
  82. {
  83. Debug.Log("Missing some inputs");
  84. }
  85. }
  86.  
  87. void LocalMove()
  88. {
  89. if (clientInputs.ContainsKey(clientTick))
  90. {
  91. //This is where the movement bug was, used to be clientPositions[clientTick], that's the kind of mistake programmers make in a hurry
  92. clientCC.Move(clientInputs[clientTick] * Time.fixedDeltaTime * curSpeed);
  93. transform.position = clientCC.gameObject.transform.position;
  94. }
  95. }
  96.  
  97. void SendPacketToServer()
  98. {
  99. if (clientPacket.Count == 5)
  100. {
  101. clientPacketArray = clientPacket.ToArray();
  102.  
  103. //call command on serverControllerScript;
  104. CmdPacketSync(clientPacketArray);
  105.  
  106. //Clear packet queue for next packet;
  107. clientPacket.Clear();
  108. }
  109. }
  110.  
  111. [Command]
  112. void CmdPacketSync(Vector4[] packet)
  113. {
  114. if (isServer)
  115. {
  116. serverControllerScript.PacketSync(packet);
  117. }
  118. }
  119.  
  120. public void ApplyResult(Vector3 result)
  121. {
  122. serverResultPos = new Vector2(result.x, result.y);
  123. serverResultTick = (int)result.z;
  124.  
  125. clientCC.gameObject.transform.position = serverResultPos;
  126.  
  127. //Clear list of old inputs/positions;
  128. List<int> removeKeys = new List<int>();
  129.  
  130. foreach (KeyValuePair<int, Vector2> pair in clientInputs)
  131. {
  132. if (pair.Key <= serverResultTick)
  133. {
  134. removeKeys.Add(pair.Key);
  135. }
  136. }
  137.  
  138. foreach (int key in removeKeys)
  139. {
  140. clientInputs.Remove(key);
  141. clientPositions.Remove(key);
  142. clientModifierInputs.Remove(key);
  143. }
  144.  
  145. foreach (KeyValuePair<int, Vector2> input in clientInputs)
  146. {
  147. clientCC.Move(input.Value * Time.fixedDeltaTime * curSpeed);
  148. transform.position = clientCC.gameObject.transform.position;
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement