private void ApplyLagComp()
{
var thisPlayer = GetPlayer();
if (thisPlayer == null) return;
var netSync = thisPlayer.GetComponent<NetPhysicsObject>();
if (netSync == null) return;
Asgard.Core.Collections.LinkedListNode<PlayerStateData> found_node = null;
foreach (var node in _moveBuffer)
{
if (node.Value.SimTick.Value == netSync.SimTick.Value)
{
found_node = node;
break;
}
}
if (found_node != null)
{
var moveData = found_node.Value;
var diff = netSync.Position.Value - moveData.Position.Value;
if (diff.LengthSquared() > 0)
{
var node = found_node;
Vector2 pos = netSync.Position;
Vector2 prev_pos = node.Value.Position;
while (node != null)
{
var move = node.Value;
Vector2 velStep = move.Position - prev_pos;
pos += velStep;
prev_pos = move.Position;
node = node.Next;
move.Position = pos;
}
var pComp = thisPlayer.GetComponent<Physics2dComponent>();
if (pComp == null || pComp.Body == null) return;
var playerComponent = thisPlayer.GetComponent<PlayerComponent>();
playerComponent.LerpToReal = true;
playerComponent.OldPosition = pComp.Body.Position;
playerComponent.LerpStart = NetTime.RealTime;
playerComponent.LerpEnd = playerComponent.LerpStart + (0.1f);
pComp.Body.Position = pos;
}
_moveBuffer.TruncateTo(found_node.Next);
}
}