2023-07-08 14:08:32 +10:00
using System.Numerics ;
2023-07-18 21:44:00 +10:00
using Content.Shared.Spawners.Components ;
2021-02-03 22:07:13 +00:00
using Robust.Client.Animations ;
using Robust.Client.GameObjects ;
using Robust.Shared.Animations ;
using Robust.Shared.Map ;
namespace Content.Client.Animations
{
public static class ReusableAnimations
{
2023-09-12 22:34:04 +10:00
public static void AnimateEntityPickup ( EntityUid entity , EntityCoordinates initialCoords , Vector2 finalPosition , Angle initialAngle , IEntityManager ? entMan = null )
2021-02-03 22:07:13 +00:00
{
2021-12-08 11:37:10 +01:00
IoCManager . Resolve ( ref entMan ) ;
2021-12-11 15:29:16 +01:00
2023-09-12 22:34:04 +10:00
if ( entMan . Deleted ( entity ) | | ! initialCoords . IsValid ( entMan ) )
2023-08-20 15:50:23 +10:00
return ;
var metadata = entMan . GetComponent < MetaDataComponent > ( entity ) ;
if ( entMan . IsPaused ( entity , metadata ) )
2021-12-11 15:29:16 +01:00
return ;
2023-09-12 22:34:04 +10:00
var animatableClone = entMan . SpawnEntity ( "clientsideclone" , initialCoords ) ;
2021-12-08 11:37:10 +01:00
string val = entMan . GetComponent < MetaDataComponent > ( entity ) . EntityName ;
2023-08-28 11:20:31 +02:00
entMan . System < MetaDataSystem > ( ) . SetEntityName ( animatableClone , val ) ;
2021-02-03 22:07:13 +00:00
2021-12-08 11:37:10 +01:00
if ( ! entMan . TryGetComponent ( entity , out SpriteComponent ? sprite0 ) )
2021-02-03 22:07:13 +00:00
{
2021-12-08 11:37:10 +01:00
Logger . Error ( "Entity ({0}) couldn't be animated for pickup since it doesn't have a {1}!" , entMan . GetComponent < MetaDataComponent > ( entity ) . EntityName , nameof ( SpriteComponent ) ) ;
2021-02-03 22:07:13 +00:00
return ;
}
2021-12-08 11:37:10 +01:00
var sprite = entMan . GetComponent < SpriteComponent > ( animatableClone ) ;
2021-02-03 22:07:13 +00:00
sprite . CopyFrom ( sprite0 ) ;
2022-01-05 17:53:08 +13:00
sprite . Visible = true ;
2021-02-03 22:07:13 +00:00
2021-12-08 11:37:10 +01:00
var animations = entMan . GetComponent < AnimationPlayerComponent > ( animatableClone ) ;
2023-09-12 22:34:04 +10:00
animations . AnimationCompleted + = ( _ ) = >
{
2021-12-08 11:37:10 +01:00
entMan . DeleteEntity ( animatableClone ) ;
2021-02-03 22:07:13 +00:00
} ;
2023-07-18 21:44:00 +10:00
var despawn = entMan . EnsureComponent < TimedDespawnComponent > ( animatableClone ) ;
despawn . Lifetime = 0.25f ;
entMan . System < SharedTransformSystem > ( ) . SetLocalRotationNoLerp ( animatableClone , initialAngle ) ;
2021-02-03 22:07:13 +00:00
animations . Play ( new Animation
{
Length = TimeSpan . FromMilliseconds ( 125 ) ,
AnimationTracks =
{
new AnimationTrackComponentProperty
{
2021-11-08 12:37:32 +01:00
ComponentType = typeof ( TransformComponent ) ,
Property = nameof ( TransformComponent . LocalPosition ) ,
2021-02-03 22:07:13 +00:00
InterpolationMode = AnimationInterpolationMode . Linear ,
KeyFrames =
{
2023-09-12 22:34:04 +10:00
new AnimationTrackProperty . KeyFrame ( initialCoords . Position , 0 ) ,
2021-03-10 14:48:29 +01:00
new AnimationTrackProperty . KeyFrame ( finalPosition , 0.125f )
2021-02-03 22:07:13 +00:00
}
2023-07-18 21:44:00 +10:00
} ,
2021-02-03 22:07:13 +00:00
}
} , "fancy_pickup_anim" ) ;
}
}
}