Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.Networking;
- public class clientController : NetworkBehaviour
- {
- public GameObject clientCCPrefab;
- public CharacterController clientCC;
- private serverController serverControllerScript;
- private playerInput playerInputScript;
- public Vector2 inputVector;
- public int inputModifier;
- public int curSpeed;
- private Dictionary<int, Vector2> clientInputs = new Dictionary<int, Vector2>();
- private Dictionary<int, int> clientModifierInputs = new Dictionary<int, int>();
- private Dictionary<int, Vector2> clientPositions = new Dictionary<int, Vector2>();
- public List<Vector4> clientPacket = new List<Vector4>();
- public Vector4[] clientPacketArray = new Vector4[5];
- public Vector2 serverResultPos;
- public int serverResultTick;
- public int clientTick;
- public override void OnStartLocalPlayer()
- {
- playerInputScript = GetComponentInChildren<playerInput>();
- }
- void Start()
- {
- serverControllerScript = GetComponent<serverController>();
- if (isLocalPlayer)
- {
- GameObject cloneCC = (GameObject)Instantiate(clientCCPrefab, Vector2.zero, transform.rotation);
- clientCC = cloneCC.GetComponent<CharacterController>();
- }
- curSpeed = 2;
- }
- void FixedUpdate()
- {
- clientTick++;
- if (isLocalPlayer)
- {
- //Send packet to server every 0.1s;
- //Fixed a bug where packets went missing because of the if statement around this and inside SendPacketToServer itself
- SendPacketToServer();
- //Grab inputs from input script;
- inputVector = playerInputScript.inputVector;
- inputModifier = playerInputScript.shift ? 1 : 0;
- LogClientPlayer(inputVector, inputModifier);
- LocalMove();
- }
- }
- //Create Packets
- void LogClientPlayer(Vector2 input, int modifier)
- {
- clientInputs.Add(clientTick, input);
- clientModifierInputs.Add(clientTick, modifier);
- clientPositions.Add(clientTick, new Vector2(clientCC.gameObject.transform.position.x, clientCC.gameObject.transform.position.y));
- if (clientPacket.Count < 5)
- {
- Vector4 curClientTickInput = new Vector4(input.x, input.y, modifier, clientTick);
- clientPacket.Add(curClientTickInput);
- }
- else
- {
- Debug.Log("Missing some inputs");
- }
- }
- void LocalMove()
- {
- if (clientInputs.ContainsKey(clientTick))
- {
- //This is where the movement bug was, used to be clientPositions[clientTick], that's the kind of mistake programmers make in a hurry
- clientCC.Move(clientInputs[clientTick] * Time.fixedDeltaTime * curSpeed);
- transform.position = clientCC.gameObject.transform.position;
- }
- }
- void SendPacketToServer()
- {
- if (clientPacket.Count == 5)
- {
- clientPacketArray = clientPacket.ToArray();
- //call command on serverControllerScript;
- CmdPacketSync(clientPacketArray);
- //Clear packet queue for next packet;
- clientPacket.Clear();
- }
- }
- [Command]
- void CmdPacketSync(Vector4[] packet)
- {
- if (isServer)
- {
- serverControllerScript.PacketSync(packet);
- }
- }
- public void ApplyResult(Vector3 result)
- {
- serverResultPos = new Vector2(result.x, result.y);
- serverResultTick = (int)result.z;
- clientCC.gameObject.transform.position = serverResultPos;
- //Clear list of old inputs/positions;
- List<int> removeKeys = new List<int>();
- foreach (KeyValuePair<int, Vector2> pair in clientInputs)
- {
- if (pair.Key <= serverResultTick)
- {
- removeKeys.Add(pair.Key);
- }
- }
- foreach (int key in removeKeys)
- {
- clientInputs.Remove(key);
- clientPositions.Remove(key);
- clientModifierInputs.Remove(key);
- }
- foreach (KeyValuePair<int, Vector2> input in clientInputs)
- {
- clientCC.Move(input.Value * Time.fixedDeltaTime * curSpeed);
- transform.position = clientCC.gameObject.transform.position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement