1
0

StationAnchorSystem.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Content.Server.Popups;
  2. using Content.Server.Power.EntitySystems;
  3. using Content.Server.Shuttles.Components;
  4. using Content.Shared.Construction.Components;
  5. using Content.Shared.Popups;
  6. namespace Content.Server.Shuttles.Systems;
  7. public sealed class StationAnchorSystem : EntitySystem
  8. {
  9. [Dependency] private readonly ShuttleSystem _shuttleSystem = default!;
  10. [Dependency] private readonly PopupSystem _popupSystem = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<StationAnchorComponent, UnanchorAttemptEvent>(OnUnanchorAttempt);
  15. SubscribeLocalEvent<StationAnchorComponent, AnchorStateChangedEvent>(OnAnchorStationChange);
  16. SubscribeLocalEvent<StationAnchorComponent, ChargedMachineActivatedEvent>(OnActivated);
  17. SubscribeLocalEvent<StationAnchorComponent, ChargedMachineDeactivatedEvent>(OnDeactivated);
  18. SubscribeLocalEvent<StationAnchorComponent, MapInitEvent>(OnMapInit);
  19. }
  20. private void OnMapInit(Entity<StationAnchorComponent> ent, ref MapInitEvent args)
  21. {
  22. if (!ent.Comp.SwitchedOn)
  23. return;
  24. SetStatus(ent, true);
  25. }
  26. private void OnActivated(Entity<StationAnchorComponent> ent, ref ChargedMachineActivatedEvent args)
  27. {
  28. SetStatus(ent, true);
  29. }
  30. private void OnDeactivated(Entity<StationAnchorComponent> ent, ref ChargedMachineDeactivatedEvent args)
  31. {
  32. SetStatus(ent, false);
  33. }
  34. /// <summary>
  35. /// Prevent unanchoring when anchor is active
  36. /// </summary>
  37. private void OnUnanchorAttempt(Entity<StationAnchorComponent> ent, ref UnanchorAttemptEvent args)
  38. {
  39. if (!ent.Comp.SwitchedOn)
  40. return;
  41. _popupSystem.PopupEntity(
  42. Loc.GetString("station-anchor-unanchoring-failed"),
  43. ent,
  44. args.User,
  45. PopupType.Medium);
  46. args.Cancel();
  47. }
  48. private void OnAnchorStationChange(Entity<StationAnchorComponent> ent, ref AnchorStateChangedEvent args)
  49. {
  50. if (!args.Anchored)
  51. SetStatus(ent, false);
  52. }
  53. private void SetStatus(Entity<StationAnchorComponent> ent, bool enabled, ShuttleComponent? shuttleComponent = default)
  54. {
  55. var transform = Transform(ent);
  56. var grid = transform.GridUid;
  57. if (!grid.HasValue || !transform.Anchored && enabled || !Resolve(grid.Value, ref shuttleComponent))
  58. return;
  59. if (enabled)
  60. {
  61. _shuttleSystem.Disable(grid.Value);
  62. }
  63. else
  64. {
  65. _shuttleSystem.Enable(grid.Value);
  66. }
  67. shuttleComponent.Enabled = !enabled;
  68. ent.Comp.SwitchedOn = enabled;
  69. }
  70. }