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

44 lines
1.4 KiB
C#
Raw Normal View History

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