Better pinpointer accuracy + small cleanup (#12378)

This commit is contained in:
Alex Evgrashin
2022-11-04 05:15:23 +01:00
committed by GitHub
parent 2a2be3d619
commit 2ad9a5dfac
9 changed files with 122 additions and 139 deletions

View File

@@ -1,10 +1,7 @@
using Content.Shared.Pinpointer; using Content.Shared.Pinpointer;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Client.Pinpointer namespace Content.Client.Pinpointer
{ {
@@ -40,9 +37,9 @@ namespace Content.Client.Pinpointer
if (args.Current is not PinpointerComponentState state) if (args.Current is not PinpointerComponentState state)
return; return;
SetActive(uid, state.IsActive, pinpointer); pinpointer.IsActive = state.IsActive;
SetDirection(uid, state.DirectionToTarget, pinpointer); pinpointer.ArrowAngle = state.ArrowAngle;
SetDistance(uid, state.DistanceToTarget, pinpointer); pinpointer.DistanceToTarget = state.DistanceToTarget;
} }
private void UpdateAppearance(EntityUid uid, PinpointerComponent? pinpointer = null, private void UpdateAppearance(EntityUid uid, PinpointerComponent? pinpointer = null,
@@ -55,13 +52,13 @@ namespace Content.Client.Pinpointer
_appearance.SetData(uid, PinpointerVisuals.TargetDistance, pinpointer.DistanceToTarget, appearance); _appearance.SetData(uid, PinpointerVisuals.TargetDistance, pinpointer.DistanceToTarget, appearance);
} }
private void UpdateDirAppearance(EntityUid uid, Direction dir,PinpointerComponent? pinpointer = null, private void UpdateArrowAngle(EntityUid uid, Angle angle, PinpointerComponent? pinpointer = null,
AppearanceComponent? appearance = null) AppearanceComponent? appearance = null)
{ {
if (!Resolve(uid, ref pinpointer, ref appearance)) if (!Resolve(uid, ref pinpointer, ref appearance))
return; return;
_appearance.SetData(uid, PinpointerVisuals.TargetDirection, dir, appearance); _appearance.SetData(uid, PinpointerVisuals.ArrowAngle, angle, appearance);
} }
/// <summary> /// <summary>
@@ -70,20 +67,12 @@ namespace Content.Client.Pinpointer
/// </summary> /// </summary>
private void UpdateEyeDir(EntityUid uid, PinpointerComponent? pinpointer = null) private void UpdateEyeDir(EntityUid uid, PinpointerComponent? pinpointer = null)
{ {
if (!Resolve(uid, ref pinpointer)) if (!Resolve(uid, ref pinpointer) || !pinpointer.HasTarget)
return; return;
var worldDir = pinpointer.DirectionToTarget;
if (worldDir == Direction.Invalid)
{
UpdateDirAppearance(uid, Direction.Invalid, pinpointer);
return;
}
var eye = _eyeManager.CurrentEye; var eye = _eyeManager.CurrentEye;
var angle = worldDir.ToAngle() + eye.Rotation; var angle = pinpointer.ArrowAngle + eye.Rotation;
var eyeDir = angle.GetDir(); UpdateArrowAngle(uid, angle, pinpointer);
UpdateDirAppearance(uid, eyeDir, pinpointer);
} }
} }
} }

View File

@@ -1,64 +0,0 @@
using Content.Shared.Pinpointer;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Client.Pinpointer
{
[UsedImplicitly]
public sealed class PinpointerVisualizer : AppearanceVisualizer
{
[Obsolete("Subscribe to AppearanceChangeEvent instead.")]
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
var entities = IoCManager.Resolve<IEntityManager>();
if (!entities.TryGetComponent(component.Owner, out SpriteComponent? sprite))
return;
// check if pinpointer screen is active
if (!component.TryGetData(PinpointerVisuals.IsActive, out bool isActive) || !isActive)
{
sprite.LayerSetVisible(PinpointerLayers.Screen, false);
return;
}
// check if it has direction to target
sprite.LayerSetVisible(PinpointerLayers.Screen, true);
sprite.LayerSetRotation(PinpointerLayers.Screen, Angle.Zero);
if (!component.TryGetData(PinpointerVisuals.TargetDirection, out Direction dir) || dir == Direction.Invalid)
{
sprite.LayerSetState(PinpointerLayers.Screen, "pinonnull");
return;
}
// check distance to target
if (!component.TryGetData(PinpointerVisuals.TargetDistance, out Distance dis))
dis = Distance.UNKNOWN;
switch (dis)
{
case Distance.REACHED:
sprite.LayerSetState(PinpointerLayers.Screen, "pinondirect");
break;
case Distance.CLOSE:
sprite.LayerSetState(PinpointerLayers.Screen, "pinonclose");
sprite.LayerSetRotation(PinpointerLayers.Screen, dir.ToAngle());
break;
case Distance.MEDIUM:
sprite.LayerSetState(PinpointerLayers.Screen, "pinonmedium");
sprite.LayerSetRotation(PinpointerLayers.Screen, dir.ToAngle());
break;
case Distance.FAR:
case Distance.UNKNOWN:
sprite.LayerSetState(PinpointerLayers.Screen, "pinonfar");
sprite.LayerSetRotation(PinpointerLayers.Screen, dir.ToAngle());
break;
}
}
}
}

