1
0

MouseRotatorSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Content.Shared.MouseRotator;
  2. using Robust.Client.Graphics;
  3. using Robust.Client.Input;
  4. using Robust.Client.Player;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Timing;
  7. namespace Content.Client.MouseRotator;
  8. /// <inheritdoc/>
  9. public sealed class MouseRotatorSystem : SharedMouseRotatorSystem
  10. {
  11. [Dependency] private readonly IInputManager _input = default!;
  12. [Dependency] private readonly IPlayerManager _player = default!;
  13. [Dependency] private readonly IGameTiming _timing = default!;
  14. [Dependency] private readonly IEyeManager _eye = default!;
  15. [Dependency] private readonly SharedTransformSystem _transform = default!;
  16. public override void Update(float frameTime)
  17. {
  18. base.Update(frameTime);
  19. if (!_timing.IsFirstTimePredicted || !_input.MouseScreenPosition.IsValid)
  20. return;
  21. var player = _player.LocalEntity;
  22. if (player == null || !TryComp<MouseRotatorComponent>(player, out var rotator))
  23. return;
  24. var xform = Transform(player.Value);
  25. // Get mouse loc and convert to angle based on player location
  26. var coords = _input.MouseScreenPosition;
  27. var mapPos = _eye.PixelToMap(coords);
  28. if (mapPos.MapId == MapId.Nullspace)
  29. return;
  30. var angle = (mapPos.Position - _transform.GetMapCoordinates(player.Value, xform: xform).Position).ToWorldAngle();
  31. var curRot = _transform.GetWorldRotation(xform);
  32. // 4-dir handling is separate --
  33. // only raise event if the cardinal direction has changed
  34. if (rotator.Simple4DirMode)
  35. {
  36. var eyeRot = _eye.CurrentEye.Rotation; // camera rotation
  37. var angleDir = (angle + eyeRot).GetCardinalDir(); // apply GetCardinalDir in the camera frame, not in the world frame
  38. if (angleDir == (curRot + eyeRot).GetCardinalDir())
  39. return;
  40. var rotation = angleDir.ToAngle() - eyeRot; // convert back to world frame
  41. if (rotation >= Math.PI) // convert to [-PI, +PI)
  42. rotation -= 2 * Math.PI;
  43. else if (rotation < -Math.PI)
  44. rotation += 2 * Math.PI;
  45. RaisePredictiveEvent(new RequestMouseRotatorRotationEvent
  46. {
  47. Rotation = rotation
  48. });
  49. return;
  50. }
  51. // Don't raise event if mouse ~hasn't moved (or if too close to goal rotation already)
  52. var diff = Angle.ShortestDistance(angle, curRot);
  53. if (Math.Abs(diff.Theta) < rotator.AngleTolerance.Theta)
  54. return;
  55. if (rotator.GoalRotation != null)
  56. {
  57. var goalDiff = Angle.ShortestDistance(angle, rotator.GoalRotation.Value);
  58. if (Math.Abs(goalDiff.Theta) < rotator.AngleTolerance.Theta)
  59. return;
  60. }
  61. RaisePredictiveEvent(new RequestMouseRotatorRotationEvent
  62. {
  63. Rotation = angle
  64. });
  65. }
  66. }