Esempio n. 1
0
        public void OnPlayerState(PlayerState state)
        {
            //old command
            int diff = NetworkGeneral.SeqDiff(state.Tick, _buffer.Last.Tick);

            if (diff <= 0)
            {
                return;
            }

            //Debug.Log($"OnPlayerState {state}");
            _receivedTime += diff * LogicTimer.FixedDelta;
            if (_buffer.IsFull)
            {
                Debug.LogWarning("[C] Remote: Something happened");
                //Lag?
                _receivedTime = 0f;
                _buffer.FastClear();
            }
            _buffer.Add(state);

            if (_position != state.Position)
            {
                Debug.Log($"OnPlayerState _lastPosition={_lastPosition} Position={state.Position}");
                _position = state.Position;
            }
            if (_rotation != state.Rotation)
            {
                _rotation = state.Rotation;
            }
        }
Esempio n. 2
0
 public override void ApplyInput(PlayerInputPacket command, float delta)
 {
     if (NetworkGeneral.SeqDiff(command.Id, LastProcessedCommandId) <= 0)
     {
         return;
     }
     LastProcessedCommandId = command.Id;
     base.ApplyInput(command, delta);
 }
Esempio n. 3
0
 private void OnServerState()
 {
     //skip duplicate or old because we received that packet unreliably
     if (NetworkGeneral.SeqDiff(_cachedServerState.Tick, _lastServerTick) <= 0)
     {
         return;
     }
     _lastServerTick = _cachedServerState.Tick;
     _playerManager.ApplyServerState(ref _cachedServerState);
 }
Esempio n. 4
0
        private void OnServerState()
        {
            if (NetworkGeneral.SeqDiff(_cachedServerState.Tick, _lastServerTick) <= 0
                ) //skip duplicate or old because we received that packet unreliably
            {
                return;
            }

            _lastServerTick = _cachedServerState.Tick;
            _clientManager.UpdateLogic(_cachedServerState.CharacterStates);
        }
Esempio n. 5
0
        public void ReceiveServerState(ServerState serverState, PlayerState ourState)
        {
            if (!_firstStateReceived)
            {
                if (serverState.LastProcessedCommand == 0)
                {
                    return;
                }
                _firstStateReceived = true;
            }
            if (serverState.Tick == _lastServerState.Tick ||
                serverState.LastProcessedCommand == _lastServerState.LastProcessedCommand)
            {
                return;
            }

            _lastServerState = serverState;

            //sync
            _position = ourState.Position;
            _rotation = ourState.Rotation;
            if (_predictionPlayerStates.Count == 0)
            {
                return;
            }

            ushort lastProcessedCommand = serverState.LastProcessedCommand;
            int    diff = NetworkGeneral.SeqDiff(lastProcessedCommand, _predictionPlayerStates.First.Id);

            //apply prediction
            if (diff >= 0 && diff < _predictionPlayerStates.Count)
            {
                //Debug.Log($"[OK]  SP: {serverState.LastProcessedCommand}, OUR: {_predictionPlayerStates.First.Id}, DF:{diff}");
                _predictionPlayerStates.RemoveFromStart(diff + 1);
                foreach (var state in _predictionPlayerStates)
                {
                    ApplyInput(state, LogicTimer.FixedDelta);
                }
            }
            else if (diff >= _predictionPlayerStates.Count)
            {
                Debug.Log($"[C] Player input lag st: {_predictionPlayerStates.First.Id} ls:{lastProcessedCommand} df:{diff}");
                //lag
                _predictionPlayerStates.FastClear();
                _nextCommand.Id = lastProcessedCommand;
            }
            else
            {
                Debug.Log($"[ERR] SP: {serverState.LastProcessedCommand}, OUR: {_predictionPlayerStates.First.Id}, DF:{diff}, STORED: {StoredCommands}");
            }
        }
Esempio n. 6
0
        public void OnPlayerState(PlayerState state)
        {
            //old command
            int diff = NetworkGeneral.SeqDiff(state.Tick, _buffer.Last.Tick);

            if (diff <= 0)
            {
                return;
            }

            _receivedTime += diff * LogicTimer.FixedDelta;
            if (_buffer.IsFull)
            {
                Debug.LogWarning("[C] Remote: Something happened");
                //Lag?
                _receivedTime = 0f;
                _buffer.FastClear();
            }
            _buffer.Add(state);
        }
Esempio n. 7
0
        public void UpdatePosition(float delta)
        {
            if (_receivedTime < BufferTime || _buffer.Count < 2)
            {
                return;
            }
            var dataA = _buffer[0];
            var dataB = _buffer[1];

            float lerpTime = NetworkGeneral.SeqDiff(dataB.Tick, dataA.Tick) * LogicTimer.FixedDelta;
            float t        = _timer / lerpTime;

            _position = Vector2.Lerp(dataA.Position, dataB.Position, t);
            _rotation = Mathf.Lerp(dataA.Rotation, dataB.Rotation, t);
            _timer   += delta;
            if (_timer > lerpTime)
            {
                _receivedTime -= lerpTime;
                _buffer.RemoveFromStart(1);
                _timer -= lerpTime;
            }
        }
Esempio n. 8
0
        public void ReceiveServerState(ServerState serverState, PlayerState ourState)
        {
            if (serverState.Tick == _lastServerTick)
            {
                return;
            }
            _lastServerTick = serverState.Tick;

            //sync
            _position = ourState.Position;
            _rotation = ourState.Rotation;
            if (_predictionPlayerStates.Count == 0)
            {
                return;
            }

            ushort lastProcessedCommand = serverState.LastProcessedCommand;
            int    diff = NetworkGeneral.SeqDiff(lastProcessedCommand, _predictionPlayerStates.First.Id);


            //apply prediction
            if (diff >= 0 && diff < _predictionPlayerStates.Count)
            {
                _predictionPlayerStates.RemoveFromStart(diff + 1);
                foreach (var state in _predictionPlayerStates)
                {
                    ApplyInput(state, LogicTimer.FixedDelta);
                }
            }
            else
            {
                Debug.Log($"[C] Player input lag: {_predictionPlayerStates.First.Id} {lastProcessedCommand}");
                //lag
                _predictionPlayerStates.FastClear();
                _nextCommand.Id = lastProcessedCommand;
            }
        }