Splits the singularity into its component parts + ECS singularity + Support for singularities in containers. (#12132)

* InitialCommit (Broken)

* Fixes compile errors

* PR comments. More doc comments. Fixes

* Makes a singularity/event horizon without radiation/physics a valid state to be in

* VV 'fake' setters, fixes the visualizer, fixes the singularity trying to eat itself instead of nearby things.

* Removes unused dependency from Content.Client.GravityWellSystem

* Testing containment and fake VV setters for SingularityGeneratorComponent

* Fixes gravity wells (broken due to LookupFlags.None). Adds recursive Event Horizon consumption

* Fix merge skew

* Fixes for the master merge

* Fix engine commit

* Dirty is obsolete

* Switch over dirty

* Fix requested changes

* ambiant -> ambient

* Moves EventHorionComponent to Shared

* Proper container handling

* Fixes master merge. Fixes post insertion assertions for singularities. Extends proper container handling to gravity wells and the distortion shader.

* Better support for admemes throwing singularities.

* Moves update timing from accumulators to target times

* Update doc comments
This commit is contained in:
TemporalOroboros
2022-12-19 18:47:15 -08:00
committed by GitHub
parent 490aefecef
commit 9a72b05a50
35 changed files with 2561 additions and 683 deletions

View File

@@ -0,0 +1,71 @@
using Robust.Shared.GameStates;
using Content.Shared.Singularity.EntitySystems;
namespace Content.Shared.Singularity.Components;
/// <summary>
/// A component that makes the associated entity destroy other within some distance of itself.
/// Also makes the associated entity destroy other entities upon contact.
/// Primarily managed by <see cref="SharedEventHorizonSystem"/> and its server/client versions.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class EventHorizonComponent : Component
{
/// <summary>
/// The radius of the event horizon within which it will destroy all entities and tiles.
/// If < 0.0 this behavior will not be active.
/// If you want to set this go through <see cref="SharedEventHorizonSystem.SetRadius"/>.
/// </summary>
[DataField("radius")]
[Access(friends:typeof(SharedEventHorizonSystem))]
public float Radius;
/// <summary>
/// Whether the event horizon can consume/destroy the devices built to contain it.
/// If you want to set this go through <see cref="SharedEventHorizonSystem.SetCanBreachContainment"/>.
/// </summary>
[DataField("canBreachContainment")]
[Access(friends:typeof(SharedEventHorizonSystem))]
public bool CanBreachContainment = false;
/// <summary>
/// The ID of the fixture used to detect if the event horizon has collided with any physics objects.
/// Can be set to null, in which case no such fixture is used.
/// If you want to set this go through <see cref="SharedEventHorizonSystem.SetHorizonFixtureId"/>.
/// </summary>
[DataField("horizonFixtureId")]
[Access(friends:typeof(SharedEventHorizonSystem))]
public string? HorizonFixtureId = "EventHorizon";
/// <summary>
/// Whether the entity this event horizon is attached to is being consumed by another event horizon.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public bool BeingConsumedByAnotherEventHorizon = false;
#region Update Timing
/// <summary>
/// The amount of time that should elapse between this event horizon consuming everything it overlaps with.
/// </summary>
[DataField("consumePeriod")]
[ViewVariables(VVAccess.ReadOnly)]
[Access(typeof(SharedEventHorizonSystem))]
public TimeSpan TargetConsumePeriod { get; set; } = TimeSpan.FromSeconds(0.5);
/// <summary>
/// The last time at which this consumed everything it overlapped with.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
[Access(typeof(SharedEventHorizonSystem))]
public TimeSpan LastConsumeWaveTime { get; set; } = default!;
/// <summary>
/// The next time at which this consumed everything it overlapped with.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
[Access(typeof(SharedEventHorizonSystem))]
public TimeSpan NextConsumeWaveTime { get; set; } = default!;
#endregion Update Timing
}

View File

@@ -1,42 +1,32 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Singularity.Components
using Content.Shared.Singularity.EntitySystems;
namespace Content.Shared.Singularity.Components;
/// <summary>
/// A component that makes the associated entity accumulate energy when an associated event horizon consumes things.
/// Energy management is server-side.
/// </summary>
[NetworkedComponent]
public abstract class SharedSingularityComponent : Component
{
[NetworkedComponent]
public abstract class SharedSingularityComponent : Component
{
/// <summary>
/// The radiation pulse component's radsPerSecond is set to the singularity's level multiplied by this number.
/// </summary>
[DataField("radsPerLevel")]
public float RadsPerLevel = 1;
/// <summary>
/// The current level of the singularity.
/// Used as a scaling factor for things like visual size, event horizon radius, gravity well radius, radiation output, etc.
/// If you want to set this use <see cref="SharedSingularitySystem.SetLevel"/>().
/// </summary>
[DataField("level")]
[Access(friends:typeof(SharedSingularitySystem), Other=AccessPermissions.Read, Self=AccessPermissions.Read)]
public byte Level = 1;
/// <summary>
/// Changed by <see cref="SharedSingularitySystem.ChangeSingularityLevel"/>
/// </summary>
[ViewVariables]
public int Level { get; set; }
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is not SingularityComponentState state)
{
return;
}
EntitySystem.Get<SharedSingularitySystem>().ChangeSingularityLevel(this, state.Level);
}
}
[Serializable, NetSerializable]
public sealed class SingularityComponentState : ComponentState
{
public int Level { get; }
public SingularityComponentState(int level)
{
Level = level;
}
}
/// <summary>
/// The amount of radiation this singularity emits per its level.
/// Has to be on shared in case someone attaches a RadiationPulseComponent to the singularity.
/// If you want to set this use <see cref="SharedSingularitySystem.SetRadsPerLevel"/>().
/// </summary>
[DataField("radsPerLevel")]
[Access(friends:typeof(SharedSingularitySystem), Other=AccessPermissions.Read, Self=AccessPermissions.Read)]
[ViewVariables(VVAccess.ReadWrite)]
public float RadsPerLevel = 2f;
}