View File

@@ -0,0 +1,60 @@
using Content.Shared.Pinpointer;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
namespace Content.Client.Pinpointer
{
[UsedImplicitly]
public sealed class PinpointerVisualizerSystem : VisualizerSystem<PinpointerComponent>
{
protected override void OnAppearanceChange(EntityUid uid, PinpointerComponent component, ref AppearanceChangeEvent args)
{
base.OnAppearanceChange(uid, component, ref args);
if (!TryComp(component.Owner, out SpriteComponent? sprite))
return;
// check if pinpointer screen is active
if (!args.Component.TryGetData(PinpointerVisuals.IsActive, out bool isActive) || !isActive)
{
sprite.LayerSetVisible(PinpointerLayers.Screen, false);
return;
}
sprite.LayerSetVisible(PinpointerLayers.Screen, true);
// check distance and direction to target
if (!args.Component.TryGetData(PinpointerVisuals.TargetDistance, out Distance dis) ||
!args.Component.TryGetData(PinpointerVisuals.ArrowAngle, out Angle angle))
{
sprite.LayerSetState(PinpointerLayers.Screen, "pinonnull");
sprite.LayerSetRotation(PinpointerLayers.Screen, Angle.Zero);
return;
}
switch (dis)
{
case Distance.Reached:
sprite.LayerSetState(PinpointerLayers.Screen, "pinondirect");
sprite.LayerSetRotation(PinpointerLayers.Screen, Angle.Zero);
break;
case Distance.Close:
sprite.LayerSetState(PinpointerLayers.Screen, "pinonclose");
sprite.LayerSetRotation(PinpointerLayers.Screen, angle);
break;
case Distance.Medium:
sprite.LayerSetState(PinpointerLayers.Screen, "pinonmedium");
sprite.LayerSetRotation(PinpointerLayers.Screen, angle);
break;
case Distance.Far:
sprite.LayerSetState(PinpointerLayers.Screen, "pinonfar");
sprite.LayerSetRotation(PinpointerLayers.Screen, angle);
break;
case Distance.Unknown:
sprite.LayerSetState(PinpointerLayers.Screen, "pinonnull");
sprite.LayerSetRotation(PinpointerLayers.Screen, Angle.Zero);
break;
}
}
}
}

View File

