Refactor Barotrauma to be ECS. (#4674)

- Refactor IPressureProtection to be two different ECS events.
This commit is contained in:
Vera Aguilera Puerto
2021-09-22 13:02:25 +02:00
committed by GitHub
parent 6891c32eb5
commit 246fda53c5
8 changed files with 217 additions and 144 deletions

View File

@@ -17,11 +17,9 @@ namespace Content.Server.Atmos.Components
[ViewVariables]
[ComponentDependency] private readonly TemperatureComponent? _temperatureComponent = null;
[ViewVariables]
[ComponentDependency] private readonly BarotraumaComponent? _barotraumaComponent = null;
public void Update(GasMixture air, float frameDelta, AtmosphereSystem atmosphereSystem)
{
// TODO: I'm coming for you next, TemperatureComponent... Fear me for I am death, destroyer of shitcode.
if (_temperatureComponent != null)
{
var temperatureDelta = air.Temperature - _temperatureComponent.CurrentTemperature;
@@ -30,8 +28,6 @@ namespace Content.Server.Atmos.Components
_temperatureComponent.ReceiveHeat(heat);
_temperatureComponent.Update();
}
_barotraumaComponent?.Update(air.Pressure);
}
}
}

View File

@@ -1,9 +1,3 @@
using System;
using System.Runtime.CompilerServices;
using Content.Server.Alert;
using Content.Server.Pressure;
using Content.Shared.Alert;
using Content.Shared.Atmos;
using Content.Shared.Damage;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -22,75 +16,5 @@ namespace Content.Server.Atmos.Components
[DataField("damage", required: true)]
[ViewVariables(VVAccess.ReadWrite)]
public DamageSpecifier Damage = default!;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(float airPressure)
{
if (!Owner.HasComponent<DamageableComponent>()) return;
var status = Owner.GetComponentOrNull<ServerAlertsComponent>();
var highPressureMultiplier = 1f;
var lowPressureMultiplier = 1f;
foreach (var protection in Owner.GetAllComponents<IPressureProtection>())
{
highPressureMultiplier *= protection.HighPressureMultiplier;
lowPressureMultiplier *= protection.LowPressureMultiplier;
}
var pressure = MathF.Max(airPressure, 1f);
switch (pressure)
{
// Low pressure.
case var p when p <= Atmospherics.WarningLowPressure:
pressure *= lowPressureMultiplier;
if (pressure > Atmospherics.WarningLowPressure)
goto default;
// Deal damage and ignore resistances. Resistance to pressure damage should be done via pressure protection gear.
EntitySystem.Get<DamageableSystem>().TryChangeDamage(Owner.Uid, Damage * Atmospherics.LowPressureDamage, true);
if (status == null) break;
if (pressure <= Atmospherics.HazardLowPressure)
{
status.ShowAlert(AlertType.LowPressure, 2);
break;
}
status.ShowAlert(AlertType.LowPressure, 1);
break;
// High pressure.
case var p when p >= Atmospherics.WarningHighPressure:
pressure *= highPressureMultiplier;
if(pressure < Atmospherics.WarningHighPressure)
goto default;
var damageScale = (int) MathF.Min((pressure / Atmospherics.HazardHighPressure) * Atmospherics.PressureDamageCoefficient, Atmospherics.MaxHighPressureDamage);
// Deal damage and ignore resistances. Resistance to pressure damage should be done via pressure protection gear.
EntitySystem.Get<DamageableSystem>().TryChangeDamage(Owner.Uid, Damage * damageScale, true);
if (status == null) break;
if (pressure >= Atmospherics.HazardHighPressure)
{
status.ShowAlert(AlertType.HighPressure, 2);
break;
}
status.ShowAlert(AlertType.HighPressure, 1);
break;
// Normal pressure.
default:
status?.ClearAlertCategory(AlertCategory.Pressure);
break;
}
}
}
}

View File

@@ -1,21 +1,24 @@
using Content.Server.Pressure;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Atmos.Components
{
[RegisterComponent]
public class PressureProtectionComponent : Component, IPressureProtection
public class PressureProtectionComponent : Component
{
public override string Name => "PressureProtection";
[ViewVariables]
[DataField("highPressureMultiplier")]
public float HighPressureMultiplier { get; private set; } = 1f;
public float HighPressureMultiplier { get; } = 1f;
[DataField("highPressureModifier")]
public float HighPressureModifier { get; } = 0f;
[ViewVariables]
[DataField("lowPressureMultiplier")]
public float LowPressureMultiplier { get; private set; } = 1f;
public float LowPressureMultiplier { get; } = 1f;
[DataField("lowPressureModifier")]
public float LowPressureModifier { get; } = 0f;
}
}