View File

@@ -0,0 +1,206 @@
using Robust.Shared.Map.Components;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Content.Shared.Ghost;
using Content.Shared.Singularity.Components;
namespace Content.Shared.Singularity.EntitySystems;
/// <summary>
/// The entity system primarily responsible for managing <see cref="EventHorizonComponent"/>s.
/// </summary>
public abstract class SharedEventHorizonSystem : EntitySystem
{
#region Dependencies
[Dependency] private readonly FixtureSystem _fixtures = default!;
[Dependency] protected readonly IViewVariablesManager Vvm = default!;
#endregion Dependencies
public override void Initialize()
{
base.Initialize();
// Allows for predicted collisions with singularities.
SubscribeLocalEvent<EventHorizonComponent, ComponentStartup>(OnEventHorizonStartup);
SubscribeLocalEvent<EventHorizonComponent, PreventCollideEvent>(OnPreventCollide);
var vvHandle = Vvm.GetTypeHandler<EventHorizonComponent>();
vvHandle.AddPath(nameof(EventHorizonComponent.Radius), (_, comp) => comp.Radius, (uid, value, comp) => SetRadius(uid, value, eventHorizon: comp));
vvHandle.AddPath(nameof(EventHorizonComponent.CanBreachContainment), (_, comp) => comp.CanBreachContainment, (uid, value, comp) => SetCanBreachContainment(uid, value, eventHorizon: comp));
vvHandle.AddPath(nameof(EventHorizonComponent.HorizonFixtureId), (_, comp) => comp.HorizonFixtureId, (uid, value, comp) => SetHorizonFixtureId(uid, value, eventHorizon: comp));
}
public override void Shutdown()
{
var vvHandle = Vvm.GetTypeHandler<EventHorizonComponent>();
vvHandle.RemovePath(nameof(EventHorizonComponent.Radius));
vvHandle.RemovePath(nameof(EventHorizonComponent.CanBreachContainment));
vvHandle.RemovePath(nameof(EventHorizonComponent.HorizonFixtureId));
base.Shutdown();
}
#region Getters/Setters
/// <summary>
/// Setter for <see cref="EventHorizonComponent.Radius"/>
/// May also update the fixture associated with the event horizon.
/// </summary>
/// <param name="uid">The uid of the event horizon to change the radius of.</param>
/// <param name="value">The new radius of the event horizon.</param>
/// <param name="updateFixture">Whether to update the associated fixture upon changing the radius of the event horizon.</param>
/// <param name="eventHorizon">The state of the event horizon to change the radius of.</param>
public void SetRadius(EntityUid uid, float value, bool updateFixture = true, EventHorizonComponent? eventHorizon = null)
{
if(!Resolve(uid, ref eventHorizon))
return;
var oldValue = eventHorizon.Radius;
if (value == oldValue)
return;
eventHorizon.Radius = value;
EntityManager.Dirty(eventHorizon);
if (updateFixture)
UpdateEventHorizonFixture(uid, eventHorizon: eventHorizon);
}
/// <summary>
/// Setter for <see cref="EventHorizonComponent.CanBreachContainment"/>
/// May also update the fixture associated with the event horizon.
/// </summary>
/// <param name="uid">The uid of the event horizon to make (in)capable of breaching containment.</param>
/// <param name="value">Whether the event horizon should be able to breach containment.</param>
/// <param name="updateFixture">Whether to update the associated fixture upon changing whether the event horizon can breach containment.</param>
/// <param name="eventHorizon">The state of the event horizon to make (in)capable of breaching containment.</param>
public void SetCanBreachContainment(EntityUid uid, bool value, bool updateFixture = true, EventHorizonComponent? eventHorizon = null)
{
if(!Resolve(uid, ref eventHorizon))
return;
var oldValue = eventHorizon.CanBreachContainment;
if (value == oldValue)
return;
eventHorizon.CanBreachContainment = value;
EntityManager.Dirty(eventHorizon);
if (updateFixture)
UpdateEventHorizonFixture(uid, eventHorizon: eventHorizon);
}
/// <summary>
/// Setter for <see cref="EventHorizonComponent.HorizonFixtureId"/>
/// May also update the fixture associated with the event horizon.
/// </summary>
/// <param name="uid">The uid of the event horizon with the fixture ID to change.</param>
/// <param name="value">The new fixture ID to associate the event horizon with.</param>
/// <param name="updateFixture">Whether to update the associated fixture upon changing whether the event horizon can breach containment.</param>
/// <param name="eventHorizon">The state of the event horizon with the fixture ID to change.</param>
public void SetHorizonFixtureId(EntityUid uid, string? value, bool updateFixture = true, EventHorizonComponent? eventHorizon = null)
{
if(!Resolve(uid, ref eventHorizon))
return;
var oldValue = eventHorizon.HorizonFixtureId;
if (value == oldValue)
return;
eventHorizon.HorizonFixtureId = value;
EntityManager.Dirty(eventHorizon);
if (updateFixture)
UpdateEventHorizonFixture(uid, eventHorizon: eventHorizon);
}
/// <summary>
/// Updates the state of the fixture associated with the event horizon.
/// </summary>
/// <param name="eventHorizon">The uid of the event horizon associated with the fixture to update.</param>
/// <param name="fixtures">The physics component containing the fixture to update.</param>
/// <param name="eventHorizon">The state of the event horizon associated with the fixture to update.</param>
public void UpdateEventHorizonFixture(EntityUid uid, PhysicsComponent? fixtures = null, EventHorizonComponent? eventHorizon = null)
{
if(!Resolve(uid, ref eventHorizon))
return;
var fixtureId = eventHorizon.HorizonFixtureId;
if (fixtureId == null || !Resolve(eventHorizon.Owner, ref fixtures, logMissing: false))
return;
var fixture = _fixtures.GetFixtureOrNull(fixtures, fixtureId);
if (fixture == null)
return;
var shape = (PhysShapeCircle)fixture.Shape;
shape.Radius = eventHorizon.Radius;
fixture.Hard = !eventHorizon.CanBreachContainment;
EntityManager.Dirty(fixtures);
}
#endregion Getters/Setters
#region EventHandlers
/// <summary>
/// Syncs the state of the fixture associated with the event horizon upon startup.
/// </summary>
/// <param name="uid">The entity that has just gained an event horizon component.</param>
/// <param name="comp">The event horizon component that is starting up.</param>
/// <param name="args">The event arguments.</param>
private void OnEventHorizonStartup(EntityUid uid, EventHorizonComponent comp, ComponentStartup args)
{
UpdateEventHorizonFixture(uid, eventHorizon: comp);
}
/// <summary>
/// Prevents the event horizon from colliding with anything it cannot consume.
/// Most notably map grids and ghosts.
/// Also makes event horizons phase through containment if it can breach.
/// </summary>
/// <param name="uid">The entity that is trying to collide with another entity.</param>
/// <param name="comp">The event horizon of the former.</param>
/// <param name="args">The event arguments.</param>
private void OnPreventCollide(EntityUid uid, EventHorizonComponent comp, ref PreventCollideEvent args)
{
if(!args.Cancelled)
PreventCollide(uid, comp, ref args);
}
/// <summary>
/// The actual, functional part of SharedEventHorizonSystem.OnPreventCollide.
/// The return value allows for overrides to early return if the base successfully handles collision prevention.
/// </summary>
/// <param name="uid">The entity that is trying to collide with another entity.</param>
/// <param name="comp">The event horizon of the former.</param>
/// <param name="args">The event arguments.</param>
/// <returns>A bool indicating whether the collision prevention has been handled.</return>
protected virtual bool PreventCollide(EntityUid uid, EventHorizonComponent comp, ref PreventCollideEvent args)
{
var otherUid = args.BodyB.Owner;
// For prediction reasons always want the client to ignore these.
if (EntityManager.HasComponent<MapGridComponent>(otherUid) ||
EntityManager.HasComponent<SharedGhostComponent>(otherUid))
{
args.Cancelled = true;
return true;
}
// If we can, breach containment
// otherwise, check if it's containment and just keep the collision
if (EntityManager.HasComponent<SharedContainmentFieldComponent>(otherUid) ||
EntityManager.HasComponent<SharedContainmentFieldGeneratorComponent>(otherUid))
{
if (comp.CanBreachContainment)
args.Cancelled = true;
return true;
}
return false;
}
#endregion EventHandlers
}

