1
0

JointVisualsOverlay.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Numerics;
  2. using Content.Shared.Physics;
  3. using Robust.Client.GameObjects;
  4. using Robust.Client.Graphics;
  5. using Robust.Shared.Enums;
  6. namespace Content.Client.Physics;
  7. /// <summary>
  8. /// Draws a texture on top of a joint.
  9. /// </summary>
  10. public sealed class JointVisualsOverlay : Overlay
  11. {
  12. public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowFOV;
  13. private IEntityManager _entManager;
  14. public JointVisualsOverlay(IEntityManager entManager)
  15. {
  16. _entManager = entManager;
  17. }
  18. protected override void Draw(in OverlayDrawArgs args)
  19. {
  20. var worldHandle = args.WorldHandle;
  21. var spriteSystem = _entManager.System<SpriteSystem>();
  22. var xformSystem = _entManager.System<SharedTransformSystem>();
  23. var joints = _entManager.EntityQueryEnumerator<JointVisualsComponent, TransformComponent>();
  24. var xformQuery = _entManager.GetEntityQuery<TransformComponent>();
  25. args.DrawingHandle.SetTransform(Matrix3x2.Identity);
  26. while (joints.MoveNext(out var visuals, out var xform))
  27. {
  28. if (xform.MapID != args.MapId)
  29. continue;
  30. var other = _entManager.GetEntity(visuals.Target);
  31. if (!xformQuery.TryGetComponent(other, out var otherXform))
  32. continue;
  33. if (xform.MapID != otherXform.MapID)
  34. continue;
  35. var texture = spriteSystem.Frame0(visuals.Sprite);
  36. var width = texture.Width / (float) EyeManager.PixelsPerMeter;
  37. var coordsA = xform.Coordinates;
  38. var coordsB = otherXform.Coordinates;
  39. var rotA = xform.LocalRotation;
  40. var rotB = otherXform.LocalRotation;
  41. coordsA = coordsA.Offset(rotA.RotateVec(visuals.OffsetA));
  42. coordsB = coordsB.Offset(rotB.RotateVec(visuals.OffsetB));
  43. var posA = xformSystem.ToMapCoordinates(coordsA).Position;
  44. var posB = xformSystem.ToMapCoordinates(coordsB).Position;
  45. var diff = (posB - posA);
  46. var length = diff.Length();
  47. var midPoint = diff / 2f + posA;
  48. var angle = (posB - posA).ToWorldAngle();
  49. var box = new Box2(-width / 2f, -length / 2f, width / 2f, length / 2f);
  50. var rotate = new Box2Rotated(box.Translated(midPoint), angle, midPoint);
  51. worldHandle.DrawTextureRect(texture, rotate);
  52. }
  53. }
  54. }