* Converted all SnapGridPositionChangedEvent subscriptions to AnchorStateChangedEvent. * Fixes power tests with new anchored requirements. * Moved AnchorableComponent into construction. AnchorableComponent now uses Transform.Anchored. * Fixed bug with nodes, power works again. * Adds lifetime stages to Component. * Update Engine to v0.4.70.
93 lines
3.3 KiB
C#
93 lines
3.3 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Content.Server.Atmos.Piping.Binary.Components;
|
|
using Content.Server.Atmos.Piping.Unary.Components;
|
|
using Content.Server.Construction.Components;
|
|
using Content.Server.GameObjects.Components.NodeContainer.Nodes;
|
|
using Content.Server.NodeContainer;
|
|
using Content.Shared.Atmos.Piping.Unary.Components;
|
|
using JetBrains.Annotations;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Map;
|
|
|
|
namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|
{
|
|
[UsedImplicitly]
|
|
public class GasPortableSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<GasPortableComponent, AnchorAttemptEvent>(OnPortableAnchorAttempt);
|
|
SubscribeLocalEvent<GasPortableComponent, AnchoredEvent>(OnPortableAnchored);
|
|
SubscribeLocalEvent<GasPortableComponent, UnanchoredEvent>(OnPortableUnanchored);
|
|
}
|
|
|
|
private void OnPortableAnchorAttempt(EntityUid uid, GasPortableComponent component, AnchorAttemptEvent args)
|
|
{
|
|
if (!ComponentManager.TryGetComponent(uid, out ITransformComponent? transform))
|
|
return;
|
|
|
|
// If we can't find any ports, cancel the anchoring.
|
|
if(!FindGasPortIn(transform.GridID, transform.Coordinates, out _))
|
|
args.Cancel();
|
|
}
|
|
|
|
private void OnPortableAnchored(EntityUid uid, GasPortableComponent portable, AnchoredEvent args)
|
|
{
|
|
if (!ComponentManager.TryGetComponent(uid, out NodeContainerComponent? nodeContainer))
|
|
return;
|
|
|
|
if (!nodeContainer.TryGetNode(portable.PortName, out PipeNode? portableNode))
|
|
return;
|
|
|
|
portableNode.ConnectionsEnabled = true;
|
|
|
|
if (ComponentManager.TryGetComponent(uid, out AppearanceComponent? appearance))
|
|
{
|
|
appearance.SetData(GasPortableVisuals.ConnectedState, true);
|
|
}
|
|
}
|
|
|
|
private void OnPortableUnanchored(EntityUid uid, GasPortableComponent portable, UnanchoredEvent args)
|
|
{
|
|
if (!ComponentManager.TryGetComponent(uid, out NodeContainerComponent? nodeContainer))
|
|
return;
|
|
|
|
if (!nodeContainer.TryGetNode(portable.PortName, out PipeNode? portableNode))
|
|
return;
|
|
|
|
portableNode.ConnectionsEnabled = false;
|
|
|
|
if (ComponentManager.TryGetComponent(uid, out AppearanceComponent? appearance))
|
|
{
|
|
appearance.SetData(GasPortableVisuals.ConnectedState, false);
|
|
}
|
|
}
|
|
|
|
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))
|
|
{
|
|
if (ComponentManager.TryGetComponent<GasPortComponent>(entityUid, out port))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|