Files
OldThink/Content.Server/MachineLinking/System/TwoWayLeverSystem.cs

52 lines
1.9 KiB
C#
Raw Normal View History

using System;
using Content.Server.MachineLinking.Components;
using Content.Server.MachineLinking.Events;
using Content.Shared.Interaction;
using Content.Shared.MachineLinking;
using Robust.Shared.GameObjects;
namespace Content.Server.MachineLinking.System
{
public sealed class TwoWayLeverSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
2022-04-04 01:13:03 -05:00
SubscribeLocalEvent<TwoWayLeverComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<TwoWayLeverComponent, InteractHandEvent>(OnInteractHand);
}
2022-04-04 01:13:03 -05:00
private void OnInit(EntityUid uid, TwoWayLeverComponent component, ComponentInit args)
{
2022-04-04 01:13:03 -05:00
var transmitter = EnsureComp<SignalTransmitterComponent>(uid);
foreach (string state in Enum.GetNames<TwoWayLeverState>())
if (!transmitter.Outputs.ContainsKey(state))
transmitter.AddPort(state);
}
private void OnInteractHand(EntityUid uid, TwoWayLeverComponent component, InteractHandEvent args)
{
component.State = component.State switch
{
2022-04-04 01:13:03 -05:00
TwoWayLeverState.Middle => component.NextSignalLeft ? TwoWayLeverState.Left : TwoWayLeverState.Right,
TwoWayLeverState.Right => TwoWayLeverState.Middle,
TwoWayLeverState.Left => TwoWayLeverState.Middle,
_ => throw new ArgumentOutOfRangeException()
};
2022-04-04 01:13:03 -05:00
if (component.State == TwoWayLeverState.Middle)
{
component.NextSignalLeft = !component.NextSignalLeft;
}
if (EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearanceComponent))
{
appearanceComponent.SetData(TwoWayLeverVisuals.State, component.State);
}
2022-04-04 01:13:03 -05:00
RaiseLocalEvent(uid, new InvokePortEvent(component.State.ToString()));
args.Handled = true;
}
}
}