Cyber/Assets/Scripts/Networking/Syncer.cs

59 lines
1.6 KiB
C#
Raw Normal View History


using UnityEngine;
namespace Cyber.Networking {
2017-05-14 22:25:19 +02:00
/// <summary>
/// An abstract syncer, used by <see cref="Serverside.ServerSyncer"/> and <see cref="Clientside.ClientSyncer"/>.
/// </summary>
public abstract class Syncer : MonoBehaviour {
private int TickCounter = 0;
private readonly float TickInterval;
private float TimeSinceLastTick = 0;
private int SyncPacketID = 0;
2017-05-14 22:25:19 +02:00
/// <summary>
/// Initializes the syncer, basically just sets the <see cref="TickInterval"/>.
/// </summary>
/// <param name="tickInterval"></param>
public Syncer(float tickInterval) {
TickInterval = tickInterval;
TimeSinceLastTick = tickInterval;
}
2017-05-14 22:25:19 +02:00
/// <summary>
/// Returns the next ID for a sync packet.
/// </summary>
/// <returns></returns>
public int NextSyncID() {
return SyncPacketID++;
}
2017-05-14 22:25:19 +02:00
/// <summary>
/// Performs a tick on the syncer, called from <see cref="Update"/>.
/// </summary>
/// <param name="Tick">The tick number, used to e.g. determine when to sync certain things.</param>
public abstract void PerformTick(int Tick);
private void Update() {
TimeSinceLastTick += Time.deltaTime;
if (TimeSinceLastTick > TickInterval) {
PerformTick(TickCounter);
if (TickCounter < int.MaxValue) {
TickCounter++;
} else {
TickCounter = 0;
}
TimeSinceLastTick -= TickInterval;
}
}
}
}