quantum-space-buddies/QSB/TimeSync/ServerTimeEvent.cs

35 lines
998 B
C#
Raw Normal View History

2020-08-13 19:46:16 +00:00
using QSB.Events;
using QSB.Messaging;
2020-08-09 18:33:36 +00:00
2020-08-13 19:46:16 +00:00
namespace QSB.TimeSync
2020-08-09 18:33:36 +00:00
{
2020-08-10 16:17:54 +00:00
public class ServerTimeEvent : QSBEvent<ServerTimeMessage>
2020-08-09 18:33:36 +00:00
{
2020-08-10 10:45:24 +00:00
public override MessageType Type => MessageType.ServerTime;
2020-08-09 18:33:36 +00:00
public override void SetupListener()
{
2020-08-15 19:52:43 +00:00
GlobalMessenger<float, int>.AddListener(EventNames.QSBServerTime, Handler);
2020-08-09 18:33:36 +00:00
}
2020-08-15 19:55:25 +00:00
2020-08-15 19:32:58 +00:00
public override void CloseListener()
{
2020-08-15 19:52:43 +00:00
GlobalMessenger<float, int>.RemoveListener(EventNames.QSBServerTime, Handler);
2020-08-15 19:32:58 +00:00
}
2020-08-15 19:52:43 +00:00
private void Handler(float time, int count) => SendEvent(CreateMessage(time, count));
2020-08-10 16:17:54 +00:00
private ServerTimeMessage CreateMessage(float time, int count) => new ServerTimeMessage
{
AboutId = LocalPlayerId,
2020-08-10 16:17:54 +00:00
ServerTime = time,
LoopCount = count
};
2020-08-10 13:40:06 +00:00
public override void OnReceiveRemote(ServerTimeMessage message)
2020-08-09 19:46:02 +00:00
{
WakeUpSync.LocalInstance.OnClientReceiveMessage(message);
}
2020-08-09 18:33:36 +00:00
}
}