BaseToggleWireAction.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. namespace Content.Server.Wires;
  2. /// <summary>
  3. /// Utility class meant to be implemented. This is to
  4. /// toggle a value whenever a wire is cut, mended,
  5. /// or pulsed.
  6. /// </summary>
  7. public abstract partial class BaseToggleWireAction : BaseWireAction
  8. {
  9. /// <summary>
  10. /// Toggles the value on the given entity. An implementor
  11. /// is expected to handle the value toggle appropriately.
  12. /// </summary>
  13. public abstract void ToggleValue(EntityUid owner, bool setting);
  14. /// <summary>
  15. /// Gets the value on the given entity. An implementor
  16. /// is expected to handle the value getter properly.
  17. /// </summary>
  18. public abstract bool GetValue(EntityUid owner);
  19. /// <summary>
  20. /// Timeout key for the wire, if it is pulsed.
  21. /// If this is null, there will be no value revert
  22. /// after a given delay, otherwise, the value will
  23. /// be set to the opposite of what it currently is
  24. /// (according to GetValue)
  25. /// </summary>
  26. public virtual object? TimeoutKey { get; } = null;
  27. public virtual int Delay { get; } = 30;
  28. public override bool Cut(EntityUid user, Wire wire)
  29. {
  30. base.Cut(user, wire);
  31. ToggleValue(wire.Owner, false);
  32. if (TimeoutKey != null)
  33. {
  34. WiresSystem.TryCancelWireAction(wire.Owner, TimeoutKey);
  35. }
  36. return true;
  37. }
  38. public override bool Mend(EntityUid user, Wire wire)
  39. {
  40. base.Mend(user, wire);
  41. ToggleValue(wire.Owner, true);
  42. return true;
  43. }
  44. public override void Pulse(EntityUid user, Wire wire)
  45. {
  46. base.Pulse(user, wire);
  47. ToggleValue(wire.Owner, !GetValue(wire.Owner));
  48. if (TimeoutKey != null)
  49. {
  50. WiresSystem.StartWireAction(wire.Owner, Delay, TimeoutKey, new TimedWireEvent(AwaitPulseCancel, wire));
  51. }
  52. }
  53. public override void Update(Wire wire)
  54. {
  55. if (TimeoutKey != null && !IsPowered(wire.Owner))
  56. {
  57. WiresSystem.TryCancelWireAction(wire.Owner, TimeoutKey);
  58. }
  59. }
  60. private void AwaitPulseCancel(Wire wire)
  61. {
  62. if (!wire.IsCut)
  63. {
  64. ToggleValue(wire.Owner, !GetValue(wire.Owner));
  65. }
  66. }
  67. }