CableVisSystem.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Content.Server.NodeContainer;
  2. using Content.Server.NodeContainer.EntitySystems;
  3. using Content.Server.Power.Components;
  4. using Content.Server.Power.Nodes;
  5. using Content.Shared.Wires;
  6. using JetBrains.Annotations;
  7. using Robust.Shared.Map.Components;
  8. namespace Content.Server.Power.EntitySystems
  9. {
  10. [UsedImplicitly]
  11. public sealed class CableVisSystem : EntitySystem
  12. {
  13. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  14. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<CableVisComponent, NodeGroupsRebuilt>(UpdateAppearance);
  19. }
  20. private void UpdateAppearance(EntityUid uid, CableVisComponent cableVis, ref NodeGroupsRebuilt args)
  21. {
  22. if (!_nodeContainer.TryGetNode(uid, cableVis.Node, out CableNode? node))
  23. return;
  24. var transform = Transform(uid);
  25. if (!TryComp<MapGridComponent>(transform.GridUid, out var grid))
  26. return;
  27. var mask = WireVisDirFlags.None;
  28. var tile = grid.TileIndicesFor(transform.Coordinates);
  29. foreach (var reachable in node.ReachableNodes)
  30. {
  31. if (reachable is not CableNode)
  32. continue;
  33. var otherTransform = Transform(reachable.Owner);
  34. var otherTile = grid.TileIndicesFor(otherTransform.Coordinates);
  35. var diff = otherTile - tile;
  36. mask |= diff switch
  37. {
  38. (0, 1) => WireVisDirFlags.North,
  39. (0, -1) => WireVisDirFlags.South,
  40. (1, 0) => WireVisDirFlags.East,
  41. (-1, 0) => WireVisDirFlags.West,
  42. _ => WireVisDirFlags.None
  43. };
  44. }
  45. _appearance.SetData(uid, WireVisVisuals.ConnectedMask, mask);
  46. }
  47. }
  48. }