-
Notifications
You must be signed in to change notification settings - Fork 164
/
LagCompensationManager.cs
138 lines (118 loc) · 4.71 KB
/
LagCompensationManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace Netcode.Extensions.LagCompensation
{
/// <summary>
/// The main class for controlling lag compensation
/// </summary>
public class LagCompensationManager : MonoBehaviour
{
public static LagCompensationManager Singleton { get; private set; }
NetworkManager m_NetworkManager;
[SerializeField]
float m_SecondsHistory;
[SerializeField]
[Tooltip("If true this will sync transform changes after the rollback back to the physics engine so that queries like raycasts use the compensated positions")]
bool m_SyncTransforms = true;
/// <summary>
/// Simulation objects
/// </summary>
public readonly List<TrackedObject> SimulationObjects = new List<TrackedObject>();
private void Awake()
{
if (Singleton != null && Singleton != this)
{
Destroy(gameObject);
return;
}
Singleton = this;
DontDestroyOnLoad(gameObject);
}
private void Update()
{
if (m_NetworkManager == null)
{
var networkManger = NetworkManager.Singleton;
if (networkManger != null && networkManger.IsServer || networkManger.IsClient) // check if networkmanager is running
{
m_NetworkManager = networkManger;
m_NetworkManager.NetworkTickSystem.Tick += AddFrames;
}
}
else
{
if (m_NetworkManager.IsServer == false && m_NetworkManager.IsClient == false) // no longer running
{
m_NetworkManager.NetworkTickSystem.Tick -= AddFrames;
m_NetworkManager = null;
}
}
}
/// <summary>
/// Turns time back a given amount of seconds, invokes an action and turns it back
/// </summary>
/// <param name="secondsAgo">The amount of seconds</param>
/// <param name="action">The action to invoke when time is turned back</param>
public void Simulate(float secondsAgo, Action action)
{
Simulate(secondsAgo, SimulationObjects, action);
}
/// <summary>
/// Turns time back a given amount of second on the given objects, invokes an action and turns it back
/// </summary>
/// <param name="secondsAgo">The amount of seconds</param>
/// <param name="simulatedObjects">The object to simulate back in time</param>
/// <param name="action">The action to invoke when time is turned back</param>
public void Simulate(float secondsAgo, IList<TrackedObject> simulatedObjects, Action action)
{
if (!NetworkManager.Singleton.IsServer)
{
throw new NotServerException("Only the server can perform lag compensation");
}
for (int i = 0; i < simulatedObjects.Count; i++)
{
simulatedObjects[i].ReverseTransform(secondsAgo);
}
if (!Physics.autoSyncTransforms && m_SyncTransforms)
{
Physics.SyncTransforms();
}
action.Invoke();
for (int i = 0; i < simulatedObjects.Count; i++)
{
simulatedObjects[i].ResetStateTransform();
}
if (!Physics.autoSyncTransforms && m_SyncTransforms)
{
Physics.SyncTransforms();
}
}
/// <summary>
/// Turns time back a given amount of seconds, invokes an action and turns it back. The time is based on the estimated RTT of a clientId
/// </summary>
/// <param name="clientId">The clientId's RTT to use</param>
/// <param name="action">The action to invoke when time is turned back</param>
public void Simulate(ulong clientId, Action action)
{
if (!NetworkManager.Singleton.IsServer)
{
throw new NotServerException("Only the server can perform lag compensation");
}
float millisecondsDelay = NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetCurrentRtt(clientId) / 2f;
Simulate(millisecondsDelay * 1000f, action);
}
internal void AddFrames()
{
for (int i = 0; i < SimulationObjects.Count; i++)
{
SimulationObjects[i].AddFrame();
}
}
internal int MaxQueuePoints()
{
return (int)(m_SecondsHistory / (1f / NetworkManager.Singleton.NetworkConfig.TickRate));
}
}
}