DisposalSignalRouterSystem.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Content.Server.DeviceLinking.Events;
  2. using Content.Server.DeviceLinking.Systems;
  3. using Content.Server.Disposal.Tube;
  4. using Content.Server.Disposal.Tube.Components;
  5. namespace Content.Server.Disposal.Tube.Systems;
  6. /// <summary>
  7. /// Handles signals and the routing get next direction event.
  8. /// </summary>
  9. public sealed class DisposalSignalRouterSystem : EntitySystem
  10. {
  11. [Dependency] private readonly DeviceLinkSystem _deviceLink = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<DisposalSignalRouterComponent, ComponentInit>(OnInit);
  16. SubscribeLocalEvent<DisposalSignalRouterComponent, SignalReceivedEvent>(OnSignalReceived);
  17. SubscribeLocalEvent<DisposalSignalRouterComponent, GetDisposalsNextDirectionEvent>(OnGetNextDirection, after: new[] { typeof(DisposalTubeSystem) });
  18. }
  19. private void OnInit(EntityUid uid, DisposalSignalRouterComponent comp, ComponentInit args)
  20. {
  21. _deviceLink.EnsureSinkPorts(uid, comp.OnPort, comp.OffPort, comp.TogglePort);
  22. }
  23. private void OnSignalReceived(EntityUid uid, DisposalSignalRouterComponent comp, ref SignalReceivedEvent args)
  24. {
  25. // TogglePort flips it
  26. // OnPort sets it to true
  27. // OffPort sets it to false
  28. comp.Routing = args.Port == comp.TogglePort
  29. ? !comp.Routing
  30. : args.Port == comp.OnPort;
  31. }
  32. private void OnGetNextDirection(EntityUid uid, DisposalSignalRouterComponent comp, ref GetDisposalsNextDirectionEvent args)
  33. {
  34. if (!comp.Routing)
  35. {
  36. args.Next = Transform(uid).LocalRotation.GetDir();
  37. return;
  38. }
  39. // use the junction side direction when a tag matches
  40. var ev = new GetDisposalsConnectableDirectionsEvent();
  41. RaiseLocalEvent(uid, ref ev);
  42. args.Next = ev.Connectable[1];
  43. }
  44. }