BUI range check changes (#3099)

This commit is contained in:
Leon Friedrich
2022-08-04 11:00:38 +12:00
committed by GitHub
parent b5279f3f89
commit 57e5ceef1a
4 changed files with 24 additions and 16 deletions

View File

@@ -81,6 +81,8 @@ namespace Robust.Server.GameObjects
{
private bool _isActive;
public float InteractionRangeSqrd;
public object UiKey { get; }
public ServerUserInterfaceComponent Owner { get; }
private readonly HashSet<IPlayerSession> _subscribedSessions = new();
@@ -105,6 +107,7 @@ namespace Robust.Server.GameObjects
RequireInputValidation = data.RequireInputValidation;
UiKey = data.UiKey;
Owner = owner;
InteractionRangeSqrd = data.InteractionRange * MathF.Abs(data.InteractionRange);
}
/// <summary>

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
@@ -7,7 +7,6 @@ using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
using Robust.Shared.ViewVariables;
namespace Robust.Server.GameObjects
@@ -15,8 +14,7 @@ namespace Robust.Server.GameObjects
[UsedImplicitly]
public sealed class UserInterfaceSystem : SharedUserInterfaceSystem
{
private const float MaxWindowRange = 2;
private const float MaxWindowRangeSquared = MaxWindowRange * MaxWindowRange;
[Dependency] private readonly TransformSystem _xformSys = default!;
private readonly List<IPlayerSession> _sessionCache = new();
@@ -101,9 +99,10 @@ namespace Robust.Server.GameObjects
/// <inheritdoc />
public override void Update(float frameTime)
{
var query = GetEntityQuery<TransformComponent>();
foreach (var userInterface in _activeInterfaces.ToList())
{
CheckRange(userInterface);
CheckRange(userInterface, query);
userInterface.DispatchPendingState();
}
}
@@ -111,35 +110,35 @@ namespace Robust.Server.GameObjects
/// <summary>
/// Verify that the subscribed clients are still in range of the interface.
/// </summary>
private void CheckRange(BoundUserInterface ui)
private void CheckRange(BoundUserInterface ui, EntityQuery<TransformComponent> query)
{
if (ui.InteractionRangeSqrd <= 0)
return;
// We have to cache the set of sessions because Unsubscribe modifies the original.
_sessionCache.Clear();
_sessionCache.AddRange(ui.SubscribedSessions);
var transform = EntityManager.GetComponent<TransformComponent>(ui.Owner.Owner);
var uiPos = transform.WorldPosition;
var transform = query.GetComponent(ui.Owner.Owner);
var uiPos = _xformSys.GetWorldPosition(transform, query);
var uiMap = transform.MapID;
foreach (var session in _sessionCache)
{
var attachedEntityTransform = session.AttachedEntityTransform;
// The component manages the set of sessions, so this invalid session should be removed soon.
if (attachedEntityTransform == null)
if (!query.TryGetComponent(session.AttachedEntity, out var xform))
{
continue;
}
if (uiMap != attachedEntityTransform.MapID)
if (uiMap != xform.MapID)
{
ui.Close(session);
continue;
}
var distanceSquared = (uiPos - attachedEntityTransform.WorldPosition).LengthSquared;
if (distanceSquared > MaxWindowRangeSquared)
var distanceSquared = (uiPos - _xformSys.GetWorldPosition(xform, query)).LengthSquared;
if (distanceSquared > ui.InteractionRangeSqrd)
{
ui.Close(session);
}

View File

@@ -79,7 +79,7 @@ namespace Robust.Server.Placement
var dirRcv = msg.DirRcv;
var session = _playerManager.GetSessionByChannel(msg.MsgChannel);
var plyEntity = session.AttachedEntityTransform;
var plyEntity = _entityManager.GetComponentOrNull<TransformComponent>(session.AttachedEntity);
// Don't have an entity, don't get to place.
if (plyEntity == null)

View File

@@ -22,6 +22,12 @@ namespace Robust.Shared.GameObjects
[DataField("type", readOnly: true, required: true)]
public string ClientType { get; set; } = default!;
/// <summary>
/// Maximum range before a BUI auto-closes. A non-positive number means there is no limit.
/// </summary>
[DataField("range")]
public float InteractionRange = 2f;
// TODO BUI move to content?
// I've tried to keep the name general, but really this is a bool for: can ghosts/stunned/dead people press buttons on this UI?
/// <summary>