-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathLagCompensationManager.cs
More file actions
77 lines (68 loc) · 2.83 KB
/
LagCompensationManager.cs
File metadata and controls
77 lines (68 loc) · 2.83 KB
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
using System;
using System.Collections.Generic;
using MLAPI.Exceptions;
namespace MLAPI.LagCompensation
{
/// <summary>
/// The main class for controlling lag compensation
/// </summary>
public static class LagCompensationManager
{
/// <summary>
/// Simulation objects
/// </summary>
public static readonly List<TrackedObject> SimulationObjects = new List<TrackedObject>();
/// <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 static 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 static 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);
}
action.Invoke();
for (int i = 0; i < simulatedObjects.Count; i++)
{
simulatedObjects[i].ResetStateTransform();
}
}
/// <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 static 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 static void AddFrames()
{
for (int i = 0; i < SimulationObjects.Count; i++)
{
SimulationObjects[i].AddFrame();
}
}
}
}