Files
tbd-station-14/Content.Shared/Anomaly/Effects/SharedGravityAnomalySystem.cs
Pieter-Jan Briers 68ce53ae17 Random spontaneous cleanup PR (#25131)
* Use new Subs.CVar helper

Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe.

This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown.

* Fix a bunch of warnings

* More warning fixes

* Use new DateTime serializer to get rid of ISerializationHooks in changelog code.

* Get rid of some more ISerializationHooks for enums

* And a little more

* Apply suggestions from code review

Co-authored-by: 0x6273 <0x40@keemail.me>

---------

Co-authored-by: 0x6273 <0x40@keemail.me>
2024-02-13 16:48:39 -05:00

82 lines
3.2 KiB
C#

using System.Linq;
using Content.Shared.Anomaly.Components;
using Content.Shared.Anomaly.Effects.Components;
using Content.Shared.Ghost;
using Content.Shared.Throwing;
using Robust.Shared.Map;
using Content.Shared.Physics;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics.Components;
namespace Content.Shared.Anomaly.Effects;
public abstract class SharedGravityAnomalySystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly ThrowingSystem _throwing = default!;
[Dependency] private readonly SharedTransformSystem _xform = default!;
[Dependency] private readonly SharedMapSystem _mapSystem = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<GravityAnomalyComponent, AnomalyPulseEvent>(OnAnomalyPulse);
SubscribeLocalEvent<GravityAnomalyComponent, AnomalySupercriticalEvent>(OnSupercritical);
}
private void OnAnomalyPulse(EntityUid uid, GravityAnomalyComponent component, ref AnomalyPulseEvent args)
{
var xform = Transform(uid);
var range = component.MaxThrowRange * args.Severity;
var strength = component.MaxThrowStrength * args.Severity;
var lookup = _lookup.GetEntitiesInRange(uid, range, LookupFlags.Dynamic | LookupFlags.Sundries);
var xformQuery = GetEntityQuery<TransformComponent>();
var worldPos = _xform.GetWorldPosition(xform, xformQuery);
var physQuery = GetEntityQuery<PhysicsComponent>();
foreach (var ent in lookup)
{
if (physQuery.TryGetComponent(ent, out var phys)
&& (phys.CollisionMask & (int) CollisionGroup.GhostImpassable) != 0)
continue;
var foo = _xform.GetWorldPosition(ent, xformQuery) - worldPos;
_throwing.TryThrow(ent, foo * 10, strength, uid, 0);
}
}
private void OnSupercritical(EntityUid uid, GravityAnomalyComponent component, ref AnomalySupercriticalEvent args)
{
var xform = Transform(uid);
if (!TryComp(xform.GridUid, out MapGridComponent? grid))
return;
var worldPos = _xform.GetWorldPosition(xform);
var tileref = _mapSystem.GetTilesIntersecting(
xform.GridUid.Value,
grid,
new Circle(worldPos, component.SpaceRange))
.ToArray();
var tiles = tileref.Select(t => (t.GridIndices, Tile.Empty)).ToList();
_mapSystem.SetTiles(xform.GridUid.Value, grid, tiles);
var range = component.MaxThrowRange * 2;
var strength = component.MaxThrowStrength * 2;
var lookup = _lookup.GetEntitiesInRange(uid, range, LookupFlags.Dynamic | LookupFlags.Sundries);
var xformQuery = GetEntityQuery<TransformComponent>();
var physQuery = GetEntityQuery<PhysicsComponent>();
foreach (var ent in lookup)
{
if (physQuery.TryGetComponent(ent, out var phys)
&& (phys.CollisionMask & (int) CollisionGroup.GhostImpassable) != 0)
continue;
var foo = _xform.GetWorldPosition(ent, xformQuery) - worldPos;
_throwing.TryThrow(ent, foo * 5, strength, uid, 0);
}
}
}