22using UnityEngine ;
33
44namespace Code . Client
5- {
5+ {
66 public class RemotePlayer : BasePlayer
77 {
8- private readonly LiteRingBuffer < PlayerState > _buffer = new LiteRingBuffer < PlayerState > ( 30 ) ;
8+ struct IncomingData
9+ {
10+ public PlayerState State ;
11+ public ushort Tick ;
12+
13+ public IncomingData ( PlayerState state , ushort serverTick )
14+ {
15+ State = state ;
16+ Tick = serverTick ;
17+ }
18+ }
19+ private readonly LiteRingBuffer < IncomingData > _buffer = new LiteRingBuffer < IncomingData > ( 30 ) ;
920 private float _receivedTime ;
1021 private float _timer ;
1122 private const float BufferTime = 0.1f ; //100 milliseconds
@@ -15,7 +26,7 @@ public RemotePlayer(ClientPlayerManager manager, string name, PlayerJoinedPacket
1526 _position = pjPacket . InitialPlayerState . Position ;
1627 _health = pjPacket . Health ;
1728 _rotation = pjPacket . InitialPlayerState . Rotation ;
18- _buffer . Add ( pjPacket . InitialPlayerState ) ;
29+ _buffer . Add ( new IncomingData ( pjPacket . InitialPlayerState , pjPacket . ServerTick ) ) ;
1930 }
2031
2132 public override void Spawn ( Vector2 position )
@@ -28,13 +39,13 @@ public void UpdatePosition(float delta)
2839 {
2940 if ( _receivedTime < BufferTime || _buffer . Count < 2 )
3041 return ;
31- var stateA = _buffer [ 0 ] ;
32- var stateB = _buffer [ 1 ] ;
42+ var dataA = _buffer [ 0 ] ;
43+ var dataB = _buffer [ 1 ] ;
3344
34- float lerpTime = NetworkGeneral . SeqDiff ( stateB . ProcessedCommandId , stateA . ProcessedCommandId ) * LogicTimer . FixedDelta ;
45+ float lerpTime = NetworkGeneral . SeqDiff ( dataB . Tick , dataA . Tick ) * LogicTimer . FixedDelta ;
3546 float t = _timer / lerpTime ;
36- _position = Vector2 . Lerp ( stateA . Position , stateB . Position , t ) ;
37- _rotation = Mathf . Lerp ( stateA . Rotation , stateB . Rotation , t ) ;
47+ _position = Vector2 . Lerp ( dataA . State . Position , dataB . State . Position , t ) ;
48+ _rotation = Mathf . Lerp ( dataA . State . Rotation , dataB . State . Rotation , t ) ;
3849 _timer += delta ;
3950 if ( _timer > lerpTime )
4051 {
@@ -44,10 +55,10 @@ public void UpdatePosition(float delta)
4455 }
4556 }
4657
47- public void OnPlayerState ( PlayerState state )
58+ public void OnPlayerState ( ushort serverTick , PlayerState state )
4859 {
4960 //old command
50- int diff = NetworkGeneral . SeqDiff ( state . ProcessedCommandId , _buffer . Last . ProcessedCommandId ) ;
61+ int diff = NetworkGeneral . SeqDiff ( serverTick , _buffer . Last . Tick ) ;
5162 if ( diff <= 0 )
5263 return ;
5364
@@ -59,7 +70,7 @@ public void OnPlayerState(PlayerState state)
5970 _receivedTime = 0f ;
6071 _buffer . FastClear ( ) ;
6172 }
62- _buffer . Add ( state ) ;
73+ _buffer . Add ( new IncomingData ( state , serverTick ) ) ;
6374 }
6475 }
6576}
0 commit comments