View File

@@ -0,0 +1,7 @@
namespace Content.Shared.Singularity.EntitySystems;
/// <summary>
/// The entity system primarily responsible for managing <see cref="SharedGravityWellComponent"/>s.
/// </summary>
public abstract class SharedGravityWellSystem : EntitySystem
{}

View File

@@ -0,0 +1,385 @@
using Robust.Shared.Containers;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Serialization;
using Content.Shared.Radiation.Components;
using Content.Shared.Singularity.Components;
using Content.Shared.Singularity.Events;
namespace Content.Shared.Singularity.EntitySystems;
/// <summary>
/// The entity system primarily responsible for managing <see cref="SharedSingularityComponent"/>s.
/// </summary>
public abstract class SharedSingularitySystem : EntitySystem
{
#region Dependencies
[Dependency] private readonly SharedAppearanceSystem _visualizer = default!;
[Dependency] private readonly SharedContainerSystem _containers = default!;
[Dependency] private readonly SharedEventHorizonSystem _horizons = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] protected readonly IViewVariablesManager Vvm = default!;
#endregion Dependencies
/// <summary>
/// The minimum level a singularity can be set to.
/// </summary>
public const byte MinSingularityLevel = 0;
/// <summary>
/// The maximum level a singularity can be set to.
/// </summary>
public const byte MaxSingularityLevel = 6;
/// <summary>
/// The amount to scale a singularities distortion shader by when it's in a container.
/// This is the inverse of an exponent, not a linear scaling factor.
/// ie. n => intensity = intensity ** (1/n)
/// </summary>
public const float DistortionContainerScaling = 4f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedSingularityComponent, ComponentStartup>(OnSingularityStartup);
SubscribeLocalEvent<AppearanceComponent, SingularityLevelChangedEvent>(UpdateAppearance);
SubscribeLocalEvent<RadiationSourceComponent, SingularityLevelChangedEvent>(UpdateRadiation);
SubscribeLocalEvent<PhysicsComponent, SingularityLevelChangedEvent>(UpdateBody);
SubscribeLocalEvent<EventHorizonComponent, SingularityLevelChangedEvent>(UpdateEventHorizon);
SubscribeLocalEvent<SingularityDistortionComponent, SingularityLevelChangedEvent>(UpdateDistortion);
SubscribeLocalEvent<SingularityDistortionComponent, EntGotInsertedIntoContainerMessage>(UpdateDistortion);
SubscribeLocalEvent<SingularityDistortionComponent, EntGotRemovedFromContainerMessage>(UpdateDistortion);
var vvHandle = Vvm.GetTypeHandler<SharedSingularityComponent>();
vvHandle.AddPath(nameof(SharedSingularityComponent.Level), (_, comp) => comp.Level, SetLevel);
vvHandle.AddPath(nameof(SharedSingularityComponent.RadsPerLevel), (_, comp) => comp.RadsPerLevel, SetRadsPerLevel);
}
public override void Shutdown()
{
var vvHandle = Vvm.GetTypeHandler<SharedSingularityComponent>();
vvHandle.RemovePath(nameof(SharedSingularityComponent.Level));
vvHandle.RemovePath(nameof(SharedSingularityComponent.RadsPerLevel));
base.Shutdown();
}
#region Getters/Setters
/// <summary>
/// Setter for <see cref="SharedSingularityComponent.Level"/>
/// Also sends out an event alerting that the singularities level has changed.
/// </summary>
/// <param name="uid">The uid of the singularity to change the level of.</param>
/// <param name="value">The new level the singularity should have.</param>
/// <param name="singularity">The state of the singularity to change the level of.</param>
public void SetLevel(EntityUid uid, byte value, SharedSingularityComponent? singularity = null)
{
if(!Resolve(uid, ref singularity))
return;
value = MathHelper.Clamp(value, MinSingularityLevel, MaxSingularityLevel);
var oldValue = singularity.Level;
if (oldValue == value)
return;
singularity.Level = value;
UpdateSingularityLevel(uid, oldValue, singularity);
if(!EntityManager.Deleted(singularity.Owner))
EntityManager.Dirty(singularity);
}
/// <summary>
/// Setter for <see cref="SharedSingularityComponent.RadsPerLevel"/>
/// Also updates the radiation output of the singularity according to the new values.
/// </summary>
/// <param name="uid">The uid of the singularity to change the radioactivity of.</param>
/// <param name="value">The new radioactivity the singularity should have.</param>
/// <param name="singularity">The state of the singularity to change the radioactivity of.</param>
public void SetRadsPerLevel(EntityUid uid, float value, SharedSingularityComponent? singularity = null)
{
if(!Resolve(uid, ref singularity))
return;
var oldValue = singularity.RadsPerLevel;
if (oldValue == value)
return;
singularity.RadsPerLevel = value;
UpdateRadiation(uid, singularity);
}
/// <summary>
/// Alerts the entity hosting the singularity that the level of the singularity has changed.
/// Usually follows a SharedSingularitySystem.SetLevel call, but is also used on component startup to sync everything.
/// </summary>
/// <param name="uid">The uid of the singularity which's level has changed.</param>
/// <param name="oldValue">The old level of the singularity. May be equal to <see cref="SharedSingularityComponent.Level"/> if the component is starting.</param>
/// <param name="singularity">The state of the singularity which's level has changed.</param>
public void UpdateSingularityLevel(EntityUid uid, byte oldValue, SharedSingularityComponent? singularity = null)
{
if(!Resolve(uid, ref singularity))
return;
RaiseLocalEvent(uid, new SingularityLevelChangedEvent(singularity.Level, oldValue, singularity));
if (singularity.Level <= 0)
EntityManager.DeleteEntity(singularity.Owner);
}
/// <summary>
/// Alerts the entity hosting the singularity that the level of the singularity has changed without the level actually changing.
/// Used to sync components when the singularity component is added to an entity.
/// </summary>
/// <param name="uid">The uid of the singularity.</param>
/// <param name="singularity">The state of the singularity.</param>
public void UpdateSingularityLevel(EntityUid uid, SharedSingularityComponent? singularity = null)
{
if (Resolve(uid, ref singularity))
UpdateSingularityLevel(uid, singularity.Level, singularity);
}
/// <summary>
/// Updates the amount of radiation the singularity emits to reflect a change in the level or radioactivity per level of the singularity.
/// </summary>
/// <param name="uid">The uid of the singularity to update the radiation of.</param>
/// <param name="singularity">The state of the singularity to update the radiation of.</param>
/// <param name="rads">The state of the radioactivity of the singularity to update.</param>
private void UpdateRadiation(EntityUid uid, SharedSingularityComponent? singularity = null, RadiationSourceComponent? rads = null)
{
if(!Resolve(uid, ref singularity, ref rads, logMissing: false))
return;
rads.Intensity = singularity.Level * singularity.RadsPerLevel;
}
#endregion Getters/Setters
#region Derivations
/// <summary>
/// The scaling factor for the size of a singularities gravity well.
/// </summary>
public const float BaseGravityWellRadius = 2f;
/// <summary>
/// The scaling factor for the base acceleration of a singularities gravity well.
/// </summary>
public const float BaseGravityWellAcceleration = 10f;
/// <summary>
/// The level at and above which a singularity should be capable of breaching containment.
/// </summary>
public const byte SingularityBreachThreshold = 5;
/// <summary>
/// Derives the proper gravity well radius for a singularity from its state.
/// </summary>
/// <param name="singulo">A singularity.</param>
/// <returns>The gravity well radius the singularity should have given its state.</returns>
public float GravPulseRange(SharedSingularityComponent singulo)
=> BaseGravityWellRadius * (singulo.Level + 1);
/// <summary>
/// Derives the proper base gravitational acceleration for a singularity from its state.
/// </summary>
/// <param name="singulo">A singularity.</param>
/// <returns>The base gravitational acceleration the singularity should have given its state.</returns>
public (float, float) GravPulseAcceleration(SharedSingularityComponent singulo)
=> (BaseGravityWellAcceleration * singulo.Level, 0f);
/// <summary>
/// Derives the proper event horizon radius for a singularity from its state.
/// </summary>
/// <param name="singulo">A singularity.</param>
/// <returns>The event horizon radius the singularity should have given its state.</returns>
public float EventHorizonRadius(SharedSingularityComponent singulo)
=> (float) singulo.Level - 0.5f;
/// <summary>
/// Derives whether a singularity should be able to breach containment from its state.
/// </summary>
/// <param name="singulo">A singularity.</param>
/// <returns>Whether the singularity should be able to breach containment.</returns>
public bool CanBreachContainment(SharedSingularityComponent singulo)
=> singulo.Level >= SingularityBreachThreshold;
/// <summary>
/// Derives the proper distortion shader falloff for a singularity from its state.
/// </summary>
/// <param name="singulo">A singularity.</param>
/// <returns>The distortion shader falloff the singularity should have given its state.</returns>
public float GetFalloff(float level)
{
return level switch {
0 => 9999f,
1 => MathF.Sqrt(6.4f),
2 => MathF.Sqrt(7.0f),
3 => MathF.Sqrt(8.0f),
4 => MathF.Sqrt(10.0f),
5 => MathF.Sqrt(12.0f),
6 => MathF.Sqrt(12.0f),
_ => -1.0f
};
}
/// <summary>
/// Derives the proper distortion shader intensity for a singularity from its state.
/// </summary>
/// <param name="singulo">A singularity.</param>
/// <returns>The distortion shader intensity the singularity should have given its state.</returns>
public float GetIntensity(float level)
{
return level switch {
0 => 0.0f,
1 => 3645f,
2 => 103680f,
3 => 1113920f,
4 => 16200000f,
5 => 180000000f,
6 => 180000000f,
_ => -1.0f
};
}
#endregion Derivations
#region Serialization
/// <summary>
/// A state wrapper used to sync the singularity between the server and client.
/// </summary>
[Serializable, NetSerializable]
protected sealed class SingularityComponentState : ComponentState
{
/// <summary>
/// The level of the singularity to sync.
/// </summary>
public readonly byte Level;
public SingularityComponentState(SharedSingularityComponent singulo)
{
Level = singulo.Level;
}
}
#endregion Serialization
#region EventHandlers
/// <summary>
/// Syncs other components with the state of the singularity via event on startup.
/// </summary>
/// <param name="uid">The entity that is becoming a singularity.</param>
/// <param name="comp">The singularity component that is being added to the entity.</param>
/// <param name="args">The event arguments.</param>
protected virtual void OnSingularityStartup(EntityUid uid, SharedSingularityComponent comp, ComponentStartup args)
{
UpdateSingularityLevel(uid, comp);
}
// TODO: Figure out which systems should have control of which coupling.
/// <summary>
/// Syncs the radius of an event horizon associated with a singularity that just changed levels.
/// </summary>
/// <param name="uid">The entity that the event horizon and singularity are attached to.</param>
/// <param name="comp">The event horizon associated with the singularity.</param>
/// <param name="args">The event arguments.</param>
private void UpdateEventHorizon(EntityUid uid, EventHorizonComponent comp, SingularityLevelChangedEvent args)
{
var singulo = args.Singularity;
_horizons.SetRadius(uid, EventHorizonRadius(singulo), false, comp);
_horizons.SetCanBreachContainment(uid, CanBreachContainment(singulo), false, comp);
_horizons.UpdateEventHorizonFixture(uid, eventHorizon: comp);
}
/// <summary>
/// Updates the distortion shader associated with a singularity when the singuarity changes levels.
/// </summary>
/// <param name="uid">The uid of the distortion shader.</param>
/// <param name="comp">The state of the distortion shader.</param>
/// <param name="args">The event arguments.</param>
private void UpdateDistortion(EntityUid uid, SingularityDistortionComponent comp, SingularityLevelChangedEvent args)
{
var newFalloffPower = GetFalloff(args.NewValue);
var newIntensity = GetIntensity(args.NewValue);
if (_containers.IsEntityInContainer(uid))
{
var absFalloffPower = MathF.Abs(newFalloffPower);
var absIntensity = MathF.Abs(newIntensity);
var factor = (1f / DistortionContainerScaling) - 1f;
newFalloffPower = absFalloffPower > 1f ? newFalloffPower * MathF.Pow(absFalloffPower, factor) : newFalloffPower;
newIntensity = absIntensity > 1f ? newIntensity * MathF.Pow(absIntensity, factor) : newIntensity;
}
comp.FalloffPower = newFalloffPower;
comp.Intensity = newIntensity;
}
/// <summary>
/// Updates the distortion shader associated with a singularity when the singuarity is inserted into a container.
/// </summary>
/// <param name="uid">The uid of the distortion shader.</param>
/// <param name="comp">The state of the distortion shader.</param>
/// <param name="args">The event arguments.</param>
private void UpdateDistortion(EntityUid uid, SingularityDistortionComponent comp, EntGotInsertedIntoContainerMessage args)
{
var absFalloffPower = MathF.Abs(comp.FalloffPower);
var absIntensity = MathF.Abs(comp.Intensity);
var factor = (1f / DistortionContainerScaling) - 1f;
comp.FalloffPower = absFalloffPower > 1 ? comp.FalloffPower * MathF.Pow(absFalloffPower, factor) : comp.FalloffPower;
comp.Intensity = absIntensity > 1 ? comp.Intensity * MathF.Pow(absIntensity, factor) : comp.Intensity;
}
/// <summary>
/// Updates the distortion shader associated with a singularity when the singuarity is removed from a container.
/// </summary>
/// <param name="uid">The uid of the distortion shader.</param>
/// <param name="comp">The state of the distortion shader.</param>
/// <param name="args">The event arguments.</param>
private void UpdateDistortion(EntityUid uid, SingularityDistortionComponent comp, EntGotRemovedFromContainerMessage args)
{
var absFalloffPower = MathF.Abs(comp.FalloffPower);
var absIntensity = MathF.Abs(comp.Intensity);
var factor = DistortionContainerScaling - 1;
comp.FalloffPower = absFalloffPower > 1 ? comp.FalloffPower * MathF.Pow(absFalloffPower, factor) : comp.FalloffPower;
comp.Intensity = absIntensity > 1 ? comp.Intensity * MathF.Pow(absIntensity, factor) : comp.Intensity;
}
/// <summary>
/// Updates the state of the physics body associated with a singularity when the singualrity changes levels.
/// </summary>
/// <param name="uid">The entity that the physics body and singularity are attached to.</param>
/// <param name="comp">The physics body associated with the singularity.</param>
/// <param name="args">The event arguments.</param>
private void UpdateBody(EntityUid uid, PhysicsComponent comp, SingularityLevelChangedEvent args)
{
_physics.SetBodyStatus(comp, (args.NewValue > 1) ? BodyStatus.InAir : BodyStatus.OnGround);
if (args.NewValue <= 1 && args.OldValue > 1) // Apparently keeps singularities from getting stuck in the corners of containment fields.
_physics.SetLinearVelocity(comp, Vector2.Zero); // No idea how stopping the singularities movement keeps it from getting stuck though.
}
/// <summary>
/// Updates the appearance of a singularity when the singularities level changes.
/// </summary>
/// <param name="uid">The entity that the singularity is attached to.</param>
/// <param name="comp">The appearance associated with the singularity.</param>
/// <param name="args">The event arguments.</param>
private void UpdateAppearance(EntityUid uid, AppearanceComponent comp, SingularityLevelChangedEvent args)
{
_visualizer.SetData(uid, SingularityVisuals.Level, args.NewValue, comp);
}
/// <summary>
/// Updates the amount of radiation a singularity emits when the singularities level changes.
/// </summary>
/// <param name="uid">The entity that the singularity is attached to.</param>
/// <param name="comp">The radiation source associated with the singularity.</param>
/// <param name="args">The event arguments.</param>
private void UpdateRadiation(EntityUid uid, RadiationSourceComponent comp, SingularityLevelChangedEvent args)
{
UpdateRadiation(uid, args.Singularity, comp);
}
#endregion EventHandlers
}

