quantum-space-buddies/QSB/Events/PlayerReadyEvent.cs
2020-08-22 18:08:25 +01:00

34 lines
954 B
C#

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