quantum-space-buddies/QSB/Tools/PlayerSignalscopeEvent.cs
2020-08-13 21:46:16 +02:00

40 lines
1.2 KiB
C#

using QSB.Events;
using QSB.Messaging;
using QSB.Utility;
namespace QSB.Tools
{
public class PlayerSignalscopeEvent : QSBEvent<ToggleMessage>
{
public override MessageType Type => MessageType.SignalscopeActiveChange;
public override void SetupListener()
{
GlobalMessenger<Signalscope>.AddListener(EventNames.EquipSignalscope, var => SendEvent(CreateMessage(true)));
GlobalMessenger.AddListener(EventNames.UnequipSignalscope, () => SendEvent(CreateMessage(false)));
}
private ToggleMessage CreateMessage(bool value) => new ToggleMessage
{
SenderId = LocalPlayerId,
ToggleValue = value
};
public override void OnReceiveRemote(ToggleMessage message)
{
var player = PlayerRegistry.GetPlayer(message.SenderId);
player.UpdateState(State.Signalscope, message.ToggleValue);
if (!IsInUniverse)
{
return;
}
player.Signalscope?.ChangeEquipState(message.ToggleValue);
}
public override void OnReceiveLocal(ToggleMessage message)
{
PlayerRegistry.LocalPlayer.UpdateState(State.Signalscope, message.ToggleValue);
}
}
}