View File

@@ -0,0 +1,31 @@
using Content.Shared.Singularity.Components;
namespace Content.Shared.Singularity.Events;
/// <summary>
/// An event raised whenever a singularity changes its level.
/// </summary>
public sealed class SingularityLevelChangedEvent : EntityEventArgs
{
/// <summary>
/// The new level of the singularity.
/// </summary>
public readonly byte NewValue;
/// <summary>
/// The previous level of the singularity.
/// </summary>
public readonly byte OldValue;
/// <summary>
/// The singularity that just changed level.
/// </summary>
public readonly SharedSingularityComponent Singularity;
public SingularityLevelChangedEvent(byte newValue, byte oldValue, SharedSingularityComponent singularity)
{
NewValue = newValue;
OldValue = oldValue;
Singularity = singularity;
}
}

View File

@@ -1,143 +0,0 @@
using Content.Shared.Ghost;
using Content.Shared.Radiation;
using Content.Shared.Radiation.Components;
using Content.Shared.Singularity.Components;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
namespace Content.Shared.Singularity
{
public abstract class SharedSingularitySystem : EntitySystem
{
[Dependency] private readonly FixtureSystem _fixtures = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
public const string DeleteFixture = "DeleteCircle";
private float GetFalloff(int level)
{
return level switch
{
0 => 9999f,
1 => MathF.Sqrt(6.4f),
2 => MathF.Sqrt(7.0f),
3 => MathF.Sqrt(8.0f),
4 => MathF.Sqrt(10.0f),
5 => MathF.Sqrt(12.0f),
6 => MathF.Sqrt(12.0f),
_ => -1.0f
};
}
private float GetIntensity(int level)
{
return level switch
{
0 => 0.0f,
1 => 3645f,
2 => 103680f,
3 => 1113920f,
4 => 16200000f,
5 => 180000000f,
6 => 180000000f,
_ => -1.0f
};
}
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedSingularityComponent, PreventCollideEvent>(OnPreventCollide);
}
protected void OnPreventCollide(EntityUid uid, SharedSingularityComponent component, ref PreventCollideEvent args)
{
PreventCollide(uid, component, ref args);
}
protected virtual bool PreventCollide(EntityUid uid, SharedSingularityComponent component, ref PreventCollideEvent args)
{
var otherUid = args.BodyB.Owner;
// For prediction reasons always want the client to ignore these.
if (EntityManager.HasComponent<MapGridComponent>(otherUid) ||
EntityManager.HasComponent<SharedGhostComponent>(otherUid))
{
args.Cancelled = true;
return true;
}
// If we're above 4 then breach containment
// otherwise, check if it's containment and just keep the collision
if (EntityManager.HasComponent<SharedContainmentFieldComponent>(otherUid) ||
EntityManager.HasComponent<SharedContainmentFieldGeneratorComponent>(otherUid))
{
if (component.Level > 4)
{
args.Cancelled = true;
}
return true;
}
return false;
}
public void ChangeSingularityLevel(SharedSingularityComponent singularity, int value)
{
if (value == singularity.Level)
{
return;
}
value = Math.Clamp(value, 0, 6);
var physics = EntityManager.GetComponentOrNull<PhysicsComponent>(singularity.Owner);
if (singularity.Level > 1 && value <= 1)
{
// Prevents it getting stuck (see SingularityController.MoveSingulo)
if (physics != null)
{
_physics.SetLinearVelocity(physics, Vector2.Zero);
}
}
singularity.Level = value;
if (EntityManager.TryGetComponent(singularity.Owner, out RadiationSourceComponent? source))
{
source.Intensity = singularity.RadsPerLevel * value;
}
_appearance.SetData(singularity.Owner, SingularityVisuals.Level, value);
if (physics != null)
{
var fixture = _fixtures.GetFixtureOrNull(physics, DeleteFixture);
if (fixture != null)
{
var circle = (PhysShapeCircle) fixture.Shape;
circle.Radius = value - 0.5f;
fixture.Hard = value <= 4;
}
}
if (EntityManager.TryGetComponent(singularity.Owner, out SingularityDistortionComponent? distortion))
{
distortion.FalloffPower = GetFalloff(value);
distortion.Intensity = GetIntensity(value);
}
singularity.Dirty();
}
}
}