quantum-space-buddies/QSB/Tools/Events/PlayerProbeEvent.cs
2020-12-03 08:28:05 +00:00

45 lines
1.3 KiB
C#

using QSB.EventsCore;
using QSB.Messaging;
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(ToggleMessage message)
{
var player = QSBPlayerManager.GetPlayer(message.AboutId);
player.UpdateState(State.ProbeActive, message.ToggleValue);
player.Probe?.SetState(message.ToggleValue);
}
public override void OnReceiveLocal(ToggleMessage message)
{
QSBPlayerManager.LocalPlayer.UpdateState(State.ProbeActive, message.ToggleValue);
}
}
}