| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using Content.Server.DeviceLinking.Components;
- using Content.Server.DeviceNetwork;
- using Content.Server.Doors.Systems;
- using Content.Shared.DeviceNetwork;
- using Content.Shared.Doors.Components;
- using Content.Shared.Doors;
- using JetBrains.Annotations;
- using SignalReceivedEvent = Content.Server.DeviceLinking.Events.SignalReceivedEvent;
- namespace Content.Server.DeviceLinking.Systems
- {
- [UsedImplicitly]
- public sealed class DoorSignalControlSystem : EntitySystem
- {
- [Dependency] private readonly DoorSystem _doorSystem = default!;
- [Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
- public override void Initialize()
- {
- base.Initialize();
- SubscribeLocalEvent<DoorSignalControlComponent, ComponentInit>(OnInit);
- SubscribeLocalEvent<DoorSignalControlComponent, SignalReceivedEvent>(OnSignalReceived);
- SubscribeLocalEvent<DoorSignalControlComponent, DoorStateChangedEvent>(OnStateChanged);
- }
- private void OnInit(EntityUid uid, DoorSignalControlComponent component, ComponentInit args)
- {
- _signalSystem.EnsureSinkPorts(uid, component.OpenPort, component.ClosePort, component.TogglePort);
- _signalSystem.EnsureSourcePorts(uid, component.OutOpen);
- }
- private void OnSignalReceived(EntityUid uid, DoorSignalControlComponent component, ref SignalReceivedEvent args)
- {
- if (!TryComp(uid, out DoorComponent? door))
- return;
- var state = SignalState.Momentary;
- args.Data?.TryGetValue(DeviceNetworkConstants.LogicState, out state);
- if (args.Port == component.OpenPort)
- {
- if (state == SignalState.High || state == SignalState.Momentary)
- {
- if (door.State == DoorState.Closed)
- _doorSystem.TryOpen(uid, door);
- }
- }
- else if (args.Port == component.ClosePort)
- {
- if (state == SignalState.High || state == SignalState.Momentary)
- {
- if (door.State == DoorState.Open)
- _doorSystem.TryClose(uid, door);
- }
- }
- else if (args.Port == component.TogglePort)
- {
- if (state == SignalState.High || state == SignalState.Momentary)
- {
- _doorSystem.TryToggleDoor(uid, door);
- }
- }
- else if (args.Port == component.InBolt)
- {
- if (!TryComp<DoorBoltComponent>(uid, out var bolts))
- return;
- // if its a pulse toggle, otherwise set bolts to high/low
- bool bolt;
- if (state == SignalState.Momentary)
- {
- bolt = !bolts.BoltsDown;
- }
- else
- {
- bolt = state == SignalState.High;
- }
- _doorSystem.SetBoltsDown((uid, bolts), bolt);
- }
- }
- private void OnStateChanged(EntityUid uid, DoorSignalControlComponent door, DoorStateChangedEvent args)
- {
- if (args.State == DoorState.Closed)
- {
- // only ever say the door is closed when it is completely airtight
- _signalSystem.SendSignal(uid, door.OutOpen, false);
- }
- else if (args.State == DoorState.Open
- || args.State == DoorState.Opening
- || args.State == DoorState.Closing
- || args.State == DoorState.Emagging)
- {
- // say the door is open whenever it would be letting air pass
- _signalSystem.SendSignal(uid, door.OutOpen, true);
- }
- }
- }
- }
|