PointingSystem.Visualizer.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Content.Client.Pointing.Components;
  2. using Content.Shared.Pointing;
  3. using Robust.Client.Animations;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.Graphics;
  6. using Robust.Shared.Animations;
  7. using System.Numerics;
  8. namespace Content.Client.Pointing;
  9. public sealed partial class PointingSystem : SharedPointingSystem
  10. {
  11. [Dependency] private readonly IEyeManager _eyeManager = default!;
  12. [Dependency] private readonly AnimationPlayerSystem _animationPlayer = default!;
  13. [Dependency] private readonly TransformSystem _transformSystem = default!;
  14. public void InitializeVisualizer()
  15. {
  16. SubscribeLocalEvent<PointingArrowComponent, AnimationCompletedEvent>(OnAnimationCompleted);
  17. }
  18. private void OnAnimationCompleted(EntityUid uid, PointingArrowComponent component, AnimationCompletedEvent args)
  19. {
  20. if (args.Key == component.AnimationKey)
  21. _animationPlayer.Stop(uid, component.AnimationKey);
  22. }
  23. private void BeginPointAnimation(EntityUid uid, Vector2 startPosition, Vector2 offset, string animationKey)
  24. {
  25. if (_animationPlayer.HasRunningAnimation(uid, animationKey))
  26. return;
  27. startPosition = new Angle(_eyeManager.CurrentEye.Rotation + _transformSystem.GetWorldRotation(uid)).RotateVec(startPosition);
  28. var animation = new Animation
  29. {
  30. Length = PointDuration,
  31. AnimationTracks =
  32. {
  33. new AnimationTrackComponentProperty
  34. {
  35. ComponentType = typeof(SpriteComponent),
  36. Property = nameof(SpriteComponent.Offset),
  37. InterpolationMode = AnimationInterpolationMode.Cubic,
  38. KeyFrames =
  39. {
  40. // We pad here to prevent improper looping and tighten the overshoot, just a touch
  41. new AnimationTrackProperty.KeyFrame(startPosition, 0f),
  42. new AnimationTrackProperty.KeyFrame(Vector2.Lerp(startPosition, offset, 0.9f), PointKeyTimeMove),
  43. new AnimationTrackProperty.KeyFrame(offset, PointKeyTimeMove),
  44. new AnimationTrackProperty.KeyFrame(Vector2.Zero, PointKeyTimeMove),
  45. new AnimationTrackProperty.KeyFrame(offset, PointKeyTimeHover),
  46. new AnimationTrackProperty.KeyFrame(Vector2.Zero, PointKeyTimeHover),
  47. new AnimationTrackProperty.KeyFrame(offset, PointKeyTimeHover),
  48. new AnimationTrackProperty.KeyFrame(Vector2.Zero, PointKeyTimeHover),
  49. new AnimationTrackProperty.KeyFrame(offset, PointKeyTimeHover),
  50. new AnimationTrackProperty.KeyFrame(Vector2.Zero, PointKeyTimeHover),
  51. new AnimationTrackProperty.KeyFrame(offset, PointKeyTimeHover),
  52. new AnimationTrackProperty.KeyFrame(Vector2.Zero, PointKeyTimeHover),
  53. }
  54. }
  55. }
  56. };
  57. _animationPlayer.Play(uid, animation, animationKey);
  58. }
  59. }