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}"); } }
public ClientPlayer(ClientLogic clientLogic, ClientPlayerManager manager, string name, byte id) : base(manager, name, id) { _playerManager = manager; _predictionPlayerStates = new LiteRingBuffer <PlayerInputPacket>(MaxStoredCommands); _clientLogic = clientLogic; _predictionPlayerStates.FastClear(); _predictionPlayerStates.Add(new PlayerInputPacket { Id = 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; } }
public override void Spawn(Vector2 position) { _buffer.FastClear(); base.Spawn(position); }