quantum-space-buddies/QSB/Events/PlayerFlashlightEvent.cs

34 lines
1.1 KiB
C#
Raw Normal View History

2020-08-09 07:17:00 +00:00
using QSB.Messaging;
namespace QSB.Events
{
2020-08-10 16:17:54 +00:00
public class PlayerFlashlightEvent : QSBEvent<ToggleMessage>
{
2020-08-09 07:17:00 +00:00
public override MessageType Type => MessageType.FlashlightActiveChange;
2020-08-08 11:23:23 +00:00
public override void SetupListener()
{
2020-08-10 17:24:28 +00:00
GlobalMessenger.AddListener(EventNames.TurnOnFlashlight, () => SendEvent(CreateMessage(true)));
GlobalMessenger.AddListener(EventNames.TurnOffFlashlight, () => SendEvent(CreateMessage(false)));
2020-08-08 11:23:23 +00:00
}
2020-08-10 16:17:54 +00:00
private ToggleMessage CreateMessage(bool value) => new ToggleMessage
{
SenderId = LocalPlayerId,
ToggleValue = value
};
2020-08-10 13:40:06 +00:00
public override void OnReceiveRemote(ToggleMessage message)
{
2020-08-09 11:19:51 +00:00
var player = PlayerRegistry.GetPlayer(message.SenderId);
2020-08-09 18:33:36 +00:00
player.UpdateState(State.Flashlight, message.ToggleValue);
2020-08-10 11:12:51 +00:00
player.FlashLight?.UpdateState(message.ToggleValue);
}
2020-08-09 07:37:32 +00:00
public override void OnReceiveLocal(ToggleMessage message)
{
2020-08-09 21:10:47 +00:00
PlayerRegistry.LocalPlayer.UpdateState(State.Flashlight, message.ToggleValue);
}
}
}