Files
tbd-station-14/Content.Client/Orbit/OrbitVisualsSystem.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

114 lines
4.2 KiB
C#

using System.Numerics;
using Content.Shared.Follower.Components;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Client.Orbit;
public sealed class OrbitVisualsSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly AnimationPlayerSystem _animations = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SpriteSystem _sprite = default!;
private readonly string _orbitStopKey = "orbiting_stop";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<OrbitVisualsComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<OrbitVisualsComponent, ComponentRemove>(OnComponentRemove);
}
private void OnComponentInit(EntityUid uid, OrbitVisualsComponent component, ComponentInit args)
{
_robustRandom.SetSeed((int)_timing.CurTime.TotalMilliseconds);
component.OrbitDistance =
_robustRandom.NextFloat(0.75f * component.OrbitDistance, 1.25f * component.OrbitDistance);
component.OrbitLength = _robustRandom.NextFloat(0.5f * component.OrbitLength, 1.5f * component.OrbitLength);
if (TryComp<SpriteComponent>(uid, out var sprite))
{
sprite.EnableDirectionOverride = true;
sprite.DirectionOverride = Direction.South;
}
var animationPlayer = EnsureComp<AnimationPlayerComponent>(uid);
if (_animations.HasRunningAnimation(uid, animationPlayer, _orbitStopKey))
{
_animations.Stop((uid, animationPlayer), _orbitStopKey);
}
}
private void OnComponentRemove(EntityUid uid, OrbitVisualsComponent component, ComponentRemove args)
{
if (!TryComp<SpriteComponent>(uid, out var sprite))
return;
sprite.EnableDirectionOverride = false;
var animationPlayer = EnsureComp<AnimationPlayerComponent>(uid);
if (!_animations.HasRunningAnimation(uid, animationPlayer, _orbitStopKey))
{
_animations.Play((uid, animationPlayer), GetStopAnimation(component, sprite), _orbitStopKey);
}
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);
var query = EntityManager.EntityQueryEnumerator<OrbitVisualsComponent, SpriteComponent>();
while (query.MoveNext(out var uid, out var orbit, out var sprite))
{
var progress = (float)(_timing.CurTime.TotalSeconds / orbit.OrbitLength) % 1;
var angle = new Angle(Math.PI * 2 * progress);
var vec = angle.RotateVec(new Vector2(orbit.OrbitDistance, 0));
_sprite.SetRotation((uid, sprite), angle);
_sprite.SetOffset((uid, sprite), vec);
}
}
private Animation GetStopAnimation(OrbitVisualsComponent component, SpriteComponent sprite)
{
var length = component.OrbitStopLength;
return new Animation()
{
Length = TimeSpan.FromSeconds(length),
AnimationTracks =
{
new AnimationTrackComponentProperty()
{
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Offset),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(sprite.Offset, 0f),
new AnimationTrackProperty.KeyFrame(Vector2.Zero, length),
},
InterpolationMode = AnimationInterpolationMode.Linear
},
new AnimationTrackComponentProperty()
{
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Rotation),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(sprite.Rotation.Reduced(), 0f),
new AnimationTrackProperty.KeyFrame(Angle.Zero, length),
},
InterpolationMode = AnimationInterpolationMode.Linear
}
}
};
}
}