forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkTickSystem.cs
More file actions
83 lines (71 loc) · 2.94 KB
/
NetworkTickSystem.cs
File metadata and controls
83 lines (71 loc) · 2.94 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
78
79
80
81
82
83
using System;
using UnityEngine;
namespace MLAPI
{
// todo: This is a pretty minimal tick system. It will be improved in the future
// It currently relies on Time.unscaledTime and, as such, will start suffering
// numerical precision issues after 2^23 ticks have passed (float have 23 bits mantissa)
// For future releases, we'll need to improve on this, probably by leveraging FixedUpdate
public class NetworkTickSystem : INetworkUpdateSystem, IDisposable
{
private const float k_DefaultTickIntervalSec = 0.05f; // Defaults to 20 ticks second
private readonly float m_TickIntervalSec; // Duration of a tick in seconds
private int m_NetworkTickCount; // How many network ticks have passed?
// special value to indicate "No tick information"
public const ushort NoTick = ushort.MaxValue;
// Number of ticks over which the tick number wraps back to 0
public const ushort TickPeriod = NoTick - 1;
/// <summary>
/// Constructor
/// Defaults to k_DefaultTickIntervalSec if no tick duration is specified
/// </summary>
/// <param name="tickIntervalSec">Duration of a network tick</param>
public NetworkTickSystem(float tickIntervalSec = k_DefaultTickIntervalSec)
{
this.RegisterNetworkUpdate(NetworkUpdateStage.EarlyUpdate);
//Assure we don't specify a value less than or equal to zero for tick frequency
m_TickIntervalSec = (tickIntervalSec <= 0f) ? k_DefaultTickIntervalSec : tickIntervalSec;
// ticks might not start at 0, so let's update right away at construction
UpdateNetworkTick();
}
public void Dispose()
{
this.UnregisterNetworkUpdate(NetworkUpdateStage.EarlyUpdate);
}
/// <summary>
/// GetTick
/// Gets the current network tick (non-fractional, wrapping around)
/// </summary>
/// <returns></returns>
public ushort GetTick()
{
return (ushort)(m_NetworkTickCount % TickPeriod);
}
/// <summary>
/// GetNetworkTime
/// Network time is calculated from m_NetworkTickCount and m_TickIntervalSec (tick frequency)
/// </summary>
/// <returns>Network Time</returns>
public float GetNetworkTime()
{
return m_NetworkTickCount * m_TickIntervalSec;
}
/// <summary>
/// UpdateNetworkTick
/// Called each network loop update during the PreUpdate stage
/// </summary>
private void UpdateNetworkTick()
{
m_NetworkTickCount = (int)(Time.unscaledTime / m_TickIntervalSec);
}
public void NetworkUpdate(NetworkUpdateStage updateStage)
{
switch (updateStage)
{
case NetworkUpdateStage.EarlyUpdate:
UpdateNetworkTick();
break;
}
}
}
}