@@ -26,12 +26,12 @@ namespace Content.Server.Pinpointer
private void OnLocateTarget(HyperspaceJumpCompletedEvent ev) private void OnLocateTarget(HyperspaceJumpCompletedEvent ev)
{ {
// This feels kind of expensive, but it only happens once per hyperspace jump // This feels kind of expensive, but it only happens once per hyperspace jump
foreach (var uid in ActivePinpointers)
// todo: ideally, you would need to raise this event only on jumped entities
// this code update ALL pinpointers in game
foreach (var pinpointer in EntityQuery<PinpointerComponent>())
{ {
if (TryComp<PinpointerComponent>(uid, out var component)) LocateTarget(pinpointer.Owner, pinpointer);
{
LocateTarget(uid, component);
}
} }
} }
@@ -58,9 +58,9 @@ namespace Content.Server.Pinpointer
// because target or pinpointer can move // because target or pinpointer can move
// we need to update pinpointers arrow each frame // we need to update pinpointers arrow each frame
foreach (var uid in ActivePinpointers) foreach (var pinpointer in EntityQuery<PinpointerComponent>())
{ {
UpdateDirectionToTarget(uid); UpdateDirectionToTarget(pinpointer.Owner, pinpointer);
} }
} }
@@ -85,14 +85,14 @@ namespace Content.Server.Pinpointer
foreach (var comp in EntityManager.GetAllComponents(whitelist)) foreach (var comp in EntityManager.GetAllComponents(whitelist))
{ {
if (!xformQuery.TryGetComponent(comp.Owner, out var compXform) || if (!xformQuery.TryGetComponent(comp.Owner, out var compXform) || compXform.MapID != mapId)
compXform.MapID != mapId) continue; continue;
var dist = (_transform.GetWorldPosition(compXform, xformQuery) - worldPos).LengthSquared; var dist = (_transform.GetWorldPosition(compXform, xformQuery) - worldPos).LengthSquared;
l.TryAdd(dist, comp.Owner); l.TryAdd(dist, comp.Owner);
} }
// return uid with a smallest distacne // return uid with a smallest distance
return l.Count > 0 ? l.First().Value : null; return l.Count > 0 ? l.First().Value : null;
} }
@@ -120,33 +120,34 @@ namespace Content.Server.Pinpointer
if (!Resolve(uid, ref pinpointer)) if (!Resolve(uid, ref pinpointer))
return; return;
if (!pinpointer.IsActive)
return;
var target = pinpointer.Target; var target = pinpointer.Target;
if (target == null || !EntityManager.EntityExists(target.Value)) if (target == null || !EntityManager.EntityExists(target.Value))
{ {
SetDirection(uid, Direction.Invalid, pinpointer); SetDistance(uid, Distance.Unknown, pinpointer);
SetDistance(uid, Distance.UNKNOWN, pinpointer);
return; return;
} }
var dirVec = CalculateDirection(uid, target.Value); var dirVec = CalculateDirection(uid, target.Value);
if (dirVec != null) if (dirVec != null)
{ {
var dir = dirVec.Value.GetDir(); var angle = dirVec.Value.ToWorldAngle();
SetDirection(uid, dir, pinpointer); TrySetArrowAngle(uid, angle, pinpointer);
var dist = CalculateDistance(uid, dirVec.Value, pinpointer); var dist = CalculateDistance(uid, dirVec.Value, pinpointer);
SetDistance(uid, dist, pinpointer); SetDistance(uid, dist, pinpointer);
} }
else else
{ {
SetDirection(uid, Direction.Invalid, pinpointer); SetDistance(uid, Distance.Unknown, pinpointer);
SetDistance(uid, Distance.UNKNOWN, pinpointer);
} }
} }
/// <summary> /// <summary>
/// Calculate direction from pinUid to trgUid /// Calculate direction from pinUid to trgUid
/// </summary> /// </summary>
/// <returns>Null if failed to caluclate distance between two entities</returns> /// <returns>Null if failed to calculate distance between two entities</returns>
private Vector2? CalculateDirection(EntityUid pinUid, EntityUid trgUid) private Vector2? CalculateDirection(EntityUid pinUid, EntityUid trgUid)
{ {
var xformQuery = GetEntityQuery<TransformComponent>(); var xformQuery = GetEntityQuery<TransformComponent>();
@@ -169,17 +170,17 @@ namespace Content.Server.Pinpointer
private Distance CalculateDistance(EntityUid uid, Vector2 vec, PinpointerComponent? pinpointer = null) private Distance CalculateDistance(EntityUid uid, Vector2 vec, PinpointerComponent? pinpointer = null)
{ {
if (!Resolve(uid, ref pinpointer)) if (!Resolve(uid, ref pinpointer))
return Distance.UNKNOWN; return Distance.Unknown;
var dist = vec.Length; var dist = vec.Length;
if (dist <= pinpointer.ReachedDistance) if (dist <= pinpointer.ReachedDistance)
return Distance.REACHED; return Distance.Reached;
else if (dist <= pinpointer.CloseDistance) else if (dist <= pinpointer.CloseDistance)
return Distance.CLOSE; return Distance.Close;
else if (dist <= pinpointer.MediumDistance) else if (dist <= pinpointer.MediumDistance)
return Distance.MEDIUM; return Distance.Medium;
else else
return Distance.FAR; return Distance.Far;
} }
} }
} }

View File

@@ -24,27 +24,34 @@ namespace Content.Shared.Pinpointer
[DataField("reachedDistance")] [DataField("reachedDistance")]
public float ReachedDistance = 1f; public float ReachedDistance = 1f;
/// <summary>
/// Pinpointer arrow precision in radians.
/// </summary>
[DataField("precision")]
public double Precision = 0.09;
public EntityUid? Target = null; public EntityUid? Target = null;
public bool IsActive = false; public bool IsActive = false;
public Direction DirectionToTarget = Direction.Invalid; public Angle ArrowAngle;
public Distance DistanceToTarget = Distance.UNKNOWN; public Distance DistanceToTarget = Distance.Unknown;
public bool HasTarget => DistanceToTarget != Distance.Unknown;
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]
public sealed class PinpointerComponentState : ComponentState public sealed class PinpointerComponentState : ComponentState
{ {
public bool IsActive; public bool IsActive;
public Direction DirectionToTarget; public Angle ArrowAngle;
public Distance DistanceToTarget; public Distance DistanceToTarget;
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]
public enum Distance : byte public enum Distance : byte
{ {
UNKNOWN, Unknown,
REACHED, Reached,
CLOSE, Close,
MEDIUM, Medium,
FAR Far
} }
} }

