Containment Field Rework (#9312)

This commit is contained in:
keronshb
2022-08-05 00:22:37 -04:00
committed by GitHub
parent c06cbed71d
commit 0eece4b47f
15 changed files with 631 additions and 666 deletions

View File

@@ -1,11 +1,24 @@
using Content.Shared.Singularity.Components;
namespace Content.Server.Singularity.Components
namespace Content.Server.Singularity.Components;
[RegisterComponent]
[ComponentReference(typeof(SharedContainmentFieldComponent))]
public sealed class ContainmentFieldComponent : SharedContainmentFieldComponent
{
[RegisterComponent]
[ComponentReference(typeof(SharedContainmentFieldComponent))]
public sealed class ContainmentFieldComponent : SharedContainmentFieldComponent
{
public ContainmentFieldConnection? Parent;
}
/// <summary>
/// The throw force for the field if an entity collides with it
/// The lighter the mass the further it will throw. 5 mass will go about 4 tiles out, 70 mass goes only a couple tiles.
/// </summary>
[ViewVariables]
[DataField("throwForce")]
public float ThrowForce = 100f;
/// <summary>
/// This shouldn't be at 99999 or higher to prevent the singulo glitching out
/// Will throw anything at the supplied mass or less that collides with the field.
/// </summary>
[ViewVariables]
[DataField("maxMass")]
public float MaxMass = 10000f;
}

View File

@@ -1,112 +0,0 @@
using System.Threading;
using Content.Server.Singularity.EntitySystems;
using Content.Shared.Singularity.Components;
using Robust.Server.GameObjects;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.Singularity.Components
{
public sealed class ContainmentFieldConnection : IDisposable
{
public ContainmentFieldGeneratorComponent Generator1;
public ContainmentFieldGeneratorComponent Generator2;
private readonly List<EntityUid> _fields = new();
private int _sharedEnergyPool;
private readonly CancellationTokenSource _powerDecreaseCancellationTokenSource = new();
public int SharedEnergyPool
{
get => _sharedEnergyPool;
set
{
_sharedEnergyPool = Math.Clamp(value, 0, 25);
if (_sharedEnergyPool == 0)
{
Dispose();
}
}
}
public ContainmentFieldConnection(ContainmentFieldGeneratorComponent generator1, ContainmentFieldGeneratorComponent generator2)
{
Generator1 = generator1;
Generator2 = generator2;
//generateFields
var pos1 = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(generator1.Owner).Coordinates;
var pos2 = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(generator2.Owner).Coordinates;
if (pos1 == pos2)
{
Dispose();
return;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
var delta = (pos2 - pos1).Position;
var dirVec = delta.Normalized;
var stopDist = delta.Length;
var currentOffset = dirVec;
while (currentOffset.Length < stopDist)
{
var currentCoords = pos1.Offset(currentOffset);
var newEnt = entityManager.SpawnEntity("ContainmentField", currentCoords);
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent<ContainmentFieldComponent?>(newEnt, out var containmentFieldComponent))
{
Logger.Error("While creating Fields in ContainmentFieldConnection, a ContainmentField without a ContainmentFieldComponent was created. Deleting newly spawned ContainmentField...");
IoCManager.Resolve<IEntityManager>().DeleteEntity(newEnt);
continue;
}
containmentFieldComponent.Parent = this;
IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(newEnt).WorldRotation = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(generator1.Owner).WorldRotation + dirVec.ToWorldAngle();
_fields.Add(newEnt);
currentOffset += dirVec;
}
Timer.SpawnRepeating(1000, () => { SharedEnergyPool--; }, _powerDecreaseCancellationTokenSource.Token);
}
public bool CanRepel(SharedSingularityComponent toRepel)
{
var powerNeeded = 2 * toRepel.Level + 1;
return _sharedEnergyPool > powerNeeded;
}
public void Dispose()
{
_powerDecreaseCancellationTokenSource.Cancel();
foreach (var field in _fields)
{
IoCManager.Resolve<IEntityManager>().DeleteEntity(field);
}
_fields.Clear();
RemoveConnection(this, Generator1);
RemoveConnection(this, Generator2);
}
public void RemoveConnection(ContainmentFieldConnection? connection, ContainmentFieldGeneratorComponent component)
{
if (component.Connection1?.Item2 == connection)
{
component.Connection1 = null;
}
else if (component.Connection2?.Item2 == connection)
{
component.Connection2 = null;
}
else if (connection != null)
{
Logger.Error("RemoveConnection called on Containmentfieldgenerator with a connection that can't be found in its connections.");
}
if (IoCManager.Resolve<IEntityManager>().TryGetComponent<PointLightComponent>(component.Owner, out var pointLightComponent))
{
bool hasAnyConnection = (component.Connection1 != null) || (component.Connection2 != null);
pointLightComponent.Enabled = hasAnyConnection;
}
}
}
}

View File

@@ -1,33 +1,107 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Physics;
using Content.Shared.Singularity.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Physics;
using Content.Shared.Tag;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Singularity.Components
namespace Content.Server.Singularity.Components;
[RegisterComponent]
[ComponentReference(typeof(SharedContainmentFieldGeneratorComponent))]
public sealed class ContainmentFieldGeneratorComponent : SharedContainmentFieldGeneratorComponent
{
[RegisterComponent]
[ComponentReference(typeof(SharedContainmentFieldGeneratorComponent))]
public sealed class ContainmentFieldGeneratorComponent : SharedContainmentFieldGeneratorComponent
private int _powerBuffer;
/// <summary>
/// Store power with a cap. Decrease over time if not being powered from source.
/// </summary>
[ViewVariables]
[DataField("powerBuffer")]
public int PowerBuffer
{
private int _powerBuffer;
[ViewVariables]
public int PowerBuffer
{
get => _powerBuffer;
set => _powerBuffer = Math.Clamp(value, 0, 6);
}
public Tuple<Direction, ContainmentFieldConnection>? Connection1;
public Tuple<Direction, ContainmentFieldConnection>? Connection2;
[ViewVariables]
public bool Enabled;
[ViewVariables]
public bool IsConnected;
get => _powerBuffer;
set => _powerBuffer = Math.Clamp(value, 0, 25); //have this decrease over time if not hit by a bolt
}
/// <summary>
/// The minimum the field generator needs to start generating a connection
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("powerMinimum")]
public int PowerMinimum = 6;
/// <summary>
/// How much power should this field generator receive from a collision
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("power")]
public int PowerReceived = 3;
/// <summary>
/// How much power should this field generator lose if not powered?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("powerLoss")]
public int PowerLoss = 2;
/// <summary>
/// Used to check if it's received power recently.
/// </summary>
[ViewVariables]
[DataField("accumulator")]
public float Accumulator;
/// <summary>
/// How many seconds should the generators wait before losing power?
/// </summary>
[ViewVariables]
[DataField("threshold")]
public float Threshold = 10f;
/// <summary>
/// How many tiles should this field check before giving up?
/// </summary>
[ViewVariables]
[DataField("maxLength")]
public float MaxLength = 8F;
/// <summary>
/// What collision should power this generator?
/// It really shouldn't be anything but an emitter bolt but it's here for fun.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("idTag", customTypeSerializer: typeof(PrototypeIdSerializer<TagPrototype>))]
public string IDTag = "EmitterBolt";
/// <summary>
/// Is the generator toggled on?
/// </summary>
[ViewVariables]
public bool Enabled;
/// <summary>
/// Is this generator connected to fields?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool IsConnected;
/// <summary>
/// The masks the raycast should not go through
/// </summary>
[ViewVariables]
[DataField("collisionMask")]
public int CollisionMask = (int) (CollisionGroup.MobMask | CollisionGroup.Impassable | CollisionGroup.MachineMask);
/// <summary>
/// A collection of connections that the generator has based on direction.
/// Stores a list of fields connected between generators in this direction.
/// </summary>
[ViewVariables]
public Dictionary<Direction, (ContainmentFieldGeneratorComponent, List<EntityUid>)> Connections = new();
/// <summary>
/// What fields should this spawn?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("createdField", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public string CreatedField = "ContainmentField";
}