NodeGroupSystem.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Content.Shared.NodeContainer;
  4. using JetBrains.Annotations;
  5. using Robust.Client.Graphics;
  6. using Robust.Client.Input;
  7. using Robust.Client.ResourceManagement;
  8. using Robust.Shared.GameObjects;
  9. using Robust.Shared.IoC;
  10. using Robust.Shared.Map;
  11. namespace Content.Client.NodeContainer
  12. {
  13. [UsedImplicitly]
  14. public sealed class NodeGroupSystem : EntitySystem
  15. {
  16. [Dependency] private readonly IOverlayManager _overlayManager = default!;
  17. [Dependency] private readonly EntityLookupSystem _entityLookup = default!;
  18. [Dependency] private readonly IMapManager _mapManager = default!;
  19. [Dependency] private readonly IInputManager _inputManager = default!;
  20. [Dependency] private readonly IResourceCache _resourceCache = default!;
  21. public bool VisEnabled { get; private set; }
  22. public Dictionary<int, NodeVis.GroupData> Groups { get; } = new();
  23. public HashSet<string> Filtered { get; } = new();
  24. public Dictionary<EntityUid, (NodeVis.GroupData group, NodeVis.NodeDatum node)[]>
  25. Entities { get; private set; } = new();
  26. public Dictionary<(int group, int node), NodeVis.NodeDatum> NodeLookup { get; private set; } = new();
  27. public override void Initialize()
  28. {
  29. base.Initialize();
  30. SubscribeNetworkEvent<NodeVis.MsgData>(DataMsgHandler);
  31. }
  32. public override void Shutdown()
  33. {
  34. base.Shutdown();
  35. _overlayManager.RemoveOverlay<NodeVisualizationOverlay>();
  36. }
  37. private void DataMsgHandler(NodeVis.MsgData ev)
  38. {
  39. if (!VisEnabled)
  40. return;
  41. foreach (var deletion in ev.GroupDeletions)
  42. {
  43. Groups.Remove(deletion);
  44. }
  45. foreach (var group in ev.Groups)
  46. {
  47. Groups.Add(group.NetId, group);
  48. }
  49. foreach (var (groupId, debugData) in ev.GroupDataUpdates)
  50. {
  51. if (Groups.TryGetValue(groupId, out var group))
  52. {
  53. group.DebugData = debugData;
  54. }
  55. }
  56. Entities = Groups.Values
  57. .SelectMany(g => g.Nodes, (data, nodeData) => (data, nodeData))
  58. .GroupBy(n => GetEntity(n.nodeData.Entity))
  59. .ToDictionary(g => g.Key, g => g.ToArray());
  60. NodeLookup = Groups.Values
  61. .SelectMany(g => g.Nodes, (data, nodeData) => (data, nodeData))
  62. .ToDictionary(n => (n.data.NetId, n.nodeData.NetId), n => n.nodeData);
  63. }
  64. public void SetVisEnabled(bool enabled)
  65. {
  66. VisEnabled = enabled;
  67. RaiseNetworkEvent(new NodeVis.MsgEnable(enabled));
  68. if (enabled)
  69. {
  70. var overlay = new NodeVisualizationOverlay(
  71. this,
  72. _entityLookup,
  73. _mapManager,
  74. _inputManager,
  75. _resourceCache,
  76. EntityManager);
  77. _overlayManager.AddOverlay(overlay);
  78. }
  79. else
  80. {
  81. Groups.Clear();
  82. Entities.Clear();
  83. }
  84. }
  85. }
  86. }