Applied constant update rate

This commit is contained in:
2026-02-01 15:03:52 +01:00
parent 912be35172
commit d03d06b5a7
4 changed files with 1221 additions and 1111 deletions

View File

@@ -59,6 +59,10 @@ namespace Marro.PacManUdon
/// How long to wait since last message to send next ping.
/// </summary>
[SerializeField] private float pingDelay = 0.3f;
/// <summary>
/// The rate at which updates occur.
/// </summary>
[SerializeField] private float updateRate = 0.0166666667f;
#endregion
#region Constants
@@ -108,6 +112,10 @@ namespace Marro.PacManUdon
/// </summary>
private float internalTime;
/// <summary>
/// Time at which the latest update occured
/// </summary>
private float nextUpdateTime;
/// <summary>
/// Time at which next received event occured.
/// </summary>
@@ -212,16 +220,6 @@ namespace Marro.PacManUdon
#endregion
#region General
public void Awake()
{
// I would set the instance variable here, but it's somehow null by the time Initialize() is called... Udon moment?
var syncedObjects = root.GetComponentsInChildren<SyncedObject>(includeInactive: true);
foreach (var obj in syncedObjects)
{
obj.networkManager = this;
}
}
public void Initialize()
{
if (!BitConverter.IsLittleEndian)
@@ -231,8 +229,12 @@ namespace Marro.PacManUdon
return;
}
syncedObjects = root.GetComponentsInChildren<SyncedObject>(includeInactive: true);
if (root == null)
{
root = transform.parent.gameObject;
}
syncedObjects = root.GetComponentsInChildren<SyncedObject>(includeInactive: true);
foreach (var obj in syncedObjects)
{
obj.networkManager = this;
@@ -252,13 +254,14 @@ namespace Marro.PacManUdon
internalTime = 0;
SyncedTime = 0;
SyncedDeltaTime = Time.fixedDeltaTime;
nextUpdateTime = SyncedTime;
Ready = true;
Debug.Log($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Initialized, time offset: {offsetTime}");
}
public void FixedUpdate()
public void Update()
{
if (!Ready)
{
@@ -277,12 +280,17 @@ namespace Marro.PacManUdon
}
else
{
ProgressEventTime(); // See if there's events that need to be replayed
ApplyReceivedEvents(); // See if there's events that need to be replayed
}
}
// Forwards simulated time at the FixedUpdate pace
PerformFixedSyncedUpdate();
// Forwards simulated time at the updateRate pace
while (nextUpdateTime <= internalTime)
{
ProgressSyncedTime(nextUpdateTime);
PerformFixedSyncedUpdate();
nextUpdateTime = SyncedTime + updateRate;
}
}
private void UpdateInternalTime()
@@ -293,7 +301,6 @@ namespace Marro.PacManUdon
private void PerformFixedSyncedUpdate()
{
IsEventUpdate = false;
ProgressSyncedTime(internalTime);
for (int i = 0; i < syncedObjects.Length; i++)
{
@@ -640,13 +647,13 @@ namespace Marro.PacManUdon
Debug.Log($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Queued full sync in buffer, should execute at {nextEventTime}.");
}
private void ProgressEventTime()
private void ApplyReceivedEvents()
{
IsEventUpdate = true;
while (eventsQueueIndex > 0 && nextEventTime <= internalTime)
{
var success = PerformEvent(eventsQueue[0]);
var success = ApplyEvent(eventsQueue[0]);
if (!success)
{
@@ -658,7 +665,7 @@ namespace Marro.PacManUdon
}
}
private bool PerformEvent(byte[] @event)
private bool ApplyEvent(byte[] @event)
{
var timestamp = GetTimestampFromHeader(@event);
var eventType = GetEventTypeFromHeader(@event);