using System.Numerics;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;
using Robust.Shared.Map;
using Robust.Shared.Spawners;
using static Robust.Client.Animations.AnimationTrackProperty;
namespace Content.Client.Animations;
///
/// System that handles animating an entity that a player has picked up.
///
public sealed class EntityPickupAnimationSystem : EntitySystem
{
[Dependency] private readonly AnimationPlayerSystem _animations = default!;
[Dependency] private readonly MetaDataSystem _metaData = default!;
[Dependency] private readonly TransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnEntityPickupAnimationCompleted);
}
private void OnEntityPickupAnimationCompleted(EntityUid uid, EntityPickupAnimationComponent component, AnimationCompletedEvent args)
{
Del(uid);
}
///
/// Animates a clone of an entity moving from one point to another before
/// being deleted.
/// Used when the player picks up an entity.
///
public void AnimateEntityPickup(EntityUid uid, EntityCoordinates initial, Vector2 final, Angle initialAngle)
{
if (Deleted(uid) || !initial.IsValid(EntityManager))
return;
var metadata = MetaData(uid);
if (IsPaused(uid, metadata))
return;
var animatableClone = Spawn("clientsideclone", initial);
EnsureComp(animatableClone);
var val = metadata.EntityName;
_metaData.SetEntityName(animatableClone, val);
if (!TryComp(uid, out SpriteComponent? sprite0))
{
Log.Error("Entity ({0}) couldn't be animated for pickup since it doesn't have a {1}!", metadata.EntityName, nameof(SpriteComponent));
return;
}
var sprite = Comp(animatableClone);
sprite.CopyFrom(sprite0);
sprite.Visible = true;
var animations = Comp(animatableClone);
var despawn = EnsureComp(animatableClone);
despawn.Lifetime = 0.25f;
_transform.SetLocalRotationNoLerp(animatableClone, initialAngle);
_animations.Play(new Entity(animatableClone, animations), new Animation
{
Length = TimeSpan.FromMilliseconds(125),
AnimationTracks =
{
new AnimationTrackComponentProperty
{
ComponentType = typeof(TransformComponent),
Property = nameof(TransformComponent.LocalPosition),
InterpolationMode = AnimationInterpolationMode.Linear,
KeyFrames =
{
new KeyFrame(initial.Position, 0),
new KeyFrame(final, 0.125f)
}
},
}
}, "fancy_pickup_anim");
}
}