SignalSwitchSystem.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Content.Server.DeviceLinking.Components;
  2. using Content.Server.DeviceNetwork;
  3. using Content.Shared.Interaction;
  4. using Robust.Shared.Audio;
  5. using Robust.Shared.Audio.Systems;
  6. namespace Content.Server.DeviceLinking.Systems;
  7. public sealed class SignalSwitchSystem : EntitySystem
  8. {
  9. [Dependency] private readonly DeviceLinkSystem _deviceLink = default!;
  10. [Dependency] private readonly SharedAudioSystem _audio = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<SignalSwitchComponent, ComponentInit>(OnInit);
  15. SubscribeLocalEvent<SignalSwitchComponent, ActivateInWorldEvent>(OnActivated);
  16. }
  17. private void OnInit(EntityUid uid, SignalSwitchComponent comp, ComponentInit args)
  18. {
  19. _deviceLink.EnsureSourcePorts(uid, comp.OnPort, comp.OffPort, comp.StatusPort);
  20. }
  21. private void OnActivated(EntityUid uid, SignalSwitchComponent comp, ActivateInWorldEvent args)
  22. {
  23. if (args.Handled || !args.Complex)
  24. return;
  25. comp.State = !comp.State;
  26. _deviceLink.InvokePort(uid, comp.State ? comp.OnPort : comp.OffPort);
  27. // only send status if it's a toggle switch and not a button
  28. if (comp.OnPort != comp.OffPort)
  29. {
  30. _deviceLink.SendSignal(uid, comp.StatusPort, comp.State);
  31. }
  32. _audio.PlayPvs(comp.ClickSound, uid, AudioParams.Default.WithVariation(0.125f).WithVolume(8f));
  33. args.Handled = true;
  34. }
  35. }