View File

@@ -6,7 +6,7 @@ namespace Content.Shared.Pinpointer
public enum PinpointerVisuals : byte public enum PinpointerVisuals : byte
{ {
IsActive, IsActive,
TargetDirection, ArrowAngle,
TargetDistance TargetDistance
} }

View File

@@ -4,13 +4,10 @@ namespace Content.Shared.Pinpointer
{ {
public abstract class SharedPinpointerSystem : EntitySystem public abstract class SharedPinpointerSystem : EntitySystem
{ {
protected readonly HashSet<EntityUid> ActivePinpointers = new();
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<PinpointerComponent, ComponentGetState>(GetCompState); SubscribeLocalEvent<PinpointerComponent, ComponentGetState>(GetCompState);
SubscribeLocalEvent<PinpointerComponent, ComponentShutdown>(OnPinpointerShutdown);
} }
private void GetCompState(EntityUid uid, PinpointerComponent pinpointer, ref ComponentGetState args) private void GetCompState(EntityUid uid, PinpointerComponent pinpointer, ref ComponentGetState args)
@@ -18,17 +15,11 @@ namespace Content.Shared.Pinpointer
args.State = new PinpointerComponentState args.State = new PinpointerComponentState
{ {
IsActive = pinpointer.IsActive, IsActive = pinpointer.IsActive,
DirectionToTarget = pinpointer.DirectionToTarget, ArrowAngle = pinpointer.ArrowAngle,
DistanceToTarget = pinpointer.DistanceToTarget DistanceToTarget = pinpointer.DistanceToTarget
}; };
} }
private void OnPinpointerShutdown(EntityUid uid, PinpointerComponent component, ComponentShutdown _)
{
// no need to dirty it/etc: it's shutting down anyway!
ActivePinpointers.Remove(uid);
}
/// <summary> /// <summary>
/// Manually set distance from pinpointer to target /// Manually set distance from pinpointer to target
/// </summary> /// </summary>
@@ -45,18 +36,22 @@ namespace Content.Shared.Pinpointer
} }
/// <summary> /// <summary>
/// Manually set pinpointer arrow direction /// Try to manually set pinpointer arrow direction.
/// If difference between current angle and new angle is smaller than
/// pinpointer precision, new value will be ignored and it will return false.
/// </summary> /// </summary>
public void SetDirection(EntityUid uid, Direction directionToTarget, PinpointerComponent? pinpointer = null) public bool TrySetArrowAngle(EntityUid uid, Angle arrowAngle, PinpointerComponent? pinpointer = null)
{ {
if (!Resolve(uid, ref pinpointer)) if (!Resolve(uid, ref pinpointer))
return; return false;
if (directionToTarget == pinpointer.DirectionToTarget) if (pinpointer.ArrowAngle.EqualsApprox(arrowAngle, pinpointer.Precision))
return; return false;
pinpointer.DirectionToTarget = directionToTarget; pinpointer.ArrowAngle = arrowAngle;
Dirty(pinpointer); Dirty(pinpointer);
return true;
} }
/// <summary> /// <summary>
@@ -68,13 +63,7 @@ namespace Content.Shared.Pinpointer
return; return;
if (isActive == pinpointer.IsActive) if (isActive == pinpointer.IsActive)
return; return;
// add-remove pinpointer from update list
if (isActive)
ActivePinpointers.Add(uid);
else
ActivePinpointers.Remove(uid);
pinpointer.IsActive = isActive; pinpointer.IsActive = isActive;
Dirty(pinpointer); Dirty(pinpointer);
} }

View File

@@ -9,6 +9,7 @@
noRot: True noRot: True
- type: Sprite - type: Sprite
netsync: false netsync: false
noRot: True
sprite: Objects/Devices/pinpointer.rsi sprite: Objects/Devices/pinpointer.rsi
layers: layers:
- state: pinpointer - state: pinpointer
@@ -19,8 +20,6 @@
sprite: Objects/Devices/pinpointer.rsi sprite: Objects/Devices/pinpointer.rsi
- type: Pinpointer - type: Pinpointer
- type: Appearance - type: Appearance
visuals:
- type: PinpointerVisualizer
- type: entity - type: entity
name: pinpointer name: pinpointer

View File

@@ -560,6 +560,8 @@ public sealed class $CLASS$ : Shared$CLASS$ {
<s:Boolean x:Key="/Default/UserDictionary/Words/=Patreon/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Patreon/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pdas/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=pdas/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Phoron/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Phoron/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pinpointer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pinpointers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pipenet/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=pipenet/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pipenode/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=pipenode/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=placeable/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=placeable/@EntryIndexedValue">True</s:Boolean>