2021-12-05 10:56:17 -08:00
using System ;
2020-10-28 19:19:47 +01:00
using System.Collections.Generic ;
using System.Threading ;
2021-02-11 01:13:03 -08:00
using Robust.Shared.GameObjects ;
2020-10-28 19:19:47 +01:00
using Robust.Shared.IoC ;
using Robust.Shared.Log ;
using Robust.Shared.Maths ;
2021-02-18 20:45:45 -08:00
using Timer = Robust . Shared . Timing . Timer ;
2020-10-28 19:19:47 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.Singularity.Components
2020-10-28 19:19:47 +01:00
{
2022-02-16 00:23:23 -07:00
public sealed class ContainmentFieldConnection : IDisposable
2020-10-28 19:19:47 +01:00
{
public readonly ContainmentFieldGeneratorComponent Generator1 ;
public readonly ContainmentFieldGeneratorComponent Generator2 ;
2021-12-05 18:09:01 +01:00
private readonly List < EntityUid > _fields = new ( ) ;
2020-10-28 19:19:47 +01:00
private int _sharedEnergyPool ;
2020-11-27 11:00:49 +01:00
private readonly CancellationTokenSource _powerDecreaseCancellationTokenSource = new ( ) ;
2020-10-28 19:19:47 +01:00
public int SharedEnergyPool
{
get = > _sharedEnergyPool ;
set
{
2021-05-28 10:44:13 +01:00
_sharedEnergyPool = Math . Clamp ( value , 0 , 25 ) ;
2020-10-28 19:19:47 +01:00
if ( _sharedEnergyPool = = 0 )
{
Dispose ( ) ;
}
}
}
public ContainmentFieldConnection ( ContainmentFieldGeneratorComponent generator1 , ContainmentFieldGeneratorComponent generator2 )
{
Generator1 = generator1 ;
Generator2 = generator2 ;
//generateFields
2021-12-03 15:53:09 +01:00
var pos1 = IoCManager . Resolve < IEntityManager > ( ) . GetComponent < TransformComponent > ( generator1 . Owner ) . Coordinates ;
var pos2 = IoCManager . Resolve < IEntityManager > ( ) . GetComponent < TransformComponent > ( generator2 . Owner ) . Coordinates ;
2020-10-28 19:19:47 +01:00
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 ) ;
2021-12-03 15:53:09 +01:00
if ( ! IoCManager . Resolve < IEntityManager > ( ) . TryGetComponent < ContainmentFieldComponent ? > ( newEnt , out var containmentFieldComponent ) )
2020-10-28 19:19:47 +01:00
{
Logger . Error ( "While creating Fields in ContainmentFieldConnection, a ContainmentField without a ContainmentFieldComponent was created. Deleting newly spawned ContainmentField..." ) ;
2021-12-05 18:09:01 +01:00
IoCManager . Resolve < IEntityManager > ( ) . DeleteEntity ( newEnt ) ;
2020-10-28 19:19:47 +01:00
continue ;
}
containmentFieldComponent . Parent = this ;
2021-12-03 15:53:09 +01:00
IoCManager . Resolve < IEntityManager > ( ) . GetComponent < TransformComponent > ( newEnt ) . WorldRotation = IoCManager . Resolve < IEntityManager > ( ) . GetComponent < TransformComponent > ( generator1 . Owner ) . WorldRotation + dirVec . ToWorldAngle ( ) ;
2020-10-28 19:19:47 +01:00
_fields . Add ( newEnt ) ;
currentOffset + = dirVec ;
}
Timer . SpawnRepeating ( 1000 , ( ) = > { SharedEnergyPool - - ; } , _powerDecreaseCancellationTokenSource . Token ) ;
}
2021-12-05 10:56:17 -08:00
public bool CanRepell ( EntityUid toRepell )
2020-10-28 19:19:47 +01:00
{
var powerNeeded = 1 ;
2021-12-03 15:53:09 +01:00
if ( IoCManager . Resolve < IEntityManager > ( ) . TryGetComponent < ServerSingularityComponent ? > ( toRepell , out var singularityComponent ) )
2020-10-28 19:19:47 +01:00
{
powerNeeded + = 2 * singularityComponent . Level ;
}
return _sharedEnergyPool > powerNeeded ;
}
public void Dispose ( )
{
_powerDecreaseCancellationTokenSource . Cancel ( ) ;
foreach ( var field in _fields )
{
2021-12-05 18:09:01 +01:00
IoCManager . Resolve < IEntityManager > ( ) . DeleteEntity ( field ) ;
2020-10-28 19:19:47 +01:00
}
_fields . Clear ( ) ;
Generator1 . RemoveConnection ( this ) ;
Generator2 . RemoveConnection ( this ) ;
}
}
}