1
0

SharedDoorSystem.Bolts.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Content.Shared.Doors.Components;
  2. using Content.Shared.Prying.Components;
  3. namespace Content.Shared.Doors.Systems;
  4. public abstract partial class SharedDoorSystem
  5. {
  6. public void InitializeBolts()
  7. {
  8. base.Initialize();
  9. SubscribeLocalEvent<DoorBoltComponent, BeforeDoorOpenedEvent>(OnBeforeDoorOpened);
  10. SubscribeLocalEvent<DoorBoltComponent, BeforeDoorClosedEvent>(OnBeforeDoorClosed);
  11. SubscribeLocalEvent<DoorBoltComponent, BeforeDoorDeniedEvent>(OnBeforeDoorDenied);
  12. SubscribeLocalEvent<DoorBoltComponent, BeforePryEvent>(OnDoorPry);
  13. SubscribeLocalEvent<DoorBoltComponent, DoorStateChangedEvent>(OnStateChanged);
  14. }
  15. private void OnDoorPry(EntityUid uid, DoorBoltComponent component, ref BeforePryEvent args)
  16. {
  17. if (args.Cancelled)
  18. return;
  19. if (!component.BoltsDown || args.Force)
  20. return;
  21. args.Message = "airlock-component-cannot-pry-is-bolted-message";
  22. args.Cancelled = true;
  23. }
  24. private void OnBeforeDoorOpened(EntityUid uid, DoorBoltComponent component, BeforeDoorOpenedEvent args)
  25. {
  26. if (component.BoltsDown)
  27. args.Cancel();
  28. }
  29. private void OnBeforeDoorClosed(EntityUid uid, DoorBoltComponent component, BeforeDoorClosedEvent args)
  30. {
  31. if (component.BoltsDown)
  32. args.Cancel();
  33. }
  34. private void OnBeforeDoorDenied(EntityUid uid, DoorBoltComponent component, BeforeDoorDeniedEvent args)
  35. {
  36. if (component.BoltsDown)
  37. args.Cancel();
  38. }
  39. public void SetBoltWireCut(Entity<DoorBoltComponent> ent, bool value)
  40. {
  41. ent.Comp.BoltWireCut = value;
  42. Dirty(ent, ent.Comp);
  43. }
  44. public void UpdateBoltLightStatus(Entity<DoorBoltComponent> ent)
  45. {
  46. AppearanceSystem.SetData(ent, DoorVisuals.BoltLights, GetBoltLightsVisible(ent));
  47. }
  48. public bool GetBoltLightsVisible(Entity<DoorBoltComponent> ent)
  49. {
  50. return ent.Comp.BoltLightsEnabled &&
  51. ent.Comp.BoltsDown &&
  52. ent.Comp.Powered;
  53. }
  54. public void SetBoltLightsEnabled(Entity<DoorBoltComponent> ent, bool value)
  55. {
  56. if (ent.Comp.BoltLightsEnabled == value)
  57. return;
  58. ent.Comp.BoltLightsEnabled = value;
  59. Dirty(ent, ent.Comp);
  60. UpdateBoltLightStatus(ent);
  61. }
  62. public void SetBoltsDown(Entity<DoorBoltComponent> ent, bool value, EntityUid? user = null, bool predicted = false)
  63. {
  64. TrySetBoltDown(ent, value, user, predicted);
  65. }
  66. public bool TrySetBoltDown(
  67. Entity<DoorBoltComponent> ent,
  68. bool value,
  69. EntityUid? user = null,
  70. bool predicted = false
  71. )
  72. {
  73. if (!_powerReceiver.IsPowered(ent.Owner))
  74. return false;
  75. if (ent.Comp.BoltsDown == value)
  76. return false;
  77. ent.Comp.BoltsDown = value;
  78. Dirty(ent, ent.Comp);
  79. UpdateBoltLightStatus(ent);
  80. // used to reset the auto-close timer after unbolting
  81. var ev = new DoorBoltsChangedEvent(value);
  82. RaiseLocalEvent(ent.Owner, ev);
  83. var sound = value ? ent.Comp.BoltDownSound : ent.Comp.BoltUpSound;
  84. if (predicted)
  85. Audio.PlayPredicted(sound, ent, user: user);
  86. else
  87. Audio.PlayPvs(sound, ent);
  88. return true;
  89. }
  90. private void OnStateChanged(Entity<DoorBoltComponent> entity, ref DoorStateChangedEvent args)
  91. {
  92. // If the door is closed, we should look if the bolt was locked while closing
  93. UpdateBoltLightStatus(entity);
  94. }
  95. public bool IsBolted(EntityUid uid, DoorBoltComponent? component = null)
  96. {
  97. if (!Resolve(uid, ref component))
  98. {
  99. return false;
  100. }
  101. return component.BoltsDown;
  102. }
  103. }