Files
tbd-station-14/Content.Shared/GameObjects/Verbs/SharedVerbSystem.cs
metalgearsloth cc1125cd91 No context menu through occluder (#1934)
* No context menu through occluder

* Fix disabled occluders

* Comment

* Server-side verb ray check

Decided to add a buffer because the entity is at the very edge of the context box (0.5, 0.5) then need to make sure that entities at the other end of the box are ignored.

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2020-08-29 12:46:42 +02:00

71 lines
2.6 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Physics;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Shared.GameObjects.Verbs
{
public class SharedVerbSystem : EntitySystem
{
private SharedInteractionSystem _interactionSystem = null!;
public override void Initialize()
{
base.Initialize();
_interactionSystem = Get<SharedInteractionSystem>();
}
/// <summary>
/// Get all of the entities relevant for the contextmenu
/// </summary>
/// <param name="player"></param>
/// <param name="targetPos"></param>
/// <param name="contextEntities"></param>
/// <param name="buffer">Whether we should slightly extend out the ignored range for the ray predicated</param>
/// <returns></returns>
protected bool TryGetContextEntities(IEntity player, MapCoordinates targetPos, [NotNullWhen(true)] out List<IEntity>? contextEntities, bool buffer = false)
{
contextEntities = null;
var length = buffer ? 1.0f: 0.5f;
var entities = EntityManager.GetEntitiesIntersecting(targetPos.MapId,
Box2.CenteredAround(targetPos.Position, (length, length))).ToList();
if (entities.Count == 0)
{
return false;
}
// Check if we have LOS to the clicked-location, otherwise no popup.
var vectorDiff = player.Transform.MapPosition.Position - targetPos.Position;
var distance = vectorDiff.Length + 0.01f;
Func<IEntity, bool> ignored = entity => entities.Contains(entity) ||
entity == player ||
!entity.TryGetComponent(out OccluderComponent? occluder) ||
!occluder.Enabled;
var result = _interactionSystem.InRangeUnobstructed(
player.Transform.MapPosition,
targetPos,
distance,
(int) CollisionGroup.Opaque,
ignored);
if (!result)
{
return false;
}
contextEntities = entities;
return true;
}
}
}