quantum-space-buddies/QSB/Events/PlayerReadyEvent.cs
2020-08-15 21:52:43 +02:00

36 lines
1.0 KiB
C#

using QSB.Messaging;
using QSB.Utility;
namespace QSB.Events
{
public class PlayerReadyEvent : QSBEvent<ToggleMessage>
{
public override MessageType Type => MessageType.PlayerReady;
public override void SetupListener()
{
GlobalMessenger<bool>.AddListener(EventNames.QSBPlayerReady, Handler);
}
public override void CloseListener()
{
DebugLog.ToConsole("Close listener for ready event");
GlobalMessenger<bool>.RemoveListener(EventNames.QSBPlayerReady, Handler);
}
private void Handler(bool ready) => SendEvent(CreateMessage(ready));
private ToggleMessage CreateMessage(bool ready) => new ToggleMessage
{
SenderId = LocalPlayerId,
ToggleValue = ready
};
public override void OnServerReceive(ToggleMessage message)
{
PlayerRegistry.GetPlayer(message.SenderId).IsReady = message.ToggleValue;
PlayerState.LocalInstance.Send();
}
}
}