1
0

LookZoomSystem.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #nullable enable
  2. using Content.Shared.Camera;
  3. using Content.Shared.Input;
  4. using Robust.Shared.Input.Binding;
  5. using Content.Client.Movement.Components;
  6. using Content.Client.Movement.Systems;
  7. using Robust.Shared.Player;
  8. using Content.Shared.Hands.EntitySystems;
  9. using Robust.Shared.Timing;
  10. using Robust.Client.Player;
  11. using Robust.Shared.GameObjects;
  12. using System.Numerics;
  13. using Robust.Client.Timing;
  14. namespace Content.Client.Civ14.LookZoom;
  15. public sealed class LookZoomSystem : EntitySystem
  16. {
  17. [Dependency] private readonly EyeCursorOffsetSystem _eyeOffset = default!;
  18. [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
  19. [Dependency] private readonly IGameTiming _timing = default!;
  20. [Dependency] private readonly IPlayerManager _player = default!;
  21. [Dependency] private readonly IClientGameTiming _gameTiming = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<LookZoomComponent, GetEyeOffsetRelayedEvent>(UpdateLookZoom);
  26. CommandBinds.Builder
  27. .Bind(ContentKeyFunctions.LookZoom, InputCmdHandler.FromDelegate(OnLookZoomHandler, handle: false, outsidePrediction: false))
  28. .Register<LookZoomSystem>();
  29. }
  30. public void OnLookZoomHandler(ICommonSession? session)
  31. {
  32. // Gets the player character uid
  33. var uid = session?.AttachedEntity;
  34. if (!TryComp<LookZoomComponent>(uid, out var comp))
  35. return;
  36. // Checks if the cooldown is over by comparing to the current time
  37. if (_timing.CurTime < comp.DelayedTime)
  38. return;
  39. // Sets the cooldown to the current time + 0.1 seconds
  40. comp.DelayedTime = _timing.CurTime + TimeSpan.FromSeconds(0.1);
  41. if (comp.State == false)
  42. {
  43. _handsSystem.TryGetActiveItem(uid.Value, out var item);
  44. ResetOffset(uid);
  45. ResetOffset(item);
  46. comp.State = true;
  47. return;
  48. }
  49. comp.State = false;
  50. }
  51. public void UpdateLookZoom(EntityUid uid, LookZoomComponent comp, ref GetEyeOffsetRelayedEvent args)
  52. {
  53. if (comp.State == false)
  54. {
  55. return;
  56. }
  57. _handsSystem.TryGetActiveItem(comp.Owner, out var item);
  58. // Sets the offset if there is an item in the active hand with the EyeCurserOffset component
  59. if (item != null && TryComp<EyeCursorOffsetComponent>(item, out var itemComp))
  60. {
  61. SetOffset(item.Value, args);
  62. return;
  63. }
  64. //Sets the offset using the EyeCurserOffset on the player entity instead
  65. SetOffset(comp.Owner, args);
  66. }
  67. private void SetOffset(EntityUid uid, GetEyeOffsetRelayedEvent args)
  68. {
  69. var offset = _eyeOffset.OffsetAfterMouse(uid, null);
  70. if (offset == null)
  71. return;
  72. args.Offset += offset.Value;
  73. }
  74. private void ResetOffset(EntityUid? uid)
  75. {
  76. if (TryComp(uid, out EyeCursorOffsetComponent? cursorOffsetComp))
  77. {
  78. if (_gameTiming.IsFirstTimePredicted)
  79. cursorOffsetComp.CurrentPosition = Vector2.Zero;
  80. }
  81. }
  82. }