Files
tbd-station-14/Content.Server/MachineLinking/Components/SignalTwoWayLeverComponent.cs
2021-07-16 17:37:09 -07:00

65 lines
2.0 KiB
C#

using Content.Shared.Interaction;
using Content.Shared.MachineLinking;
using Content.Shared.Notification;
using Content.Shared.Notification.Managers;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
namespace Content.Server.MachineLinking.Components
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class SignalTwoWayLeverComponent : SignalTransmitterComponent, IInteractHand, IActivate
{
public override string Name => "TwoWayLever";
private TwoWayLeverSignal _state = TwoWayLeverSignal.Middle;
private bool _nextForward = true;
public TwoWayLeverSignal State
{
get => _state;
private set
{
_state = value;
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(TwoWayLeverVisuals.State, value);
}
}
}
private void NextState(IEntity user)
{
State = State switch
{
TwoWayLeverSignal.Left => TwoWayLeverSignal.Middle,
TwoWayLeverSignal.Middle => _nextForward ? TwoWayLeverSignal.Right : TwoWayLeverSignal.Left,
TwoWayLeverSignal.Right => TwoWayLeverSignal.Middle,
_ => TwoWayLeverSignal.Middle
};
if (State == TwoWayLeverSignal.Left || State == TwoWayLeverSignal.Right) _nextForward = !_nextForward;
if (!TransmitSignal(State))
{
Owner.PopupMessage(user, Loc.GetString("signal-two-way-lever-component-next-state-no-receivers-connected"));
}
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
NextState(eventArgs.User);
return true;
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
NextState(eventArgs.User);
}
}
}