RadiationDebugOverlay.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Client.Radiation.Systems;
  4. using Robust.Client.Graphics;
  5. using Robust.Client.ResourceManagement;
  6. using Robust.Shared.Enums;
  7. using Robust.Shared.Map.Components;
  8. namespace Content.Client.Radiation.Overlays;
  9. public sealed class RadiationDebugOverlay : Overlay
  10. {
  11. [Dependency] private readonly IEntityManager _entityManager = default!;
  12. private readonly SharedMapSystem _mapSystem;
  13. private readonly RadiationSystem _radiation;
  14. private readonly Font _font;
  15. public override OverlaySpace Space => OverlaySpace.WorldSpace | OverlaySpace.ScreenSpace;
  16. public RadiationDebugOverlay()
  17. {
  18. IoCManager.InjectDependencies(this);
  19. _radiation = _entityManager.System<RadiationSystem>();
  20. _mapSystem = _entityManager.System<SharedMapSystem>();
  21. var cache = IoCManager.Resolve<IResourceCache>();
  22. _font = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Regular.ttf"), 8);
  23. }
  24. protected override void Draw(in OverlayDrawArgs args)
  25. {
  26. switch (args.Space)
  27. {
  28. case OverlaySpace.ScreenSpace:
  29. DrawScreenRays(args);
  30. DrawScreenResistance(args);
  31. break;
  32. case OverlaySpace.WorldSpace:
  33. DrawWorld(args);
  34. break;
  35. }
  36. }
  37. private void DrawScreenRays(OverlayDrawArgs args)
  38. {
  39. var rays = _radiation.Rays;
  40. if (rays == null || args.ViewportControl == null)
  41. return;
  42. var handle = args.ScreenHandle;
  43. foreach (var ray in rays)
  44. {
  45. if (ray.MapId != args.MapId)
  46. continue;
  47. if (ray.ReachedDestination)
  48. {
  49. var screenCenter = args.ViewportControl.WorldToScreen(ray.Destination);
  50. handle.DrawString(_font, screenCenter, ray.Rads.ToString("F2"), 2f, Color.White);
  51. }
  52. foreach (var (netGrid, blockers) in ray.Blockers)
  53. {
  54. var gridUid = _entityManager.GetEntity(netGrid);
  55. if (!_entityManager.TryGetComponent<MapGridComponent>(gridUid, out var grid))
  56. continue;
  57. foreach (var (tile, rads) in blockers)
  58. {
  59. var worldPos = _mapSystem.GridTileToWorldPos(gridUid, grid, tile);
  60. var screenCenter = args.ViewportControl.WorldToScreen(worldPos);
  61. handle.DrawString(_font, screenCenter, rads.ToString("F2"), 1.5f, Color.White);
  62. }
  63. }
  64. }
  65. }
  66. private void DrawScreenResistance(OverlayDrawArgs args)
  67. {
  68. var resistance = _radiation.ResistanceGrids;
  69. if (resistance == null || args.ViewportControl == null)
  70. return;
  71. var handle = args.ScreenHandle;
  72. var query = _entityManager.GetEntityQuery<TransformComponent>();
  73. foreach (var (netGrid, resMap) in resistance)
  74. {
  75. var gridUid = _entityManager.GetEntity(netGrid);
  76. if (!_entityManager.TryGetComponent<MapGridComponent>(gridUid, out var grid))
  77. continue;
  78. if (query.TryGetComponent(gridUid, out var trs) && trs.MapID != args.MapId)
  79. continue;
  80. var offset = new Vector2(grid.TileSize, -grid.TileSize) * 0.25f;
  81. foreach (var (tile, value) in resMap)
  82. {
  83. var localPos = _mapSystem.GridTileToLocal(gridUid, grid, tile).Position + offset;
  84. var worldPos = _mapSystem.LocalToWorld(gridUid, grid, localPos);
  85. var screenCenter = args.ViewportControl.WorldToScreen(worldPos);
  86. handle.DrawString(_font, screenCenter, value.ToString("F2"), color: Color.White);
  87. }
  88. }
  89. }
  90. private void DrawWorld(in OverlayDrawArgs args)
  91. {
  92. var rays = _radiation.Rays;
  93. if (rays == null)
  94. return;
  95. var handle = args.WorldHandle;
  96. // draw lines for raycasts
  97. foreach (var ray in rays)
  98. {
  99. if (ray.MapId != args.MapId)
  100. continue;
  101. if (ray.ReachedDestination)
  102. {
  103. handle.DrawLine(ray.Source, ray.Destination, Color.Red);
  104. continue;
  105. }
  106. foreach (var (netGrid, blockers) in ray.Blockers)
  107. {
  108. var gridUid = _entityManager.GetEntity(netGrid);
  109. if (!_entityManager.TryGetComponent<MapGridComponent>(gridUid, out var grid))
  110. continue;
  111. var (destTile, _) = blockers.Last();
  112. var destWorld = _mapSystem.GridTileToWorldPos(gridUid, grid, destTile);
  113. handle.DrawLine(ray.Source, destWorld, Color.Red);
  114. }
  115. }
  116. }
  117. }