Singularity, Particle Accelerator & Radiation Collectors (#2169)
* basic radiation generator * might need this * thonk * big thonk * oop * e * werks * sprite * oopsy woopsy * radiation * clean up file * makes it work, probably * minor fixes * resources * progress on component * this will no longer be necessary * radiation go brrrr * finally fix container issues * out var Co-authored-by: Remie Richards <remierichards@gmail.com> * second out fix * another out fix Co-authored-by: Remie Richards <remierichards@gmail.com> * switch case * fix switch * sound and improvements * nullable * basic containment field system * ensure alignment * fix beam placement logic * field generation fully working * fix potential crash * working containment functionality * extremely basic emitter functionality * fix radiation panel naming * emitter stuff * oopsies * fixes * some fixes * cleanup * small fix and move emitter file * add sprite resources for PA * slight rework of the singulo adds rads * pushing for smugleaf :) * added radiationpanels * some fixes for the singulo * containmentfield * pa wip * progress * pa working * emitter fix * works :) * ui works * some work on ui & pa * progress * ui work & misc fixes * GREYSCALE * pa ui polish containmentfieldgen rework * singulo rework added snapgrid * getcomponent get out * singulo rework added collisiongroups underplating & passable * yaml work: - collision boxes - singulo now unshaded * no unlit * misc changes * pa wires * add usability check * nullable enable * minor fix * power need added * reenables containment field energy drain menu close button singularity collider fix * sprite replacement * finished singulo pulling * pjb fixes * fixing sprites & minor adjustments * decrease containmentfield power * some yml adjustments * unlit layers singulogenerator * singulogen * everything works just not the powergetting on the pa i wanna die * Adds PA construction graphs, PA construction works * Snap to grid parts when completing construction * updated to newest master * inb4 i work on power * fixes upstream merge adds power need to particleaccelerator * properly implements power & apc power * Emitters are now fancy. * I have actually no idea how this happened. * Give PA a wiring LayoutId * PA is an acronym * indicators fixes hacking * Singulo is a word you blasphemous IDE. * Rewrite the PA. * Fancy names for PA parts. * Wiring fixes, strength wire cutting. * fixes projectile & ignores components * nullability errors * fixes integration tests Co-authored-by: unusualcrow <unusualcrow@protonmail.com> Co-authored-by: L.E.D <10257081+unusualcrow@users.noreply.github.com> Co-authored-by: Remie Richards <remierichards@gmail.com> Co-authored-by: Víctor Aguilera Puerto <zddm@outlook.es> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Singularity
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ContainmentFieldComponent : Component, ICollideBehavior
|
||||
{
|
||||
public override string Name => "ContainmentField";
|
||||
public ContainmentFieldConnection? Parent;
|
||||
|
||||
public void CollideWith(IEntity collidedWith)
|
||||
{
|
||||
if (Parent == null)
|
||||
{
|
||||
Owner.Delete();
|
||||
return;
|
||||
}
|
||||
|
||||
Parent.TryRepell(Owner, collidedWith);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Maths;
|
||||
using Timer = Robust.Shared.Timers.Timer;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Singularity
|
||||
{
|
||||
public class ContainmentFieldConnection : IDisposable
|
||||
{
|
||||
public readonly ContainmentFieldGeneratorComponent Generator1;
|
||||
public readonly ContainmentFieldGeneratorComponent Generator2;
|
||||
private List<IEntity> _fields = new List<IEntity>();
|
||||
private int _sharedEnergyPool;
|
||||
private CancellationTokenSource _powerDecreaseCancellationTokenSource = new CancellationTokenSource();
|
||||
public int SharedEnergyPool
|
||||
{
|
||||
get => _sharedEnergyPool;
|
||||
set
|
||||
{
|
||||
_sharedEnergyPool = Math.Clamp(value, 0, 10);
|
||||
if (_sharedEnergyPool == 0)
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ContainmentFieldConnection(ContainmentFieldGeneratorComponent generator1, ContainmentFieldGeneratorComponent generator2)
|
||||
{
|
||||
Generator1 = generator1;
|
||||
Generator2 = generator2;
|
||||
|
||||
//generateFields
|
||||
var pos1 = generator1.Owner.Transform.Coordinates;
|
||||
var pos2 = generator2.Owner.Transform.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 (!newEnt.TryGetComponent<ContainmentFieldComponent>(out var containmentFieldComponent))
|
||||
{
|
||||
Logger.Error("While creating Fields in ContainmentFieldConnection, a ContainmentField without a ContainmentFieldComponent was created. Deleting newly spawned ContainmentField...");
|
||||
newEnt.Delete();
|
||||
continue;
|
||||
}
|
||||
|
||||
containmentFieldComponent.Parent = this;
|
||||
newEnt.Transform.WorldRotation = dirVec.ToAngle();
|
||||
|
||||
_fields.Add(newEnt);
|
||||
currentOffset += dirVec;
|
||||
}
|
||||
|
||||
|
||||
Timer.SpawnRepeating(1000, () => { SharedEnergyPool--;}, _powerDecreaseCancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
public bool CanRepell(IEntity toRepell)
|
||||
{
|
||||
var powerNeeded = 1;
|
||||
if (toRepell.TryGetComponent<SingularityComponent>(out var singularityComponent))
|
||||
{
|
||||
powerNeeded += 2*singularityComponent.Level;
|
||||
}
|
||||
|
||||
return _sharedEnergyPool > powerNeeded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to repell a Entity. This deletes the connection if the repelling fails!
|
||||
/// </summary>
|
||||
/// <param name="repellFrom">Entity to repell from. Should be a field, otherwise return will be false.</param>
|
||||
/// <param name="toRepell">Entity to repell.</param>
|
||||
public void TryRepell(IEntity repellFrom, IEntity toRepell)
|
||||
{
|
||||
if (!_fields.Contains(repellFrom) || !toRepell.TryGetComponent<IPhysicsComponent>(out var collidableComponent)) return;
|
||||
|
||||
var speed = 5;
|
||||
var containmentFieldRepellController = collidableComponent.EnsureController<ContainmentFieldRepellController>();
|
||||
|
||||
if (!CanRepell(toRepell))
|
||||
{
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
if (Math.Abs(repellFrom.Transform.WorldRotation.Degrees + 90f) < 0.1f ||
|
||||
Math.Abs(repellFrom.Transform.WorldRotation.Degrees - 90f) < 0.1f)
|
||||
{
|
||||
if (repellFrom.Transform.WorldPosition.X.CompareTo(toRepell.Transform.WorldPosition.X) > 0)
|
||||
{
|
||||
containmentFieldRepellController.Repell(Direction.West, speed);
|
||||
}
|
||||
else
|
||||
{
|
||||
containmentFieldRepellController.Repell(Direction.East, speed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (repellFrom.Transform.WorldPosition.Y.CompareTo(toRepell.Transform.WorldPosition.Y) > 0)
|
||||
{
|
||||
containmentFieldRepellController.Repell(Direction.South, speed);
|
||||
}
|
||||
else
|
||||
{
|
||||
containmentFieldRepellController.Repell(Direction.North, speed);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_powerDecreaseCancellationTokenSource.Cancel();
|
||||
foreach (var field in _fields)
|
||||
{
|
||||
field.Delete();
|
||||
}
|
||||
_fields.Clear();
|
||||
|
||||
Generator1.RemoveConnection(this);
|
||||
Generator2.RemoveConnection(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Server.GameObjects.Components.Projectiles;
|
||||
using Content.Server.Utility;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Physics;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Physics;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Singularity
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ContainmentFieldGeneratorComponent : Component, ICollideBehavior
|
||||
{
|
||||
[Dependency] private IPhysicsManager _physicsManager = null!;
|
||||
[Dependency] private IEntityManager _entityManager = null!;
|
||||
|
||||
public override string Name => "ContainmentFieldGenerator";
|
||||
|
||||
private int _powerBuffer;
|
||||
|
||||
[ViewVariables]
|
||||
public int PowerBuffer
|
||||
{
|
||||
get => _powerBuffer;
|
||||
set => _powerBuffer = Math.Clamp(value, 0, 6);
|
||||
}
|
||||
|
||||
public void ReceivePower(int power)
|
||||
{
|
||||
var totalPower = power + PowerBuffer;
|
||||
var powerPerConnection = totalPower / 2;
|
||||
var newBuffer = totalPower % 2;
|
||||
TryPowerConnection(ref _connection1, ref newBuffer, powerPerConnection);
|
||||
TryPowerConnection(ref _connection2, ref newBuffer, powerPerConnection);
|
||||
|
||||
PowerBuffer = newBuffer;
|
||||
}
|
||||
|
||||
private void TryPowerConnection(ref Tuple<Direction, ContainmentFieldConnection>? connectionProperty, ref int powerBuffer, int powerPerConnection)
|
||||
{
|
||||
if (connectionProperty != null)
|
||||
{
|
||||
connectionProperty.Item2.SharedEnergyPool += powerPerConnection;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (TryGenerateFieldConnection(ref connectionProperty))
|
||||
{
|
||||
connectionProperty.Item2.SharedEnergyPool += powerPerConnection;
|
||||
}
|
||||
else
|
||||
{
|
||||
powerBuffer += powerPerConnection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PhysicsComponent? _collidableComponent;
|
||||
|
||||
private Tuple<Direction, ContainmentFieldConnection>? _connection1;
|
||||
private Tuple<Direction, ContainmentFieldConnection>? _connection2;
|
||||
|
||||
public bool CanRepell(IEntity toRepell) => _connection1?.Item2?.CanRepell(toRepell) == true ||
|
||||
_connection2?.Item2?.CanRepell(toRepell) == true;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
if (!Owner.TryGetComponent(out _collidableComponent))
|
||||
{
|
||||
Logger.Error("ContainmentFieldGeneratorComponent created with no CollidableComponent");
|
||||
return;
|
||||
}
|
||||
_collidableComponent.AnchoredChanged += OnAnchoredChanged;
|
||||
}
|
||||
|
||||
|
||||
private void OnAnchoredChanged()
|
||||
{
|
||||
if(_collidableComponent?.Anchored == true)
|
||||
{
|
||||
Owner.SnapToGrid();
|
||||
}
|
||||
else
|
||||
{
|
||||
_connection1?.Item2.Dispose();
|
||||
_connection2?.Item2.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsConnectedWith(ContainmentFieldGeneratorComponent comp)
|
||||
{
|
||||
|
||||
return comp == this || _connection1?.Item2.Generator1 == comp || _connection1?.Item2.Generator2 == comp ||
|
||||
_connection2?.Item2.Generator1 == comp || _connection2?.Item2.Generator2 == comp;
|
||||
}
|
||||
|
||||
public bool HasFreeConnections()
|
||||
{
|
||||
return _connection1 == null || _connection2 == null;
|
||||
}
|
||||
|
||||
private bool TryGenerateFieldConnection([NotNullWhen(true)] ref Tuple<Direction, ContainmentFieldConnection>? propertyFieldTuple)
|
||||
{
|
||||
if (propertyFieldTuple != null) return false;
|
||||
if(_collidableComponent?.Anchored == false) return false;
|
||||
|
||||
foreach (var direction in new[] {Direction.North, Direction.East, Direction.South, Direction.West})
|
||||
{
|
||||
if (_connection1?.Item1 == direction || _connection2?.Item1 == direction) continue;
|
||||
|
||||
var dirVec = direction.ToVec();
|
||||
var ray = new CollisionRay(Owner.Transform.WorldPosition, dirVec, (int) CollisionGroup.MobMask);
|
||||
var rawRayCastResults = _physicsManager.IntersectRay(Owner.Transform.MapID, ray, 4.5f, Owner, false);
|
||||
|
||||
var rayCastResults = rawRayCastResults as RayCastResults[] ?? rawRayCastResults.ToArray();
|
||||
if(!rayCastResults.Any()) continue;
|
||||
|
||||
RayCastResults? closestResult = null;
|
||||
var smallestDist = 4.5f;
|
||||
foreach (var res in rayCastResults)
|
||||
{
|
||||
if (res.Distance > smallestDist) continue;
|
||||
|
||||
smallestDist = res.Distance;
|
||||
closestResult = res;
|
||||
}
|
||||
if(closestResult == null) continue;
|
||||
var ent = closestResult.Value.HitEntity;
|
||||
if (!ent.TryGetComponent<ContainmentFieldGeneratorComponent>(out var fieldGeneratorComponent) ||
|
||||
fieldGeneratorComponent.Owner == Owner ||
|
||||
!fieldGeneratorComponent.HasFreeConnections() ||
|
||||
IsConnectedWith(fieldGeneratorComponent) ||
|
||||
!ent.TryGetComponent<PhysicsComponent>(out var collidableComponent) ||
|
||||
!collidableComponent.Anchored)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var connection = new ContainmentFieldConnection(this, fieldGeneratorComponent);
|
||||
propertyFieldTuple = new Tuple<Direction, ContainmentFieldConnection>(direction, connection);
|
||||
if (fieldGeneratorComponent._connection1 == null)
|
||||
{
|
||||
fieldGeneratorComponent._connection1 = new Tuple<Direction, ContainmentFieldConnection>(direction.GetOpposite(), connection);
|
||||
}
|
||||
else if (fieldGeneratorComponent._connection2 == null)
|
||||
{
|
||||
fieldGeneratorComponent._connection2 = new Tuple<Direction, ContainmentFieldConnection>(direction.GetOpposite(), connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error("When trying to connect two Containmentfieldgenerators, the second one already had two connection but the check didn't catch it");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RemoveConnection(ContainmentFieldConnection? connection)
|
||||
{
|
||||
if (_connection1?.Item2 == connection)
|
||||
{
|
||||
_connection1 = null;
|
||||
}else if (_connection2?.Item2 == connection)
|
||||
{
|
||||
_connection2 = null;
|
||||
}
|
||||
else if(connection != null)
|
||||
{
|
||||
Logger.Error("RemoveConnection called on Containmentfieldgenerator with a connection that can't be found in its connections.");
|
||||
}
|
||||
}
|
||||
|
||||
public void CollideWith(IEntity collidedWith)
|
||||
{
|
||||
if(collidedWith.HasComponent<EmitterBoltComponent>())
|
||||
{
|
||||
ReceivePower(4);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
_connection1?.Item2.Dispose();
|
||||
_connection2?.Item2.Dispose();
|
||||
base.OnRemove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.GameObjects.Components.Access;
|
||||
using Content.Server.GameObjects.Components.Power.PowerNetComponents;
|
||||
using Content.Server.GameObjects.Components.Projectiles;
|
||||
using Content.Server.Interfaces;
|
||||
using Content.Server.Utility;
|
||||
using Content.Shared.GameObjects.Components.Singularity;
|
||||
using Content.Shared.Interfaces;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.ComponentDependencies;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Random;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using Timer = Robust.Shared.Timers.Timer;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Singularity
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class EmitterComponent : Component, IActivate, IInteractUsing
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
[ComponentDependency] private AppearanceComponent? _appearance;
|
||||
[ComponentDependency] private AccessReader? _accessReader;
|
||||
|
||||
public override string Name => "Emitter";
|
||||
|
||||
private CancellationTokenSource? _timerCancel;
|
||||
|
||||
private PhysicsComponent _collidableComponent = default!;
|
||||
private PowerConsumerComponent _powerConsumer = default!;
|
||||
|
||||
// whether the power switch is in "on"
|
||||
[ViewVariables] private bool _isOn;
|
||||
// Whether the power switch is on AND the machine has enough power (so is actively firing)
|
||||
[ViewVariables] private bool _isPowered;
|
||||
[ViewVariables] private bool _isLocked;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] private int _fireShotCounter;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] private string _fireSound = default!;
|
||||
[ViewVariables(VVAccess.ReadWrite)] private string _boltType = default!;
|
||||
[ViewVariables(VVAccess.ReadWrite)] private int _powerUseActive;
|
||||
[ViewVariables(VVAccess.ReadWrite)] private int _fireBurstSize;
|
||||
[ViewVariables(VVAccess.ReadWrite)] private TimeSpan _fireInterval;
|
||||
[ViewVariables(VVAccess.ReadWrite)] private TimeSpan _fireBurstDelayMin;
|
||||
[ViewVariables(VVAccess.ReadWrite)] private TimeSpan _fireBurstDelayMax;
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
serializer.DataField(ref _fireBurstDelayMin, "fireBurstDelayMin", TimeSpan.FromSeconds(2));
|
||||
serializer.DataField(ref _fireBurstDelayMax, "fireBurstDelayMax", TimeSpan.FromSeconds(10));
|
||||
serializer.DataField(ref _fireInterval, "fireInterval", TimeSpan.FromSeconds(2));
|
||||
serializer.DataField(ref _fireBurstSize, "fireBurstSize", 3);
|
||||
serializer.DataField(ref _powerUseActive, "powerUseActive", 500);
|
||||
serializer.DataField(ref _boltType, "boltType", "EmitterBolt");
|
||||
serializer.DataField(ref _fireSound, "fireSound", "/Audio/Weapons/emitter.ogg");
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (!Owner.TryGetComponent(out _collidableComponent!))
|
||||
{
|
||||
Logger.Error($"EmitterComponent {Owner} created with no CollidableComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Owner.TryGetComponent(out _powerConsumer!))
|
||||
{
|
||||
Logger.Error($"EmitterComponent {Owner} created with no PowerConsumerComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
_collidableComponent.AnchoredChanged += OnAnchoredChanged;
|
||||
_powerConsumer.OnReceivedPowerChanged += OnReceivedPowerChanged;
|
||||
}
|
||||
|
||||
private void OnReceivedPowerChanged(object? sender, ReceivedPowerChangedEventArgs e)
|
||||
{
|
||||
if (!_isOn)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.ReceivedPower < e.DrawRate)
|
||||
{
|
||||
PowerOff();
|
||||
}
|
||||
else
|
||||
{
|
||||
PowerOn();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAnchoredChanged()
|
||||
{
|
||||
if (_collidableComponent.Anchored)
|
||||
Owner.SnapToGrid();
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (_isLocked)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("{0:TheName} is access locked!", Owner));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_isOn)
|
||||
{
|
||||
SwitchOn();
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("{0:TheName} turns on.", Owner));
|
||||
}
|
||||
else
|
||||
{
|
||||
SwitchOff();
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("{0:TheName} turns off.", Owner));
|
||||
}
|
||||
}
|
||||
|
||||
Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (_accessReader == null || !eventArgs.Using.TryGetComponent(out IAccess? access))
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
if (_accessReader.IsAllowed(access))
|
||||
{
|
||||
_isLocked ^= true;
|
||||
|
||||
if (_isLocked)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("You lock {0:TheName}.", Owner));
|
||||
}
|
||||
else
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("You unlock {0:TheName}.", Owner));
|
||||
}
|
||||
|
||||
UpdateAppearance();
|
||||
}
|
||||
else
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Access denied."));
|
||||
}
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
private void SwitchOff()
|
||||
{
|
||||
_isOn = false;
|
||||
_powerConsumer.DrawRate = 0;
|
||||
PowerOff();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void SwitchOn()
|
||||
{
|
||||
_isOn = true;
|
||||
_powerConsumer.DrawRate = _powerUseActive;
|
||||
// Do not directly PowerOn().
|
||||
// OnReceivedPowerChanged will get fired due to DrawRate change which will turn it on.
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void PowerOff()
|
||||
{
|
||||
if (!_isPowered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isPowered = false;
|
||||
|
||||
// Must be set while emitter powered.
|
||||
DebugTools.AssertNotNull(_timerCancel);
|
||||
_timerCancel!.Cancel();
|
||||
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void PowerOn()
|
||||
{
|
||||
if (_isPowered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isPowered = true;
|
||||
|
||||
_fireShotCounter = 0;
|
||||
_timerCancel = new CancellationTokenSource();
|
||||
|
||||
Timer.Spawn(_fireBurstDelayMax, ShotTimerCallback, _timerCancel.Token);
|
||||
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void ShotTimerCallback()
|
||||
{
|
||||
// Any power-off condition should result in the timer for this method being cancelled
|
||||
// and thus not firing
|
||||
DebugTools.Assert(_isPowered);
|
||||
DebugTools.Assert(_isOn);
|
||||
DebugTools.Assert(_powerConsumer.DrawRate <= _powerConsumer.ReceivedPower);
|
||||
|
||||
Fire();
|
||||
|
||||
TimeSpan delay;
|
||||
if (_fireShotCounter < _fireBurstSize)
|
||||
{
|
||||
_fireShotCounter += 1;
|
||||
delay = _fireInterval;
|
||||
}
|
||||
else
|
||||
{
|
||||
_fireShotCounter = 0;
|
||||
var diff = _fireBurstDelayMax - _fireBurstDelayMin;
|
||||
// TIL you can do TimeSpan * double.
|
||||
delay = _fireBurstDelayMin + _robustRandom.NextFloat() * diff;
|
||||
}
|
||||
|
||||
// Must be set while emitter powered.
|
||||
DebugTools.AssertNotNull(_timerCancel);
|
||||
Timer.Spawn(delay, ShotTimerCallback, _timerCancel!.Token);
|
||||
}
|
||||
|
||||
private void Fire()
|
||||
{
|
||||
var projectile = _entityManager.SpawnEntity(_boltType, Owner.Transform.Coordinates);
|
||||
|
||||
if (!projectile.TryGetComponent<PhysicsComponent>(out var physicsComponent))
|
||||
{
|
||||
Logger.Error("Emitter tried firing a bolt, but it was spawned without a CollidableComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
physicsComponent.Status = BodyStatus.InAir;
|
||||
|
||||
if (!projectile.TryGetComponent<ProjectileComponent>(out var projectileComponent))
|
||||
{
|
||||
Logger.Error("Emitter tried firing a bolt, but it was spawned without a ProjectileComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
projectileComponent.IgnoreEntity(Owner);
|
||||
|
||||
physicsComponent
|
||||
.EnsureController<BulletController>()
|
||||
.LinearVelocity = Owner.Transform.WorldRotation.ToVec() * 20f;
|
||||
|
||||
projectile.Transform.LocalRotation = Owner.Transform.WorldRotation;
|
||||
|
||||
// TODO: Move to projectile's code.
|
||||
Timer.Spawn(3000, () => projectile.Delete());
|
||||
|
||||
EntitySystem.Get<AudioSystem>().PlayFromEntity(_fireSound, Owner);
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
if (_appearance == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EmitterVisualState state;
|
||||
if (_isPowered)
|
||||
{
|
||||
state = EmitterVisualState.On;
|
||||
}
|
||||
else if (_isOn)
|
||||
{
|
||||
state = EmitterVisualState.Underpowered;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = EmitterVisualState.Off;
|
||||
}
|
||||
|
||||
_appearance.SetData(EmitterVisuals.VisualState, state);
|
||||
_appearance.SetData(EmitterVisuals.Locked, _isLocked);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.GameObjects.Components.StationEvents;
|
||||
using Content.Shared.GameObjects;
|
||||
using Content.Shared.GameObjects.EntitySystemMessages;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects.Components.Map;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Map;
|
||||
using Robust.Shared.Interfaces.Random;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Timers;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Singularity
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class SingularityComponent : Component, ICollideBehavior
|
||||
{
|
||||
[Dependency] private IEntityManager _entityManager = null!;
|
||||
[Dependency] private IMapManager _mapManager = null!;
|
||||
[Dependency] private IRobustRandom _random = null!;
|
||||
|
||||
|
||||
public override uint? NetID => ContentNetIDs.SINGULARITY;
|
||||
|
||||
public override string Name => "Singularity";
|
||||
|
||||
public int Energy
|
||||
{
|
||||
get => _energy;
|
||||
set
|
||||
{
|
||||
if (value == _energy) return;
|
||||
|
||||
_energy = value;
|
||||
if (_energy <= 0)
|
||||
{
|
||||
if(_singularityController != null) _singularityController.LinearVelocity = Vector2.Zero;
|
||||
_spriteComponent?.LayerSetVisible(0, false);
|
||||
|
||||
Owner.Delete();
|
||||
return;
|
||||
}
|
||||
|
||||
Level = _energy switch
|
||||
{
|
||||
var n when n >= 1500 => 6,
|
||||
var n when n >= 1000 => 5,
|
||||
var n when n >= 600 => 4,
|
||||
var n when n >= 300 => 3,
|
||||
var n when n >= 200 => 2,
|
||||
var n when n < 200 => 1,
|
||||
_ => 1
|
||||
};
|
||||
}
|
||||
}
|
||||
private int _energy = 180;
|
||||
|
||||
public int Level
|
||||
{
|
||||
get => _level;
|
||||
set
|
||||
{
|
||||
if (value == _level) return;
|
||||
if (value < 0) value = 0;
|
||||
if (value > 6) value = 6;
|
||||
|
||||
_level = value;
|
||||
|
||||
if(_radiationPulseComponent != null) _radiationPulseComponent.RadsPerSecond = 10 * value;
|
||||
|
||||
_spriteComponent?.LayerSetRSI(0, "Effects/Singularity/singularity_" + _level + ".rsi");
|
||||
_spriteComponent?.LayerSetState(0, "singularity_" + _level);
|
||||
|
||||
if(_collidableComponent != null && _collidableComponent.PhysicsShapes.Any() && _collidableComponent.PhysicsShapes[0] is PhysShapeCircle circle)
|
||||
{
|
||||
circle.Radius = _level - 0.5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
private int _level;
|
||||
|
||||
public int EnergyDrain =>
|
||||
Level switch
|
||||
{
|
||||
6 => 20,
|
||||
5 => 15,
|
||||
4 => 10,
|
||||
3 => 5,
|
||||
2 => 2,
|
||||
1 => 1,
|
||||
_ => 0
|
||||
};
|
||||
|
||||
private SingularityController? _singularityController;
|
||||
private PhysicsComponent? _collidableComponent;
|
||||
private SpriteComponent? _spriteComponent;
|
||||
private RadiationPulseComponent? _radiationPulseComponent;
|
||||
private AudioSystem _audioSystem = null!;
|
||||
private AudioSystem.AudioSourceServer? _playingSound;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_audioSystem = EntitySystem.Get<AudioSystem>();
|
||||
var audioParams = AudioParams.Default;
|
||||
audioParams.Loop = true;
|
||||
audioParams.MaxDistance = 20f;
|
||||
audioParams.Volume = 5;
|
||||
_audioSystem.PlayFromEntity("/Audio/Effects/singularity_form.ogg", Owner);
|
||||
Timer.Spawn(5200,() => _playingSound = _audioSystem.PlayFromEntity("/Audio/Effects/singularity.ogg", Owner, audioParams));
|
||||
|
||||
|
||||
if (!Owner.TryGetComponent(out _collidableComponent))
|
||||
{
|
||||
Logger.Error("SingularityComponent was spawned without CollidableComponent");
|
||||
}
|
||||
else
|
||||
{
|
||||
_collidableComponent.Hard = false;
|
||||
}
|
||||
|
||||
if (!Owner.TryGetComponent(out _spriteComponent))
|
||||
{
|
||||
Logger.Error("SingularityComponent was spawned without SpriteComponent");
|
||||
}
|
||||
|
||||
_singularityController = _collidableComponent?.EnsureController<SingularityController>();
|
||||
if(_singularityController!=null)_singularityController.ControlledComponent = _collidableComponent;
|
||||
|
||||
if (!Owner.TryGetComponent(out _radiationPulseComponent))
|
||||
{
|
||||
Logger.Error("SingularityComponent was spawned without RadiationPulseComponent");
|
||||
}
|
||||
|
||||
Level = 1;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
Energy -= EnergyDrain;
|
||||
|
||||
if(Level == 1) return;
|
||||
//pushing
|
||||
var pushVector = new Vector2((_random.Next(-10, 10)), _random.Next(-10, 10));
|
||||
while (pushVector.X == 0 && pushVector.Y == 0)
|
||||
{
|
||||
pushVector = new Vector2((_random.Next(-10, 10)), _random.Next(-10, 10));
|
||||
}
|
||||
_singularityController?.Push(pushVector.Normalized, 2);
|
||||
}
|
||||
|
||||
List<IEntity> _previousPulledEntites = new List<IEntity>();
|
||||
public void PullUpdate()
|
||||
{
|
||||
foreach (var previousPulledEntity in _previousPulledEntites)
|
||||
{
|
||||
if(previousPulledEntity.Deleted) continue;
|
||||
if (!previousPulledEntity.TryGetComponent<PhysicsComponent>(out var collidableComponent)) continue;
|
||||
var controller = collidableComponent.EnsureController<SingularityPullController>();
|
||||
controller.StopPull();
|
||||
}
|
||||
_previousPulledEntites.Clear();
|
||||
|
||||
var entitiesToPull = _entityManager.GetEntitiesInRange(Owner.Transform.Coordinates, Level * 10);
|
||||
foreach (var entity in entitiesToPull)
|
||||
{
|
||||
if (!entity.TryGetComponent<PhysicsComponent>(out var collidableComponent)) continue;
|
||||
var controller = collidableComponent.EnsureController<SingularityPullController>();
|
||||
if(Owner.Transform.Coordinates.EntityId != entity.Transform.Coordinates.EntityId) continue;
|
||||
var vec = (Owner.Transform.Coordinates - entity.Transform.Coordinates).Position;
|
||||
if (vec == Vector2.Zero) continue;
|
||||
|
||||
var speed = 10 / vec.Length * Level;
|
||||
|
||||
controller.Pull(vec.Normalized, speed);
|
||||
_previousPulledEntites.Add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollideBehavior.CollideWith(IEntity entity)
|
||||
{
|
||||
if (_collidableComponent == null) return; //how did it even collide then? :D
|
||||
|
||||
if (entity.TryGetComponent<IMapGridComponent>(out var mapGridComponent))
|
||||
{
|
||||
foreach (var tile in mapGridComponent.Grid.GetTilesIntersecting(((IPhysBody) _collidableComponent).WorldAABB))
|
||||
{
|
||||
mapGridComponent.Grid.SetTile(tile.GridIndices, Tile.Empty);
|
||||
Energy++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (entity.HasComponent<ContainmentFieldComponent>() || (entity.TryGetComponent<ContainmentFieldGeneratorComponent>(out var component) && component.CanRepell(Owner)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (ContainerHelpers.IsInContainer(entity)) return;
|
||||
|
||||
entity.Delete();
|
||||
Energy++;
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
_playingSound?.Stop();
|
||||
_audioSystem.PlayAtCoords("/Audio/Effects/singularity_collapse.ogg", Owner.Transform.Coordinates);
|
||||
base.OnRemove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Singularity
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class SingularityGeneratorComponent : Component
|
||||
{
|
||||
public override string Name => "SingularityGenerator";
|
||||
|
||||
private int _power;
|
||||
|
||||
public int Power
|
||||
{
|
||||
get => _power;
|
||||
set
|
||||
{
|
||||
if(_power == value) return;
|
||||
|
||||
_power = value;
|
||||
if (_power > 15)
|
||||
{
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
entityManager.SpawnEntity("Singularity", Owner.Transform.Coordinates);
|
||||
//dont delete ourselves, just wait to get eaten
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user