MiningOverlay.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Numerics;
  2. using Content.Shared.Mining.Components;
  3. using Robust.Client.GameObjects;
  4. using Robust.Client.Graphics;
  5. using Robust.Client.Player;
  6. using Robust.Shared.Enums;
  7. using Robust.Shared.Timing;
  8. using Robust.Shared.Utility;
  9. namespace Content.Client.Mining;
  10. public sealed class MiningOverlay : Overlay
  11. {
  12. [Dependency] private readonly IEntityManager _entityManager = default!;
  13. [Dependency] private readonly IGameTiming _timing = default!;
  14. [Dependency] private readonly IPlayerManager _player = default!;
  15. private readonly EntityLookupSystem _lookup;
  16. private readonly SpriteSystem _sprite;
  17. private readonly TransformSystem _xform;
  18. private readonly EntityQuery<SpriteComponent> _spriteQuery;
  19. private readonly EntityQuery<TransformComponent> _xformQuery;
  20. public override OverlaySpace Space => OverlaySpace.WorldSpace;
  21. public override bool RequestScreenTexture => false;
  22. private readonly HashSet<Entity<MiningScannerViewableComponent>> _viewableEnts = new();
  23. public MiningOverlay()
  24. {
  25. IoCManager.InjectDependencies(this);
  26. _lookup = _entityManager.System<EntityLookupSystem>();
  27. _sprite = _entityManager.System<SpriteSystem>();
  28. _xform = _entityManager.System<TransformSystem>();
  29. _spriteQuery = _entityManager.GetEntityQuery<SpriteComponent>();
  30. _xformQuery = _entityManager.GetEntityQuery<TransformComponent>();
  31. }
  32. protected override void Draw(in OverlayDrawArgs args)
  33. {
  34. var handle = args.WorldHandle;
  35. if (_player.LocalEntity is not { } localEntity ||
  36. !_entityManager.TryGetComponent<MiningScannerViewerComponent>(localEntity, out var viewerComp))
  37. return;
  38. if (viewerComp.LastPingLocation == null)
  39. return;
  40. var scaleMatrix = Matrix3Helpers.CreateScale(Vector2.One);
  41. _viewableEnts.Clear();
  42. _lookup.GetEntitiesInRange(viewerComp.LastPingLocation.Value, viewerComp.ViewRange, _viewableEnts);
  43. foreach (var ore in _viewableEnts)
  44. {
  45. if (!_xformQuery.TryComp(ore, out var xform) ||
  46. !_spriteQuery.TryComp(ore, out var sprite))
  47. continue;
  48. if (xform.MapID != args.MapId || !sprite.Visible)
  49. continue;
  50. if (!sprite.LayerMapTryGet(MiningScannerVisualLayers.Overlay, out var idx))
  51. continue;
  52. var layer = sprite[idx];
  53. if (layer.ActualRsi?.Path == null || layer.RsiState.Name == null)
  54. continue;
  55. var gridRot = xform.GridUid == null ? 0 : _xformQuery.CompOrNull(xform.GridUid.Value)?.LocalRotation ?? 0;
  56. var rotationMatrix = Matrix3Helpers.CreateRotation(gridRot);
  57. var worldMatrix = Matrix3Helpers.CreateTranslation(_xform.GetWorldPosition(xform));
  58. var scaledWorld = Matrix3x2.Multiply(scaleMatrix, worldMatrix);
  59. var matty = Matrix3x2.Multiply(rotationMatrix, scaledWorld);
  60. handle.SetTransform(matty);
  61. var spriteSpec = new SpriteSpecifier.Rsi(layer.ActualRsi.Path, layer.RsiState.Name);
  62. var texture = _sprite.GetFrame(spriteSpec, TimeSpan.FromSeconds(layer.AnimationTime));
  63. var animTime = (viewerComp.NextPingTime - _timing.CurTime).TotalSeconds;
  64. var alpha = animTime < viewerComp.AnimationDuration
  65. ? 0
  66. : (float) Math.Clamp((animTime - viewerComp.AnimationDuration) / viewerComp.AnimationDuration, 0f, 1f);
  67. var color = Color.White.WithAlpha(alpha);
  68. handle.DrawTexture(texture, -(Vector2) texture.Size / 2f / EyeManager.PixelsPerMeter, layer.Rotation, modulate: color);
  69. }
  70. handle.SetTransform(Matrix3x2.Identity);
  71. }
  72. }