ECS BloodstreamComponent (#5629)
This commit is contained in:
@@ -3,6 +3,7 @@ using Content.Server.Atmos;
|
||||
using Content.Server.Atmos.Components;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Body.Components;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Body.Components;
|
||||
@@ -188,7 +189,7 @@ namespace Content.Server.Body.Behavior
|
||||
return;
|
||||
}
|
||||
|
||||
bloodstream.PumpToxins(Air);
|
||||
EntitySystem.Get<BloodstreamSystem>().PumpToxins(Body.OwnerUid, Air, bloodstream);
|
||||
|
||||
var lungRemoved = Air.RemoveRatio(0.5f);
|
||||
EntitySystem.Get<AtmosphereSystem>().Merge(to, lungRemoved);
|
||||
|
||||
@@ -1,104 +1,34 @@
|
||||
using System;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Shared.Atmos;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedBloodstreamComponent))]
|
||||
public class BloodstreamComponent : SharedBloodstreamComponent, IGasMixtureHolder
|
||||
[RegisterComponent, Friend(typeof(BloodstreamSystem))]
|
||||
public class BloodstreamComponent : Component, IGasMixtureHolder
|
||||
{
|
||||
public override string Name => "Bloodstream";
|
||||
|
||||
/// <summary>
|
||||
/// Max volume of internal solution storage
|
||||
/// </summary>
|
||||
[DataField("maxVolume")] [ViewVariables]
|
||||
private FixedPoint2 _initialMaxVolume = FixedPoint2.New(250);
|
||||
[DataField("maxVolume")]
|
||||
public FixedPoint2 InitialMaxVolume = FixedPoint2.New(250);
|
||||
|
||||
/// <summary>
|
||||
/// Internal solution for reagent storage
|
||||
/// </summary>
|
||||
[ViewVariables] private Solution? _internalSolution;
|
||||
|
||||
/// <summary>
|
||||
/// Empty volume of internal solution
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public FixedPoint2 EmptyVolume => _internalSolution?.AvailableVolume ?? FixedPoint2.Zero;
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public Solution Solution = default!;
|
||||
|
||||
[ViewVariables]
|
||||
public GasMixture Air { get; set; } = new(6)
|
||||
{ Temperature = Atmospherics.NormalBodyTemperature };
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_internalSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner.Uid, DefaultSolutionName);
|
||||
if (_internalSolution != null)
|
||||
{
|
||||
_internalSolution.MaxVolume = _initialMaxVolume;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to transfer provided solution to internal solution.
|
||||
/// Only supports complete transfers
|
||||
/// </summary>
|
||||
/// <param name="solution">Solution to be transferred</param>
|
||||
/// <returns>Whether or not transfer was a success</returns>
|
||||
public override bool TryTransferSolution(Solution solution)
|
||||
{
|
||||
// For now doesn't support partial transfers
|
||||
var current = _internalSolution?.CurrentVolume ?? FixedPoint2.Zero;
|
||||
var max = _internalSolution?.MaxVolume ?? FixedPoint2.Zero;
|
||||
if (solution.TotalVolume + current > max)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(Owner.Uid, _internalSolution, solution);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void PumpToxins(GasMixture to)
|
||||
{
|
||||
var atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
|
||||
var respiratorSystem = EntitySystem.Get<RespiratorSystem>();
|
||||
|
||||
if (!Owner.TryGetComponent(out RespiratorComponent? metabolism))
|
||||
{
|
||||
atmosphereSystem.Merge(to, Air);
|
||||
Air.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var toxins = respiratorSystem.Clean(OwnerUid, metabolism, this);
|
||||
var toOld = new float[to.Moles.Length];
|
||||
Array.Copy(to.Moles, toOld, toOld.Length);
|
||||
|
||||
atmosphereSystem.Merge(to, toxins);
|
||||
|
||||
for (var i = 0; i < toOld.Length; i++)
|
||||
{
|
||||
var newAmount = to.GetMoles(i);
|
||||
var oldAmount = toOld[i];
|
||||
var delta = newAmount - oldAmount;
|
||||
|
||||
toxins.AdjustMoles(i, -delta);
|
||||
}
|
||||
|
||||
atmosphereSystem.Merge(Air, toxins);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Content.Server.Body.Components
|
||||
/// From which solution will this metabolizer attempt to metabolize chemicals
|
||||
/// </summary>
|
||||
[DataField("solution")]
|
||||
public string SolutionName { get; set; } = SharedBloodstreamComponent.DefaultSolutionName;
|
||||
public string SolutionName { get; set; } = BloodstreamSystem.DefaultSolutionName;
|
||||
|
||||
/// <summary>
|
||||
/// Does this component use a solution on it's parent entity (the body) or itself
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace Content.Server.Body.Components
|
||||
/// What solution should this stomach push reagents into, on the body?
|
||||
/// </summary>
|
||||
[DataField("bodySolutionName")]
|
||||
public string BodySolutionName = SharedBloodstreamComponent.DefaultSolutionName;
|
||||
public string BodySolutionName = BloodstreamSystem.DefaultSolutionName;
|
||||
|
||||
/// <summary>
|
||||
/// Initial internal solution storage volume
|
||||
@@ -36,7 +36,7 @@ namespace Content.Server.Body.Components
|
||||
|
||||
/// <summary>
|
||||
/// Time in seconds between reagents being ingested and them being
|
||||
/// transferred to <see cref="SharedBloodstreamComponent"/>
|
||||
/// transferred to <see cref="BloodstreamComponent"/>
|
||||
/// </summary>
|
||||
[DataField("digestionDelay")]
|
||||
public float DigestionDelay = 20;
|
||||
|
||||
77
Content.Server/Body/Systems/BloodstreamSystem.cs
Normal file
77
Content.Server/Body/Systems/BloodstreamSystem.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Body.Components;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Server.Body.Systems;
|
||||
|
||||
public class BloodstreamSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
[Dependency] private readonly AtmosphereSystem _atmosSystem = default!;
|
||||
[Dependency] private readonly RespiratorSystem _respiratorSystem = default!;
|
||||
|
||||
public static string DefaultSolutionName = "bloodstream";
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<BloodstreamComponent, ComponentInit>(OnComponentInit);
|
||||
}
|
||||
|
||||
private void OnComponentInit(EntityUid uid, BloodstreamComponent component, ComponentInit args)
|
||||
{
|
||||
component.Solution = _solutionContainerSystem.EnsureSolution(uid, DefaultSolutionName);
|
||||
if (component.Solution != null)
|
||||
{
|
||||
component.Solution.MaxVolume = component.InitialMaxVolume;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to transfer provided solution to internal solution.
|
||||
/// </summary>
|
||||
public bool TryAddToBloodstream(EntityUid uid, Solution solution, BloodstreamComponent? component=null)
|
||||
{
|
||||
if (!Resolve(uid, ref component, false))
|
||||
return false;
|
||||
|
||||
return _solutionContainerSystem.TryAddSolution(uid, component.Solution, solution);
|
||||
}
|
||||
|
||||
public void PumpToxins(EntityUid uid, GasMixture to, BloodstreamComponent? blood=null, RespiratorComponent? respiration=null)
|
||||
{
|
||||
if (!Resolve(uid, ref blood))
|
||||
return;
|
||||
|
||||
if(!Resolve(uid, ref respiration, false))
|
||||
{
|
||||
_atmosSystem.Merge(to, blood.Air);
|
||||
blood.Air.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var toxins = _respiratorSystem.Clean(uid, respiration, blood);
|
||||
var toOld = new float[to.Moles.Length];
|
||||
Array.Copy(to.Moles, toOld, toOld.Length);
|
||||
|
||||
_atmosSystem.Merge(to, toxins);
|
||||
|
||||
for (var i = 0; i < toOld.Length; i++)
|
||||
{
|
||||
var newAmount = to.GetMoles(i);
|
||||
var oldAmount = toOld[i];
|
||||
var delta = newAmount - oldAmount;
|
||||
|
||||
toxins.AdjustMoles(i, -delta);
|
||||
}
|
||||
|
||||
_atmosSystem.Merge(blood.Air, toxins);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user