quantum-space-buddies/QSB/Events/PlayerReadyEvent.cs

34 lines
954 B
C#
Raw Normal View History

2020-08-10 10:45:24 +00:00
using QSB.Messaging;
namespace QSB.Events
{
2020-08-10 16:17:54 +00:00
public class PlayerReadyEvent : QSBEvent<ToggleMessage>
2020-08-10 10:45:24 +00:00
{
2020-08-22 17:08:25 +00:00
public override EventType Type => EventType.PlayerReady;
2020-08-10 10:45:24 +00:00
public override void SetupListener()
{
2020-08-15 19:52:43 +00:00
GlobalMessenger<bool>.AddListener(EventNames.QSBPlayerReady, Handler);
2020-08-10 10:45:24 +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<bool>.RemoveListener(EventNames.QSBPlayerReady, Handler);
2020-08-15 19:32:58 +00:00
}
2020-08-15 19:52:43 +00:00
private void Handler(bool ready) => SendEvent(CreateMessage(ready));
2020-08-10 16:17:54 +00:00
private ToggleMessage CreateMessage(bool ready) => new ToggleMessage
{
2020-08-17 15:51:56 +00:00
AboutId = LocalPlayerId,
2020-08-10 16:17:54 +00:00
ToggleValue = ready
};
2020-08-10 10:45:24 +00:00
public override void OnServerReceive(ToggleMessage message)
{
2020-08-17 15:51:56 +00:00
PlayerRegistry.GetPlayer(message.AboutId).IsReady = message.ToggleValue;
2020-08-10 10:45:24 +00:00
PlayerState.LocalInstance.Send();
}
}
}