quantum-space-buddies/QSB/Tools/Events/PlayerProbeEvent.cs
2020-12-14 16:26:38 +00:00

44 lines
1.4 KiB
C#

using QSB.Events;
using QSB.Messages;
using QSB.Player;
namespace QSB.Tools.Events
{
public class PlayerProbeEvent : QSBEvent<ToggleMessage>
{
public override EventType Type => EventType.ProbeActiveChange;
public override void SetupListener()
{
GlobalMessenger<SurveyorProbe>.AddListener(EventNames.LaunchProbe, HandleLaunch);
GlobalMessenger<SurveyorProbe>.AddListener(EventNames.RetrieveProbe, HandleRetrieve);
}
public override void CloseListener()
{
GlobalMessenger<SurveyorProbe>.RemoveListener(EventNames.LaunchProbe, HandleLaunch);
GlobalMessenger<SurveyorProbe>.RemoveListener(EventNames.RetrieveProbe, HandleRetrieve);
}
private void HandleLaunch(SurveyorProbe probe) => SendEvent(CreateMessage(true));
private void HandleRetrieve(SurveyorProbe probe) => SendEvent(CreateMessage(false));
private ToggleMessage CreateMessage(bool value) => new ToggleMessage
{
AboutId = LocalPlayerId,
ToggleValue = value
};
public override void OnReceiveRemote(bool server, ToggleMessage message)
{
var player = QSBPlayerManager.GetPlayer(message.AboutId);
player.UpdateState(State.ProbeActive, message.ToggleValue);
player.Probe?.SetState(message.ToggleValue);
}
public override void OnReceiveLocal(bool server, ToggleMessage message)
{
QSBPlayerManager.LocalPlayer.UpdateState(State.ProbeActive, message.ToggleValue);
}
}
}