Refactor InRangeUnobstructed and add extension methods (#1925)

* Sort out InRangeUnobstructed and add extension methods

* Rename client RangeChecks to RangeExtensions

* Add container extension methods and test

* Add missing component methods

Component to container
Grid coordinates to container
Map coordinates to container
Local player to container

* Actually use the field

* Merge fixes

* Add popup argument to local player extension methods

* Reduce code repetition for client range extensions
This commit is contained in:
DrSmugleaf
2020-08-30 11:37:06 +02:00
committed by GitHub
parent 9ec3ddf368
commit 9d6c394f6b
39 changed files with 1287 additions and 359 deletions

View File

@@ -142,8 +142,7 @@ namespace Content.Client.GameObjects.EntitySystems
if (_entityManager.TryGetEntity(args.EntityUid, out var entity))
{
// check if the entity is reachable
if (_interactionSystem.InRangeUnobstructed(dragger.Transform.MapPosition,
entity.Transform.MapPosition, ignoredEnt: dragger) == false)
if (!_interactionSystem.InRangeUnobstructed(dragger, entity))
{
return false;
}
@@ -193,8 +192,8 @@ namespace Content.Client.GameObjects.EntitySystems
// tell the server we are dropping if we are over a valid drop target in range.
// We don't use args.EntityUid here because drag interactions generally should
// work even if there's something "on top" of the drop target
if (_interactionSystem.InRangeUnobstructed(_dragger.Transform.MapPosition,
args.Coordinates.ToMap(_mapManager), ignoredEnt: _dragger, ignoreInsideBlocker: true) == false)
if (!_interactionSystem.InRangeUnobstructed(_dragger,
args.Coordinates, ignoreInsideBlocker: true))
{
CancelDrag(false, null);
return false;
@@ -288,8 +287,7 @@ namespace Content.Client.GameObjects.EntitySystems
if (anyValidDraggable)
{
// highlight depending on whether its in or out of range
var inRange = _interactionSystem.InRangeUnobstructed(_dragger.Transform.MapPosition,
pvsEntity.Transform.MapPosition, ignoredEnt: _dragger);
var inRange = _interactionSystem.InRangeUnobstructed(_dragger, pvsEntity);
inRangeSprite.PostShader = inRange ? _dropTargetInRangeShader : _dropTargetOutOfRangeShader;
inRangeSprite.RenderOrder = EntityManager.CurrentTick.Value;
highlightedSprites.Add(inRangeSprite);
@@ -377,8 +375,7 @@ namespace Content.Client.GameObjects.EntitySystems
return;
}
// still in range of the thing we are dragging?
if (_interactionSystem.InRangeUnobstructed(_dragger.Transform.MapPosition,
_draggedEntity.Transform.MapPosition, ignoredEnt: _dragger) == false)
if (!_interactionSystem.InRangeUnobstructed(_dragger, _draggedEntity))
{
CancelDrag(false, null);
return;

View File

@@ -1,6 +1,7 @@
using Content.Client.GameObjects.Components.Instruments;
using Content.Client.UserInterface.Stylesheets;
using Content.Shared.GameObjects.EntitySystems;
using Content.Client.Utility;
using Content.Shared.Utility;
using Robust.Client.Audio.Midi;
using Robust.Client.Graphics.Drawing;
using Robust.Client.Interfaces.UserInterface;
@@ -9,7 +10,6 @@ using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
@@ -197,9 +197,7 @@ namespace Content.Client.Instruments
|| conMan.Owner != localPlayer.ControlledEntity))) return;
// We check that we're in range unobstructed just in case.
if(!EntitySystem.Get<SharedInteractionSystem>()
.InRangeUnobstructed(localPlayer.ControlledEntity.Transform.MapPosition,
instrumentEnt.Transform.MapPosition, ignoredEnt:instrumentEnt)) return;
if (!localPlayer.InRangeUnobstructed(instrumentEnt)) return;
if (!_midiManager.IsMidiFile(filename))
{

View File

@@ -2,7 +2,8 @@
using System.Collections.Immutable;
using System.Linq;
using Content.Client.GameObjects.Components;
using Content.Shared.GameObjects.EntitySystems;
using Content.Client.Utility;
using Content.Shared.Utility;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Client.Interfaces.GameObjects;
using Robust.Client.Interfaces.Graphics.ClientEye;
@@ -67,13 +68,7 @@ namespace Content.Client.State
var inRange = false;
if (localPlayer.ControlledEntity != null && entityToClick != null)
{
var playerPos = localPlayer.ControlledEntity.Transform.MapPosition;
var entityPos = entityToClick.Transform.MapPosition;
inRange = EntitySystemManager.GetEntitySystem<SharedInteractionSystem>()
.InRangeUnobstructed(playerPos, entityPos,
predicate: entity =>
entity == localPlayer.ControlledEntity || entity == entityToClick,
ignoreInsideBlocker: true);
inRange = localPlayer.InRangeUnobstructed(entityToClick, ignoreInsideBlocker: true);
}
InteractionOutlineComponent outline;

View File

@@ -0,0 +1,93 @@
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Physics;
using Robust.Client.Player;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using static Content.Shared.GameObjects.EntitySystems.SharedInteractionSystem;
namespace Content.Client.Utility
{
public static class RangeExtensions
{
private static SharedInteractionSystem SharedInteractionSystem => EntitySystem.Get<SharedInteractionSystem>();
public static bool InRangeUnobstructed(
this LocalPlayer origin,
IEntity other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
var otherPosition = other.Transform.MapPosition;
return origin.InRangeUnobstructed(otherPosition, range, collisionMask, predicate, ignoreInsideBlocker,
popup);
}
public static bool InRangeUnobstructed(
this LocalPlayer origin,
IComponent other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
return origin.InRangeUnobstructed(other.Owner, range, collisionMask, predicate, ignoreInsideBlocker, popup);
}
public static bool InRangeUnobstructed(
this LocalPlayer origin,
IContainer other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
return origin.InRangeUnobstructed(other.Owner, range, collisionMask, predicate, ignoreInsideBlocker, popup);
}
public static bool InRangeUnobstructed(
this LocalPlayer origin,
GridCoordinates other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
var mapManager = IoCManager.Resolve<IMapManager>();
var otherPosition = other.ToMap(mapManager);
return origin.InRangeUnobstructed(otherPosition, range, collisionMask, predicate, ignoreInsideBlocker,
popup);
}
public static bool InRangeUnobstructed(
this LocalPlayer origin,
MapCoordinates other,
float range = InteractionRange,
CollisionGroup collisionMask = CollisionGroup.Impassable,
Ignored predicate = null,
bool ignoreInsideBlocker = false,
bool popup = false)
{
var originEntity = origin.ControlledEntity;
if (originEntity == null)
{
// TODO: Take into account the player's camera position?
return false;
}
return SharedInteractionSystem.InRangeUnobstructed(originEntity, other, range, collisionMask, predicate,
ignoreInsideBlocker, popup);
}
}
}