2021-06-19 13:25:05 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using Content.Server.Atmos.Piping.Binary.Components;
|
|
|
|
|
using Content.Server.Atmos.Piping.Unary.Components;
|
2021-06-19 19:41:26 -07:00
|
|
|
using Content.Server.Construction.Components;
|
2021-06-19 13:25:05 +02:00
|
|
|
using Content.Server.NodeContainer;
|
2021-07-04 18:11:52 +02:00
|
|
|
using Content.Server.NodeContainer.Nodes;
|
2021-06-19 13:25:05 +02:00
|
|
|
using Content.Shared.Atmos.Piping.Unary.Components;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class GasPortableSystem : EntitySystem
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
|
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<GasPortableComponent, AnchorAttemptEvent>(OnPortableAnchorAttempt);
|
2022-01-13 06:49:28 +13:00
|
|
|
SubscribeLocalEvent<GasPortableComponent, AnchorStateChangedEvent>(OnAnchorChanged);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPortableAnchorAttempt(EntityUid uid, GasPortableComponent component, AnchorAttemptEvent args)
|
|
|
|
|
{
|
2021-11-08 12:37:32 +01:00
|
|
|
if (!EntityManager.TryGetComponent(uid, out TransformComponent? transform))
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If we can't find any ports, cancel the anchoring.
|
|
|
|
|
if(!FindGasPortIn(transform.GridID, transform.Coordinates, out _))
|
|
|
|
|
args.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 06:49:28 +13:00
|
|
|
private void OnAnchorChanged(EntityUid uid, GasPortableComponent portable, ref AnchorStateChangedEvent args)
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
if (!EntityManager.TryGetComponent(uid, out NodeContainerComponent? nodeContainer))
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!nodeContainer.TryGetNode(portable.PortName, out PipeNode? portableNode))
|
|
|
|
|
return;
|
|
|
|
|
|
2022-01-13 06:49:28 +13:00
|
|
|
portableNode.ConnectionsEnabled = args.Anchored;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2022-01-13 06:49:28 +13:00
|
|
|
appearance.SetData(GasPortableVisuals.ConnectedState, args.Anchored);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool FindGasPortIn(GridId gridId, EntityCoordinates coordinates, [NotNullWhen(true)] out GasPortComponent? port)
|
|
|
|
|
{
|
|
|
|
|
port = null;
|
|
|
|
|
|
|
|
|
|
if (!gridId.IsValid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var grid = _mapManager.GetGrid(gridId);
|
|
|
|
|
|
|
|
|
|
foreach (var entityUid in grid.GetLocal(coordinates))
|
|
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
if (EntityManager.TryGetComponent<GasPortComponent>(entityUid, out port))
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|