2021-07-12 09:47:59 +02:00
using Content.Server.Atmos.Piping.Binary.Components ;
using Content.Server.NodeContainer ;
using Content.Server.NodeContainer.Nodes ;
using Content.Shared.ActionBlocker ;
using Content.Shared.Atmos.Piping ;
2021-11-06 17:32:33 -07:00
using Content.Shared.Audio ;
2021-10-19 21:46:31 +00:00
using Content.Shared.Examine ;
2021-07-12 09:47:59 +02:00
using Content.Shared.Interaction ;
using Content.Shared.Interaction.Helpers ;
using JetBrains.Annotations ;
using Robust.Server.GameObjects ;
2021-11-06 17:32:33 -07:00
using Robust.Shared.Audio ;
2021-07-12 09:47:59 +02:00
using Robust.Shared.GameObjects ;
2021-12-03 14:20:34 +01:00
using Robust.Shared.IoC ;
2021-10-19 21:46:31 +00:00
using Robust.Shared.Localization ;
2021-11-06 17:32:33 -07:00
using Robust.Shared.Player ;
2021-07-12 09:47:59 +02:00
namespace Content.Server.Atmos.Piping.Binary.EntitySystems
{
[UsedImplicitly]
public class GasValveSystem : EntitySystem
{
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < GasValveComponent , ComponentStartup > ( OnStartup ) ;
SubscribeLocalEvent < GasValveComponent , ActivateInWorldEvent > ( OnActivate ) ;
2021-10-19 21:46:31 +00:00
SubscribeLocalEvent < GasValveComponent , ExaminedEvent > ( OnExamined ) ;
}
private void OnExamined ( EntityUid uid , GasValveComponent valve , ExaminedEvent args )
{
2021-12-03 15:53:09 +01:00
if ( ! IoCManager . Resolve < IEntityManager > ( ) . GetComponent < TransformComponent > ( valve . Owner ) . Anchored | | ! args . IsInDetailsRange ) // Not anchored? Out of range? No status.
2021-10-19 21:46:31 +00:00
return ;
if ( Loc . TryGetString ( "gas-valve-system-examined" , out var str ,
( "statusColor" , valve . Open ? "green" : "orange" ) ,
( "open" , valve . Open )
) )
args . PushMarkup ( str ) ;
2021-07-12 09:47:59 +02:00
}
private void OnStartup ( EntityUid uid , GasValveComponent component , ComponentStartup args )
{
// We call set in startup so it sets the appearance, node state, etc.
Set ( uid , component , component . Open ) ;
}
private void OnActivate ( EntityUid uid , GasValveComponent component , ActivateInWorldEvent args )
{
2021-12-03 15:53:09 +01:00
if ( args . User . InRangeUnobstructed ( args . Target ) & & Get < ActionBlockerSystem > ( ) . CanInteract ( args . User ) )
2021-11-06 17:32:33 -07:00
{
2021-07-12 09:47:59 +02:00
Toggle ( uid , component ) ;
2021-11-06 17:32:33 -07:00
SoundSystem . Play ( Filter . Pvs ( component . Owner ) , component . _valveSound . GetSound ( ) , component . Owner , AudioHelpers . WithVariation ( 0.25f ) ) ;
}
2021-07-12 09:47:59 +02:00
}
public void Set ( EntityUid uid , GasValveComponent component , bool value )
{
component . Open = value ;
2021-09-28 13:35:29 +02:00
if ( EntityManager . TryGetComponent ( uid , out NodeContainerComponent ? nodeContainer )
2021-07-12 09:47:59 +02:00
& & nodeContainer . TryGetNode ( component . PipeName , out PipeNode ? pipe ) )
{
pipe . ConnectionsEnabled = component . Open ;
}
}
public void Toggle ( EntityUid uid , GasValveComponent component )
{
Set ( uid , component , ! component . Open ) ;
}
}
}