PipeNetDeviceComponent refactor (#2912)

* Removes inheritance of PipeNetDeviceComponent

* Enables nullable in piping

* Piping error messages report the prototype

* Moves PipeNetDevice updating to ComponentMessage

* Build fix

* Review fixes

* review fix 2

Co-authored-by: py01 <pyronetics01@gmail.com>
This commit is contained in:
py01
2021-01-07 00:37:17 -06:00
committed by GitHub
parent 2b195fccb9
commit 8fa8de36ed
5 changed files with 180 additions and 109 deletions

View File

@@ -1,11 +1,12 @@
using System.Linq;
#nullable enable
using System.Linq;
using Content.Server.Atmos;
using Content.Server.GameObjects.Components.NodeContainer;
using Content.Server.GameObjects.Components.NodeContainer.Nodes;
using Content.Shared.GameObjects.Components.Atmos;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Log;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -15,7 +16,7 @@ namespace Content.Server.GameObjects.Components.Atmos.Piping.Pumps
/// <summary>
/// Transfer gas from one <see cref="PipeNode"/> to another.
/// </summary>
public abstract class BasePumpComponent : PipeNetDeviceComponent
public abstract class BasePumpComponent : Component
{
/// <summary>
/// If the pump is currently pumping.
@@ -33,87 +34,88 @@ namespace Content.Server.GameObjects.Components.Atmos.Piping.Pumps
private bool _pumpEnabled;
/// <summary>
/// Needs to be same <see cref="PipeDirection"/> as that of a <see cref="Pipe"/> on this entity.
/// Needs to be same <see cref="PipeDirection"/> as that of a <see cref="PipeNode"/> on this entity.
/// </summary>
[ViewVariables]
private PipeDirection _inletDirection;
private PipeDirection _initialInletDirection;
/// <summary>
/// Needs to be same <see cref="PipeDirection"/> as that of a <see cref="Pipe"/> on this entity.
/// Needs to be same <see cref="PipeDirection"/> as that of a <see cref="PipeNode"/> on this entity.
/// </summary>
[ViewVariables]
private PipeDirection _outletDirection;
private PipeDirection _initialOutletDirection;
[ViewVariables]
private PipeNode _inletPipe;
private PipeNode? _inletPipe;
[ViewVariables]
private PipeNode _outletPipe;
private PipeNode? _outletPipe;
private AppearanceComponent _appearance;
private AppearanceComponent? _appearance;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _inletDirection, "inletDirection", PipeDirection.None);
serializer.DataField(ref _outletDirection, "outletDirection", PipeDirection.None);
serializer.DataField(ref _initialInletDirection, "inletDirection", PipeDirection.None);
serializer.DataField(ref _initialOutletDirection, "outletDirection", PipeDirection.None);
serializer.DataField(ref _pumpEnabled, "pumpEnabled", false);
}
public override void Initialize()
{
base.Initialize();
UpdatePipes();
Owner.EntityManager.EventBus.SubscribeEvent<RotateEvent>(EventSource.Local, this, RotateEvent);
Owner.EnsureComponentWarn<PipeNetDeviceComponent>();
SetPipes();
Owner.TryGetComponent(out _appearance);
UpdateAppearance();
}
public override void Update()
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case PipeNetUpdateMessage:
Update();
break;
}
}
public void Update()
{
if (!PumpEnabled)
return;
if (_inletPipe == null || _outletPipe == null)
return;
PumpGas(_inletPipe.Air, _outletPipe.Air);
}
protected abstract void PumpGas(GasMixture inletGas, GasMixture outletGas);
private void RotateEvent(RotateEvent ev)
{
if (ev.Sender != Owner || ev.NewRotation == ev.OldRotation)
return;
var diff = ev.NewRotation - ev.OldRotation;
_inletDirection = _inletDirection.RotatePipeDirection(diff);
_outletDirection = _outletDirection.RotatePipeDirection(diff);
UpdatePipes();
}
private void UpdateAppearance()
{
if (_inletPipe == null || _outletPipe == null) return;
_appearance?.SetData(PumpVisuals.VisualState, new PumpVisualState(_inletDirection, _outletDirection, _inletPipe.ConduitLayer, _outletPipe.ConduitLayer, PumpEnabled));
_appearance?.SetData(PumpVisuals.VisualState, new PumpVisualState(_initialInletDirection, _initialOutletDirection, _inletPipe.ConduitLayer, _outletPipe.ConduitLayer, PumpEnabled));
}
private void UpdatePipes()
private void SetPipes()
{
_inletPipe = null;
_outletPipe = null;
if (!Owner.TryGetComponent<NodeContainerComponent>(out var container))
{
JoinedGridAtmos?.RemovePipeNetDevice(this);
Logger.Error($"{typeof(BasePumpComponent)} on entity {Owner.Uid} did not have a {nameof(NodeContainerComponent)}.");
Logger.Error($"{typeof(BasePumpComponent)} on {Owner?.Prototype?.ID}, Uid {Owner?.Uid} did not have a {nameof(NodeContainerComponent)}.");
return;
}
var pipeNodes = container.Nodes.OfType<PipeNode>();
_inletPipe = pipeNodes.Where(pipe => pipe.PipeDirection == _inletDirection).FirstOrDefault();
_outletPipe = pipeNodes.Where(pipe => pipe.PipeDirection == _outletDirection).FirstOrDefault();
if (_inletPipe == null | _outletPipe == null)
_inletPipe = pipeNodes.Where(pipe => pipe.PipeDirection == _initialInletDirection).FirstOrDefault();
_outletPipe = pipeNodes.Where(pipe => pipe.PipeDirection == _initialOutletDirection).FirstOrDefault();
if (_inletPipe == null || _outletPipe == null)
{
JoinedGridAtmos?.RemovePipeNetDevice(this);
Logger.Error($"{typeof(BasePumpComponent)} on entity {Owner.Uid} could not find compatible {nameof(PipeNode)}s on its {nameof(NodeContainerComponent)}.");
Logger.Error($"{typeof(BasePumpComponent)} on {Owner?.Prototype?.ID}, Uid {Owner?.Uid} could not find compatible {nameof(PipeNode)}s on its {nameof(NodeContainerComponent)}.");
return;
}
}