2020-09-13 14:23:52 +02:00
using System.Linq ;
using Content.Server.Atmos ;
2020-08-27 09:45:27 -06:00
using Content.Server.GameObjects.Components.NodeContainer ;
using Content.Server.GameObjects.Components.NodeContainer.Nodes ;
using Content.Server.GameObjects.EntitySystems ;
2020-09-21 17:51:07 +02:00
using Content.Shared.GameObjects.Components.Atmos ;
2020-09-12 06:26:50 -06:00
using Robust.Server.GameObjects ;
2020-08-27 09:45:27 -06:00
using Robust.Shared.GameObjects.Systems ;
2020-09-06 16:11:53 +02:00
using Robust.Shared.Interfaces.GameObjects ;
using Robust.Shared.IoC ;
2020-09-13 14:23:52 +02:00
using Robust.Shared.Log ;
using Robust.Shared.ViewVariables ;
2020-08-27 09:45:27 -06:00
2020-09-13 14:23:52 +02:00
namespace Content.Server.GameObjects.Components.Atmos.Piping.Vents
2020-08-27 09:45:27 -06:00
{
/// <summary>
/// Transfers gas from a <see cref="PipeNode"/> to the tile it is on.
/// </summary>
public abstract class BaseVentComponent : PipeNetDeviceComponent
{
2020-09-06 16:11:53 +02:00
[Dependency] private readonly IEntityManager _entityManager = default ! ;
2020-08-27 09:45:27 -06:00
[ViewVariables]
private PipeNode _ventInlet ;
private AtmosphereSystem _atmosSystem ;
2020-09-12 06:26:50 -06:00
[ViewVariables(VVAccess.ReadWrite)]
public bool VentEnabled
{
get = > _ventEnabled ;
set
{
_ventEnabled = value ;
UpdateAppearance ( ) ;
}
}
private bool _ventEnabled = true ;
private AppearanceComponent _appearance ;
2020-08-27 09:45:27 -06:00
public override void Initialize ( )
{
base . Initialize ( ) ;
_atmosSystem = EntitySystem . Get < AtmosphereSystem > ( ) ;
if ( ! Owner . TryGetComponent < NodeContainerComponent > ( out var container ) )
{
JoinedGridAtmos ? . RemovePipeNetDevice ( this ) ;
Logger . Error ( $"{typeof(BaseVentComponent)} on entity {Owner.Uid} did not have a {nameof(NodeContainerComponent)}." ) ;
return ;
}
_ventInlet = container . Nodes . OfType < PipeNode > ( ) . FirstOrDefault ( ) ;
if ( _ventInlet = = null )
{
JoinedGridAtmos ? . RemovePipeNetDevice ( this ) ;
Logger . Error ( $"{typeof(BaseVentComponent)} on entity {Owner.Uid} could not find compatible {nameof(PipeNode)}s on its {nameof(NodeContainerComponent)}." ) ;
return ;
}
2020-09-12 06:26:50 -06:00
Owner . TryGetComponent ( out _appearance ) ;
UpdateAppearance ( ) ;
2020-08-27 09:45:27 -06:00
}
public override void Update ( )
{
2020-09-12 06:26:50 -06:00
if ( ! VentEnabled )
return ;
2020-09-06 16:11:53 +02:00
var tileAtmos = Owner . Transform . Coordinates . GetTileAtmosphere ( _entityManager ) ;
2020-08-27 09:45:27 -06:00
if ( tileAtmos = = null )
return ;
VentGas ( _ventInlet . Air , tileAtmos . Air ) ;
_atmosSystem . GetGridAtmosphere ( Owner . Transform . GridID ) . Invalidate ( tileAtmos . GridIndices ) ;
}
protected abstract void VentGas ( GasMixture inletGas , GasMixture outletGas ) ;
2020-09-12 06:26:50 -06:00
private void UpdateAppearance ( )
{
_appearance ? . SetData ( VentVisuals . VisualState , new VentVisualState ( VentEnabled ) ) ;
}
2020-08-27 09:45:27 -06:00
}
}