mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-07 13:05:41 +00:00
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using QSB.Events;
|
|
using QSB.Messaging;
|
|
|
|
namespace QSB.Tools
|
|
{
|
|
public class PlayerFlashlightEvent : QSBEvent<ToggleMessage>
|
|
{
|
|
public override EventType Type => EventType.FlashlightActiveChange;
|
|
|
|
public override void SetupListener()
|
|
{
|
|
GlobalMessenger.AddListener(EventNames.TurnOnFlashlight, HandleTurnOn);
|
|
GlobalMessenger.AddListener(EventNames.TurnOffFlashlight, HandleTurnOff);
|
|
}
|
|
|
|
public override void CloseListener()
|
|
{
|
|
GlobalMessenger.RemoveListener(EventNames.TurnOnFlashlight, HandleTurnOn);
|
|
GlobalMessenger.RemoveListener(EventNames.TurnOffFlashlight, HandleTurnOff);
|
|
}
|
|
|
|
private void HandleTurnOn() => SendEvent(CreateMessage(true));
|
|
private void HandleTurnOff() => SendEvent(CreateMessage(false));
|
|
|
|
private ToggleMessage CreateMessage(bool value) => new ToggleMessage
|
|
{
|
|
AboutId = LocalPlayerId,
|
|
ToggleValue = value
|
|
};
|
|
|
|
public override void OnReceiveRemote(ToggleMessage message)
|
|
{
|
|
var player = PlayerRegistry.GetPlayer(message.AboutId);
|
|
player.UpdateState(State.Flashlight, message.ToggleValue);
|
|
player.FlashLight?.UpdateState(message.ToggleValue);
|
|
}
|
|
|
|
public override void OnReceiveLocal(ToggleMessage message)
|
|
{
|
|
PlayerRegistry.LocalPlayer.UpdateState(State.Flashlight, message.ToggleValue);
|
|
}
|
|
}
|
|
}
|