1
0

EntityPickupAnimationSystem.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Numerics;
  2. using Robust.Client.Animations;
  3. using Robust.Client.GameObjects;
  4. using Robust.Shared.Animations;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Spawners;
  7. using static Robust.Client.Animations.AnimationTrackProperty;
  8. namespace Content.Client.Animations;
  9. /// <summary>
  10. /// System that handles animating an entity that a player has picked up.
  11. /// </summary>
  12. public sealed class EntityPickupAnimationSystem : EntitySystem
  13. {
  14. [Dependency] private readonly AnimationPlayerSystem _animations = default!;
  15. [Dependency] private readonly MetaDataSystem _metaData = default!;
  16. [Dependency] private readonly TransformSystem _transform = default!;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<EntityPickupAnimationComponent, AnimationCompletedEvent>(OnEntityPickupAnimationCompleted);
  21. }
  22. private void OnEntityPickupAnimationCompleted(EntityUid uid, EntityPickupAnimationComponent component, AnimationCompletedEvent args)
  23. {
  24. Del(uid);
  25. }
  26. /// <summary>
  27. /// Animates a clone of an entity moving from one point to another before
  28. /// being deleted.
  29. /// Used when the player picks up an entity.
  30. /// </summary>
  31. public void AnimateEntityPickup(EntityUid uid, EntityCoordinates initial, Vector2 final, Angle initialAngle)
  32. {
  33. if (Deleted(uid) || !initial.IsValid(EntityManager))
  34. return;
  35. var metadata = MetaData(uid);
  36. if (IsPaused(uid, metadata))
  37. return;
  38. var animatableClone = Spawn("clientsideclone", initial);
  39. EnsureComp<EntityPickupAnimationComponent>(animatableClone);
  40. var val = metadata.EntityName;
  41. _metaData.SetEntityName(animatableClone, val);
  42. if (!TryComp(uid, out SpriteComponent? sprite0))
  43. {
  44. Log.Error("Entity ({0}) couldn't be animated for pickup since it doesn't have a {1}!", metadata.EntityName, nameof(SpriteComponent));
  45. return;
  46. }
  47. var sprite = Comp<SpriteComponent>(animatableClone);
  48. sprite.CopyFrom(sprite0);
  49. sprite.Visible = true;
  50. var animations = Comp<AnimationPlayerComponent>(animatableClone);
  51. var despawn = EnsureComp<TimedDespawnComponent>(animatableClone);
  52. despawn.Lifetime = 0.25f;
  53. _transform.SetLocalRotationNoLerp(animatableClone, initialAngle);
  54. _animations.Play(new Entity<AnimationPlayerComponent>(animatableClone, animations), new Animation
  55. {
  56. Length = TimeSpan.FromMilliseconds(125),
  57. AnimationTracks =
  58. {
  59. new AnimationTrackComponentProperty
  60. {
  61. ComponentType = typeof(TransformComponent),
  62. Property = nameof(TransformComponent.LocalPosition),
  63. InterpolationMode = AnimationInterpolationMode.Linear,
  64. KeyFrames =
  65. {
  66. new KeyFrame(initial.Position, 0),
  67. new KeyFrame(final, 0.125f)
  68. }
  69. },
  70. }
  71. }, "fancy_pickup_anim");
  72. }
  73. }