1
0

FloatingVisualizerSystem.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Numerics;
  2. using Content.Shared.Gravity;
  3. using Robust.Client.GameObjects;
  4. using Robust.Client.Animations;
  5. using Robust.Shared.Animations;
  6. namespace Content.Client.Gravity;
  7. /// <inheritdoc/>
  8. public sealed class FloatingVisualizerSystem : SharedFloatingVisualizerSystem
  9. {
  10. [Dependency] private readonly AnimationPlayerSystem AnimationSystem = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<FloatingVisualsComponent, AnimationCompletedEvent>(OnAnimationCompleted);
  15. }
  16. /// <inheritdoc/>
  17. public override void FloatAnimation(EntityUid uid, Vector2 offset, string animationKey, float animationTime, bool stop = false)
  18. {
  19. if (stop)
  20. {
  21. AnimationSystem.Stop(uid, animationKey);
  22. return;
  23. }
  24. var animation = new Animation
  25. {
  26. // We multiply by the number of extra keyframes to make time for them
  27. Length = TimeSpan.FromSeconds(animationTime*2),
  28. AnimationTracks =
  29. {
  30. new AnimationTrackComponentProperty
  31. {
  32. ComponentType = typeof(SpriteComponent),
  33. Property = nameof(SpriteComponent.Offset),
  34. InterpolationMode = AnimationInterpolationMode.Linear,
  35. KeyFrames =
  36. {
  37. new AnimationTrackProperty.KeyFrame(Vector2.Zero, 0f),
  38. new AnimationTrackProperty.KeyFrame(offset, animationTime),
  39. new AnimationTrackProperty.KeyFrame(Vector2.Zero, animationTime),
  40. }
  41. }
  42. }
  43. };
  44. if (!AnimationSystem.HasRunningAnimation(uid, animationKey))
  45. AnimationSystem.Play(uid, animation, animationKey);
  46. }
  47. private void OnAnimationCompleted(EntityUid uid, FloatingVisualsComponent component, AnimationCompletedEvent args)
  48. {
  49. if (args.Key != component.AnimationKey)
  50. return;
  51. FloatAnimation(uid, component.Offset, component.AnimationKey, component.AnimationTime, stop: !component.CanFloat);
  52. }
  53. }