using System.Collections.Generic; using System.Linq; using Content.Shared.NodeContainer; using JetBrains.Annotations; using Robust.Client.Graphics; using Robust.Client.Input; using Robust.Client.ResourceManagement; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; namespace Content.Client.NodeContainer { [UsedImplicitly] public sealed class NodeGroupSystem : EntitySystem { [Dependency] private readonly IOverlayManager _overlayManager = default!; [Dependency] private readonly IEntityLookup _entityLookup = default!; [Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IInputManager _inputManager = default!; [Dependency] private readonly IResourceCache _resourceCache = default!; public bool VisEnabled { get; private set; } public Dictionary Groups { get; } = new(); public HashSet Filtered { get; } = new(); public Dictionary Entities { get; private set; } = new(); public Dictionary<(int group, int node), NodeVis.NodeDatum> NodeLookup { get; private set; } = new(); public override void Initialize() { base.Initialize(); SubscribeNetworkEvent(DataMsgHandler); } public override void Shutdown() { base.Shutdown(); _overlayManager.RemoveOverlay(); } private void DataMsgHandler(NodeVis.MsgData ev) { if (!VisEnabled) return; foreach (var deletion in ev.GroupDeletions) { Groups.Remove(deletion); } foreach (var group in ev.Groups) { Groups.Add(group.NetId, group); } Entities = Groups.Values .SelectMany(g => g.Nodes, (data, nodeData) => (data, nodeData)) .GroupBy(n => n.nodeData.Entity) .ToDictionary(g => g.Key, g => g.ToArray()); NodeLookup = Groups.Values .SelectMany(g => g.Nodes, (data, nodeData) => (data, nodeData)) .ToDictionary(n => (n.data.NetId, n.nodeData.NetId), n => n.nodeData); } public void SetVisEnabled(bool enabled) { VisEnabled = enabled; RaiseNetworkEvent(new NodeVis.MsgEnable(enabled)); if (enabled) { var overlay = new NodeVisualizationOverlay( this, _entityLookup, _mapManager, _inputManager, _resourceCache, EntityManager); _overlayManager.AddOverlay(overlay); } else { Groups.Clear(); Entities.Clear(); } } } }