GasPortableSystem.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Server.Atmos.Piping.Binary.Components;
  3. using Content.Server.Atmos.Piping.Unary.Components;
  4. using Content.Server.NodeContainer;
  5. using Content.Server.NodeContainer.EntitySystems;
  6. using Content.Server.NodeContainer.Nodes;
  7. using Content.Shared.Atmos.Piping.Unary.Components;
  8. using Content.Shared.Construction.Components;
  9. using JetBrains.Annotations;
  10. using Robust.Shared.Map;
  11. using Robust.Shared.Map.Components;
  12. namespace Content.Server.Atmos.Piping.Unary.EntitySystems
  13. {
  14. [UsedImplicitly]
  15. public sealed class GasPortableSystem : EntitySystem
  16. {
  17. [Dependency] private readonly SharedMapSystem _mapSystem = default!;
  18. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  19. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  20. public override void Initialize()
  21. {
  22. base.Initialize();
  23. SubscribeLocalEvent<GasPortableComponent, AnchorAttemptEvent>(OnPortableAnchorAttempt);
  24. // Shouldn't need re-anchored event.
  25. SubscribeLocalEvent<GasPortableComponent, AnchorStateChangedEvent>(OnAnchorChanged);
  26. }
  27. private void OnPortableAnchorAttempt(EntityUid uid, GasPortableComponent component, AnchorAttemptEvent args)
  28. {
  29. if (!EntityManager.TryGetComponent(uid, out TransformComponent? transform))
  30. return;
  31. // If we can't find any ports, cancel the anchoring.
  32. if (!FindGasPortIn(transform.GridUid, transform.Coordinates, out _))
  33. args.Cancel();
  34. }
  35. private void OnAnchorChanged(EntityUid uid, GasPortableComponent portable, ref AnchorStateChangedEvent args)
  36. {
  37. if (!_nodeContainer.TryGetNode(uid, portable.PortName, out PipeNode? portableNode))
  38. return;
  39. portableNode.ConnectionsEnabled = args.Anchored;
  40. }
  41. public bool FindGasPortIn(EntityUid? gridId, EntityCoordinates coordinates, [NotNullWhen(true)] out GasPortComponent? port)
  42. {
  43. port = null;
  44. if (!TryComp<MapGridComponent>(gridId, out var grid))
  45. return false;
  46. foreach (var entityUid in _mapSystem.GetLocal(gridId.Value, grid, coordinates))
  47. {
  48. if (EntityManager.TryGetComponent(entityUid, out port))
  49. {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. }
  56. }