Files
tbd-station-14/Content.Server/Atmos/Piping/EntitySystems/AtmosPipeAppearanceSystem.cs
Tayrtahn 6f89c2c455 Cleanup warnings in AtmosPipeAppearanceSystem (#37706)
* Cleanup 2 warnings in AtmosPipeAppearanceSystem

* Let's use the Entity<T> version

* todo comment
2025-05-21 21:06:06 -04:00

78 lines
2.6 KiB
C#

using Content.Server.NodeContainer;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.NodeContainer.Nodes;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.NodeContainer;
using Robust.Shared.Map.Components;
namespace Content.Server.Atmos.Piping.EntitySystems;
public sealed class AtmosPipeAppearanceSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedMapSystem _map = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PipeAppearanceComponent, NodeGroupsRebuilt>(OnNodeUpdate);
}
private void OnNodeUpdate(EntityUid uid, PipeAppearanceComponent component, ref NodeGroupsRebuilt args)
{
UpdateAppearance(args.NodeOwner);
}
private void UpdateAppearance(EntityUid uid, AppearanceComponent? appearance = null, NodeContainerComponent? container = null,
TransformComponent? xform = null)
{
if (!Resolve(uid, ref appearance, ref container, ref xform, false))
return;
if (!TryComp<MapGridComponent>(xform.GridUid, out var grid))
return;
// get connected entities
var anyPipeNodes = false;
HashSet<EntityUid> connected = new();
foreach (var node in container.Nodes.Values)
{
if (node is not PipeNode)
continue;
anyPipeNodes = true;
foreach (var connectedNode in node.ReachableNodes)
{
if (connectedNode is PipeNode)
connected.Add(connectedNode.Owner);
}
}
if (!anyPipeNodes)
return;
// find the cardinal directions of any connected entities
var netConnectedDirections = PipeDirection.None;
var tile = _map.TileIndicesFor((xform.GridUid.Value, grid), xform.Coordinates);
foreach (var neighbour in connected)
{
// TODO z-levels, pipes across grids - we shouldn't assume that the neighboring tile's transform is on the same grid
var otherTile = _map.TileIndicesFor((xform.GridUid.Value, grid), Transform(neighbour).Coordinates);
netConnectedDirections |= (otherTile - tile) switch
{
(0, 1) => PipeDirection.North,
(0, -1) => PipeDirection.South,
(1, 0) => PipeDirection.East,
(-1, 0) => PipeDirection.West,
_ => PipeDirection.None
};
}
_appearance.SetData(uid, PipeVisuals.VisualState, netConnectedDirections, appearance);
}
}