ChasmFallingVisualsSystem.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Content.Shared.Chasm;
  2. using Robust.Client.Animations;
  3. using Robust.Client.GameObjects;
  4. using Robust.Shared.Animations;
  5. namespace Content.Client.Chasm;
  6. /// <summary>
  7. /// Handles the falling animation for entities that fall into a chasm.
  8. /// </summary>
  9. public sealed class ChasmFallingVisualsSystem : EntitySystem
  10. {
  11. [Dependency] private readonly AnimationPlayerSystem _anim = default!;
  12. private readonly string _chasmFallAnimationKey = "chasm_fall";
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<ChasmFallingComponent, ComponentInit>(OnComponentInit);
  17. SubscribeLocalEvent<ChasmFallingComponent, ComponentRemove>(OnComponentRemove);
  18. }
  19. private void OnComponentInit(EntityUid uid, ChasmFallingComponent component, ComponentInit args)
  20. {
  21. if (!TryComp<SpriteComponent>(uid, out var sprite) ||
  22. TerminatingOrDeleted(uid))
  23. {
  24. return;
  25. }
  26. component.OriginalScale = sprite.Scale;
  27. var player = EnsureComp<AnimationPlayerComponent>(uid);
  28. if (_anim.HasRunningAnimation(player, _chasmFallAnimationKey))
  29. return;
  30. _anim.Play((uid, player), GetFallingAnimation(component), _chasmFallAnimationKey);
  31. }
  32. private void OnComponentRemove(EntityUid uid, ChasmFallingComponent component, ComponentRemove args)
  33. {
  34. if (!TryComp<SpriteComponent>(uid, out var sprite))
  35. return;
  36. var player = EnsureComp<AnimationPlayerComponent>(uid);
  37. if (_anim.HasRunningAnimation(player, _chasmFallAnimationKey))
  38. _anim.Stop(player, _chasmFallAnimationKey);
  39. sprite.Scale = component.OriginalScale;
  40. }
  41. private Animation GetFallingAnimation(ChasmFallingComponent component)
  42. {
  43. var length = component.AnimationTime;
  44. return new Animation()
  45. {
  46. Length = length,
  47. AnimationTracks =
  48. {
  49. new AnimationTrackComponentProperty()
  50. {
  51. ComponentType = typeof(SpriteComponent),
  52. Property = nameof(SpriteComponent.Scale),
  53. KeyFrames =
  54. {
  55. new AnimationTrackProperty.KeyFrame(component.OriginalScale, 0.0f),
  56. new AnimationTrackProperty.KeyFrame(component.AnimationScale, length.Seconds),
  57. },
  58. InterpolationMode = AnimationInterpolationMode.Cubic
  59. }
  60. }
  61. };
  62. }
  63. }