fix: anomaly shooting no longer throws when it have no possible targets in range (#37927)
Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using Content.Server.Anomaly.Components;
|
||||
using Content.Server.Weapons.Ranged.Systems;
|
||||
@@ -7,7 +6,6 @@ using Content.Shared.Mobs.Components;
|
||||
using Content.Shared.Projectiles;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Anomaly.Effects;
|
||||
@@ -24,10 +22,19 @@ public sealed class ProjectileAnomalySystem : EntitySystem
|
||||
[Dependency] private readonly GunSystem _gunSystem = default!;
|
||||
[Dependency] private readonly SharedMapSystem _map = default!;
|
||||
|
||||
private EntityQuery<TransformComponent> _xFormQuery;
|
||||
private EntityQuery<MobStateComponent> _mobQuery;
|
||||
|
||||
/// <summary> Pre-allocated collection for calculating entities in range. </summary>
|
||||
private readonly HashSet<EntityUid> _inRange = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<ProjectileAnomalyComponent, AnomalyPulseEvent>(OnPulse);
|
||||
SubscribeLocalEvent<ProjectileAnomalyComponent, AnomalySupercriticalEvent>(OnSupercritical);
|
||||
|
||||
_xFormQuery = GetEntityQuery<TransformComponent>();
|
||||
_mobQuery = GetEntityQuery<MobStateComponent>();
|
||||
}
|
||||
|
||||
private void OnPulse(EntityUid uid, ProjectileAnomalyComponent component, ref AnomalyPulseEvent args)
|
||||
@@ -43,16 +50,19 @@ public sealed class ProjectileAnomalySystem : EntitySystem
|
||||
private void ShootProjectilesAtEntities(EntityUid uid, ProjectileAnomalyComponent component, float severity)
|
||||
{
|
||||
var projectileCount = (int)MathF.Round(MathHelper.Lerp(component.MinProjectiles, component.MaxProjectiles, severity));
|
||||
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||
var mobQuery = GetEntityQuery<MobStateComponent>();
|
||||
var xform = xformQuery.GetComponent(uid);
|
||||
|
||||
var inRange = _lookup.GetEntitiesInRange(uid, component.ProjectileRange * severity, LookupFlags.Dynamic).ToList();
|
||||
_random.Shuffle(inRange);
|
||||
var xform = _xFormQuery.GetComponent(uid);
|
||||
|
||||
_inRange.Clear();
|
||||
_lookup.GetEntitiesInRange(uid, component.ProjectileRange * severity, _inRange, LookupFlags.Dynamic);
|
||||
|
||||
if (_inRange.Count == 0)
|
||||
return;
|
||||
|
||||
var priority = new List<EntityUid>();
|
||||
foreach (var entity in inRange)
|
||||
foreach (var entity in _inRange)
|
||||
{
|
||||
if (mobQuery.HasComponent(entity))
|
||||
if (_mobQuery.HasComponent(entity))
|
||||
priority.Add(entity);
|
||||
}
|
||||
|
||||
@@ -60,17 +70,20 @@ public sealed class ProjectileAnomalySystem : EntitySystem
|
||||
while (projectileCount > 0)
|
||||
{
|
||||
Log.Debug($"{projectileCount}");
|
||||
var target = priority.Any()
|
||||
var target = priority.Count > 0
|
||||
? _random.PickAndTake(priority)
|
||||
: _random.Pick(inRange);
|
||||
: _random.Pick(_inRange);
|
||||
|
||||
var targetCoords = xformQuery.GetComponent(target).Coordinates.Offset(_random.NextVector2(0.5f));
|
||||
var targetXForm= _xFormQuery.GetComponent(target);
|
||||
var targetCoords = targetXForm.Coordinates.Offset(_random.NextVector2(0.5f));
|
||||
|
||||
ShootProjectile(
|
||||
uid, component,
|
||||
uid,
|
||||
component,
|
||||
xform.Coordinates,
|
||||
targetCoords,
|
||||
severity);
|
||||
severity
|
||||
);
|
||||
projectileCount--;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +93,8 @@ public sealed class ProjectileAnomalySystem : EntitySystem
|
||||
ProjectileAnomalyComponent component,
|
||||
EntityCoordinates coords,
|
||||
EntityCoordinates targetCoords,
|
||||
float severity)
|
||||
float severity
|
||||
)
|
||||
{
|
||||
var mapPos = _xform.ToMapCoordinates(coords);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user