Exemplo n.º 1
0
        public MovementInput[] GetToLatest(int startID)
        {
            int length = currentID - startID;

            MovementInput[] result = new MovementInput[length];

            for (int i = 0; i < length; i++)
            {
                result[i] = history[(startID + i) % historySize];
            }

            if (result.Length > 0)
            {
                if (result[0].id != startID)
                {
                    throw new Exception("Bad code! Exprected " + startID + " got " + result[0].id);
                }

                if (result[result.Length - 1].id != currentID - 1)
                {
                    throw new Exception("Bad code! Exprected " + (currentID - 1) + " got " + result[result.Length - 1].id);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public MovementInput GetMove(int id)
        {
            int           index  = id % historySize;
            MovementInput result = history[index];

            if (result.id != id)
            {
                Debug.LogWarning("Looking for input history with ID " + id + ", found " + result.id + ". You may need to increase the history size.");
            }

            return(result);
        }
Exemplo n.º 3
0
        public int Move(Vector2 move)
        {
            MovementInput input = new MovementInput
            {
                move = move,
                id   = currentID,
                time = Time.time
            };

            int index = currentID % historySize;

            history[index] = input;

            currentID++;

            return(input.id);
        }
Exemplo n.º 4
0
        public void ReceiveUpdate(Vector2 position, int id)
        {
            if (id != latestAcknowledgedInput.id + 1)
            {
                Debug.LogWarning("Packet received out of order! Expecting " + (latestAcknowledgedInput.id + 1) + ", got " + id);
            }

            // make sure we didn't get an update out of order
            if (latestAcknowledgedInput.id >= id)
            {
                return;
            }

            Debug.DrawLine(debugServerPos, position, Color.green, 5);
            debugServerPos = position;

            latestAcknowledgedInput = history.GetMove(id);

            // replay all inputs that occured after this state update
            MovementInput[] inputs   = history.GetToLatest(id);
            MovementInput   previous = history.GetMove(id - 1);

            for (int i = 0; i < inputs.Length; i++)
            {
                //float deltaTime = inputs[i].time - previous.time;
                float deltaTime = movementDelta;
                position += inputs[i].move * speed * deltaTime;
                previous  = inputs[i];
            }

            // add the input that hasen't been sent to the server at all yet
            // disabled cuz it doesn't really seem to do anything, but maybe this is a good idea for later
            //position += cumulativeInput*speed*(Time.time - previous.time);

            if (smoothCorrections)
            {
                correction = position - (Vector2)transform.position;
                Debug.DrawRay(transform.position, correction, Color.red, 5);
            }
            else
            {
                transform.position = position;
            }
        }
Exemplo n.º 5
0
 private void Awake()
 {
     latestAcknowledgedInput = new MovementInput {
         id = -1
     };
 }