quantum-space-buddies/QSB/Tools/PlayerProbeEvent.cs

44 lines
1.5 KiB
C#
Raw Normal View History

2020-08-13 21:46:16 +02:00
using QSB.Events;
using QSB.Messaging;
2020-08-10 14:40:06 +01:00
2020-08-13 21:46:16 +02:00
namespace QSB.Tools
2020-08-10 14:40:06 +01:00
{
2020-08-10 18:17:54 +02:00
public class PlayerProbeEvent : QSBEvent<ToggleMessage>
2020-08-10 14:40:06 +01:00
{
2020-08-22 18:08:25 +01:00
public override EventType Type => EventType.ProbeActiveChange;
2020-08-10 14:40:06 +01:00
public override void SetupListener()
{
2020-08-15 21:52:43 +02:00
GlobalMessenger<SurveyorProbe>.AddListener(EventNames.LaunchProbe, HandleLaunch);
GlobalMessenger<SurveyorProbe>.AddListener(EventNames.RetrieveProbe, HandleRetrieve);
2020-08-10 14:40:06 +01:00
}
2020-08-15 20:32:58 +01:00
public override void CloseListener()
{
2020-08-15 21:52:43 +02:00
GlobalMessenger<SurveyorProbe>.RemoveListener(EventNames.LaunchProbe, HandleLaunch);
GlobalMessenger<SurveyorProbe>.RemoveListener(EventNames.RetrieveProbe, HandleRetrieve);
2020-08-15 20:32:58 +01:00
}
2020-08-15 21:52:43 +02:00
private void HandleLaunch(SurveyorProbe probe) => SendEvent(CreateMessage(true));
private void HandleRetrieve(SurveyorProbe probe) => SendEvent(CreateMessage(false));
2020-08-10 18:17:54 +02:00
private ToggleMessage CreateMessage(bool value) => new ToggleMessage
{
2020-08-17 16:51:56 +01:00
AboutId = LocalPlayerId,
2020-08-10 18:17:54 +02:00
ToggleValue = value
};
2020-08-10 14:40:06 +01:00
public override void OnReceiveRemote(ToggleMessage message)
{
2020-08-17 16:51:56 +01:00
var player = PlayerRegistry.GetPlayer(message.AboutId);
2020-08-10 14:40:06 +01:00
player.UpdateState(State.ProbeActive, message.ToggleValue);
2020-08-17 18:58:45 +02:00
player.Probe?.SetState(message.ToggleValue);
2020-08-10 14:40:06 +01:00
}
public override void OnReceiveLocal(ToggleMessage message)
{
2020-08-10 16:49:22 +01:00
PlayerRegistry.LocalPlayer.UpdateState(State.ProbeActive, message.ToggleValue);
2020-08-10 14:40:06 +01:00
}
}
}