Add gas tank valve (#19830)

This commit is contained in:
Slava0135
2023-09-05 20:20:05 +03:00
committed by GitHub
parent b1eeb07da9
commit 81388f77ed
4 changed files with 96 additions and 20 deletions

View File

@@ -1,3 +1,4 @@
using System.Numerics;
using Content.Server.Atmos.Components;
using Content.Server.Body.Components;
using Content.Server.Body.Systems;
@@ -15,6 +16,9 @@ using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Content.Shared.Verbs;
using Robust.Shared.Physics.Systems;
namespace Content.Server.Atmos.EntitySystems
{
@@ -28,6 +32,8 @@ namespace Content.Server.Atmos.EntitySystems
[Dependency] private readonly SharedContainerSystem _containers = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private const float TimerDelay = 0.5f;
private float _timer = 0f;
@@ -45,6 +51,7 @@ namespace Content.Server.Atmos.EntitySystems
SubscribeLocalEvent<GasTankComponent, GasTankToggleInternalsMessage>(OnGasTankToggleInternals);
SubscribeLocalEvent<GasTankComponent, GasAnalyzerScanEvent>(OnAnalyzed);
SubscribeLocalEvent<GasTankComponent, PriceCalculationEvent>(OnGasTankPrice);
SubscribeLocalEvent<GasTankComponent, GetVerbsEvent<AlternativeVerb>>(OnGetAlternativeVerb);
}
private void OnGasShutdown(EntityUid uid, GasTankComponent component, ComponentShutdown args)
@@ -103,6 +110,7 @@ namespace Content.Server.Atmos.EntitySystems
args.PushMarkup(Loc.GetString("comp-gas-tank-examine", ("pressure", Math.Round(component.Air?.Pressure ?? 0))));
if (component.IsConnected)
args.PushMarkup(Loc.GetString("comp-gas-tank-connected"));
args.PushMarkup(Loc.GetString(component.IsValveOpen ? "comp-gas-tank-examine-open-valve" : "comp-gas-tank-examine-closed-valve"));
}
private void OnActionToggle(EntityUid uid, GasTankComponent component, ToggleActionEvent args)
@@ -123,27 +131,50 @@ namespace Content.Server.Atmos.EntitySystems
if (_timer < TimerDelay) return;
_timer -= TimerDelay;
foreach (var gasTank in EntityManager.EntityQuery<GasTankComponent>())
var query = EntityQueryEnumerator<GasTankComponent>();
while (query.MoveNext(out var uid, out var gasTank))
{
if (gasTank.IsValveOpen && !gasTank.IsLowPressure)
{
ReleaseGas(uid, gasTank);
}
if (gasTank.CheckUser)
{
gasTank.CheckUser = false;
if (Transform(gasTank.Owner).ParentUid != gasTank.User)
if (Transform(uid).ParentUid != gasTank.User)
{
DisconnectFromInternals(gasTank);
continue;
}
}
_atmosphereSystem.React(gasTank.Air, gasTank);
if (gasTank.Air != null)
{
_atmosphereSystem.React(gasTank.Air, gasTank);
}
CheckStatus(gasTank);
if (_ui.IsUiOpen(gasTank.Owner, SharedGasTankUiKey.Key))
if (_ui.IsUiOpen(uid, SharedGasTankUiKey.Key))
{
UpdateUserInterface(gasTank);
}
}
}
private void ReleaseGas(EntityUid uid, GasTankComponent component)
{
var removed = RemoveAirVolume(component, component.ValveOutputRate * TimerDelay);
var environment = _atmosphereSystem.GetContainingMixture(uid, false, true);
if (environment != null)
{
_atmosphereSystem.Merge(environment, removed);
}
var impulse = removed.TotalMoles * removed.Temperature;
_physics.ApplyLinearImpulse(uid, _random.NextAngle().ToWorldVec() * impulse);
_physics.ApplyAngularImpulse(uid, _random.NextFloat(-3f, 3f));
_audioSys.PlayPvs(component.RuptureSound, uid);
}
private void ToggleInternals(GasTankComponent component)
{
if (component.IsConnected)
@@ -183,7 +214,7 @@ namespace Content.Server.Atmos.EntitySystems
public bool CanConnectToInternals(GasTankComponent component)
{
var internals = GetInternalsComponent(component);
return internals != null && internals.BreathToolEntity != null;
return internals != null && internals.BreathToolEntity != null && !component.IsValveOpen;
}
public void ConnectToInternals(GasTankComponent component)
@@ -330,5 +361,21 @@ namespace Content.Server.Atmos.EntitySystems
{
args.Price += _atmosphereSystem.GetPrice(component.Air);
}
private void OnGetAlternativeVerb(EntityUid uid, GasTankComponent component, GetVerbsEvent<AlternativeVerb> args)
{
if (!args.CanAccess || !args.CanInteract || args.Hands == null)
return;
args.Verbs.Add(new AlternativeVerb()
{
Text = component.IsValveOpen ? Loc.GetString("comp-gas-tank-close-valve") : Loc.GetString("comp-gas-tank-open-valve"),
Act = () =>
{
component.IsValveOpen = !component.IsValveOpen;
_audioSys.PlayPvs(component.ValveSound, uid);
},
Disabled = component.IsConnected,
});
}
}
}