CombatModeIndicatorsOverlay.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Numerics;
  2. using Content.Client.Hands.Systems;
  3. using Content.Shared.Weapons.Ranged.Components;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.Graphics;
  6. using Robust.Client.Input;
  7. using Robust.Client.Serialization;
  8. using Robust.Client.UserInterface;
  9. using Robust.Shared.Enums;
  10. using Robust.Shared.Graphics;
  11. using Robust.Shared.Utility;
  12. namespace Content.Client.CombatMode;
  13. /// <summary>
  14. /// This shows something like crosshairs for the combat mode next to the mouse cursor.
  15. /// For weapons with the gun class, a crosshair of one type is displayed,
  16. /// while for all other types of weapons and items in hand, as well as for an empty hand,
  17. /// a crosshair of a different type is displayed. These crosshairs simply show the state of combat mode (on|off).
  18. /// </summary>
  19. public sealed class CombatModeIndicatorsOverlay : Overlay
  20. {
  21. private readonly IInputManager _inputManager;
  22. private readonly IEntityManager _entMan;
  23. private readonly IEyeManager _eye;
  24. private readonly CombatModeSystem _combat;
  25. private readonly HandsSystem _hands = default!;
  26. private readonly Texture _gunSight;
  27. private readonly Texture _gunBoltSight;
  28. private readonly Texture _meleeSight;
  29. public override OverlaySpace Space => OverlaySpace.ScreenSpace;
  30. public Color MainColor = Color.White.WithAlpha(0.3f);
  31. public Color StrokeColor = Color.Black.WithAlpha(0.5f);
  32. public float Scale = 0.6f; // 1 is a little big
  33. public CombatModeIndicatorsOverlay(IInputManager input, IEntityManager entMan,
  34. IEyeManager eye, CombatModeSystem combatSys, HandsSystem hands)
  35. {
  36. _inputManager = input;
  37. _entMan = entMan;
  38. _eye = eye;
  39. _combat = combatSys;
  40. _hands = hands;
  41. var spriteSys = _entMan.EntitySysManager.GetEntitySystem<SpriteSystem>();
  42. _gunSight = spriteSys.Frame0(new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Misc/crosshair_pointers.rsi"),
  43. "gun_sight"));
  44. _gunBoltSight = spriteSys.Frame0(new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Misc/crosshair_pointers.rsi"),
  45. "gun_bolt_sight"));
  46. _meleeSight = spriteSys.Frame0(new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Misc/crosshair_pointers.rsi"),
  47. "melee_sight"));
  48. }
  49. protected override bool BeforeDraw(in OverlayDrawArgs args)
  50. {
  51. if (!_combat.IsInCombatMode())
  52. return false;
  53. return base.BeforeDraw(in args);
  54. }
  55. protected override void Draw(in OverlayDrawArgs args)
  56. {
  57. var mouseScreenPosition = _inputManager.MouseScreenPosition;
  58. var mousePosMap = _eye.PixelToMap(mouseScreenPosition);
  59. if (mousePosMap.MapId != args.MapId)
  60. return;
  61. var handEntity = _hands.GetActiveHandEntity();
  62. var isHandGunItem = _entMan.HasComponent<GunComponent>(handEntity);
  63. var isGunBolted = true;
  64. if (_entMan.TryGetComponent(handEntity, out ChamberMagazineAmmoProviderComponent? chamber))
  65. isGunBolted = chamber.BoltClosed ?? true;
  66. var mousePos = mouseScreenPosition.Position;
  67. var uiScale = (args.ViewportControl as Control)?.UIScale ?? 1f;
  68. var limitedScale = uiScale > 1.25f ? 1.25f : uiScale;
  69. var sight = isHandGunItem ? (isGunBolted ? _gunSight : _gunBoltSight) : _meleeSight;
  70. DrawSight(sight, args.ScreenHandle, mousePos, limitedScale * Scale);
  71. }
  72. private void DrawSight(Texture sight, DrawingHandleScreen screen, Vector2 centerPos, float scale)
  73. {
  74. var sightSize = sight.Size * scale;
  75. var expandedSize = sightSize + new Vector2(7f, 7f);
  76. screen.DrawTextureRect(sight,
  77. UIBox2.FromDimensions(centerPos - sightSize * 0.5f, sightSize), StrokeColor);
  78. screen.DrawTextureRect(sight,
  79. UIBox2.FromDimensions(centerPos - expandedSize * 0.5f, expandedSize), MainColor);
  80. }
  81. }