Files
tbd-station-14/Content.Client/Throwing/ThrownItemVisualizerSystem.cs
Tayrtahn 818d047449 Cleanup more SpriteComponent warnings (part 5) (#37590)
* 1 warning in KudzuVisualizerSystem

* 2 warnings in ChameleonProjectorSystem

* 1 warning in MarkerSystem

* 2 warnings in ItemSystem

* 1 warning in GhostToggleSelfVisibility

* 1 warning in FoamVisualizerSystem

* 1 warning in ClickableTest

* 1 warning in ThrownItemVisualizerSystem

* 2 warnings in InfantSystem

* 1 warning in ChasmFallingVisualsSystem

* 1 warning in PotencyVisualsSystem

* 2 warnings in OrbitVisualsSystem

* 2 warnings in BeamSystem

* 1 warning in JitteringSystem

* 1 warning in CardboardBoxSystem

* 2 warnings in StationAiSystem

* 2 warnings in FirelockSystem

* 2 warnings in CargoSystem.Telepad

* 1 warning in StasisBedSystem

* 2 warnings in WeldableVisualizerSystem

* 2 warnings in DeliveryVisualizerSystem

* 1 warning in TimerTriggerVisualizerSystem

* 1 warning in StorageFillVisualizerSystem

* 2 warnings in RadiationCollectorSystem

* 2 warnings in BorgSwitchableTypeSystem

* 1 warning in TurnstileSystem

* 1 warning in SurveillanceCameraVisualsSystem

* 1 warning in BurnStateVisualizerSystem

* 2 warnings in CableVisualizerSystem

* 1 warning in JetpackSystem
2025-05-19 03:09:47 +02:00

88 lines
2.9 KiB
C#

using Content.Shared.Throwing;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;
namespace Content.Client.Throwing;
/// <summary>
/// Handles animating thrown items.
/// </summary>
public sealed class ThrownItemVisualizerSystem : EntitySystem
{
[Dependency] private readonly AnimationPlayerSystem _anim = default!;
[Dependency] private readonly SpriteSystem _sprite = default!;
private const string AnimationKey = "thrown-item";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ThrownItemComponent, AfterAutoHandleStateEvent>(OnAutoHandleState);
SubscribeLocalEvent<ThrownItemComponent, ComponentShutdown>(OnShutdown);
}
private void OnAutoHandleState(EntityUid uid, ThrownItemComponent component, ref AfterAutoHandleStateEvent args)
{
if (!TryComp<SpriteComponent>(uid, out var sprite) || !component.Animate)
return;
var animationPlayer = EnsureComp<AnimationPlayerComponent>(uid);
if (_anim.HasRunningAnimation(uid, animationPlayer, AnimationKey))
return;
var anim = GetAnimation((uid, component, sprite));
if (anim == null)
return;
component.OriginalScale = sprite.Scale;
_anim.Play((uid, animationPlayer), anim, AnimationKey);
}
private void OnShutdown(EntityUid uid, ThrownItemComponent component, ComponentShutdown args)
{
if (!_anim.HasRunningAnimation(uid, AnimationKey))
return;
if (TryComp<SpriteComponent>(uid, out var sprite) && component.OriginalScale != null)
_sprite.SetScale((uid, sprite), component.OriginalScale.Value);
_anim.Stop(uid, AnimationKey);
}
private static Animation? GetAnimation(Entity<ThrownItemComponent, SpriteComponent> ent)
{
if (ent.Comp1.LandTime - ent.Comp1.ThrownTime is not { } length)
return null;
if (length <= TimeSpan.Zero)
return null;
var scale = ent.Comp2.Scale;
var lenFloat = (float)length.TotalSeconds;
// TODO use like actual easings here
return new Animation
{
Length = length,
AnimationTracks =
{
new AnimationTrackComponentProperty
{
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Scale),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(scale, 0.0f),
new AnimationTrackProperty.KeyFrame(scale * 1.4f, lenFloat * 0.25f),
new AnimationTrackProperty.KeyFrame(scale, lenFloat * 0.75f)
},
InterpolationMode = AnimationInterpolationMode.Linear
}
}
};
}
}