* 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
107 lines
4.1 KiB
C#
107 lines
4.1 KiB
C#
using System.Numerics;
|
|
using Content.Shared.Jittering;
|
|
using Robust.Client.Animations;
|
|
using Robust.Client.GameObjects;
|
|
using Robust.Shared.Random;
|
|
|
|
namespace Content.Client.Jittering
|
|
{
|
|
public sealed class JitteringSystem : SharedJitteringSystem
|
|
{
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
[Dependency] private readonly AnimationPlayerSystem _animationPlayer = default!;
|
|
[Dependency] private readonly SpriteSystem _sprite = default!;
|
|
|
|
private readonly float[] _sign = { -1, 1 };
|
|
private readonly string _jitterAnimationKey = "jittering";
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<JitteringComponent, ComponentStartup>(OnStartup);
|
|
SubscribeLocalEvent<JitteringComponent, ComponentShutdown>(OnShutdown);
|
|
SubscribeLocalEvent<JitteringComponent, AnimationCompletedEvent>(OnAnimationCompleted);
|
|
}
|
|
|
|
private void OnStartup(EntityUid uid, JitteringComponent jittering, ComponentStartup args)
|
|
{
|
|
if (!TryComp(uid, out SpriteComponent? sprite))
|
|
return;
|
|
|
|
var animationPlayer = EnsureComp<AnimationPlayerComponent>(uid);
|
|
|
|
jittering.StartOffset = sprite.Offset;
|
|
_animationPlayer.Play((uid, animationPlayer), GetAnimation(jittering, sprite), _jitterAnimationKey);
|
|
}
|
|
|
|
private void OnShutdown(EntityUid uid, JitteringComponent jittering, ComponentShutdown args)
|
|
{
|
|
if (TryComp(uid, out AnimationPlayerComponent? animationPlayer))
|
|
_animationPlayer.Stop(uid, animationPlayer, _jitterAnimationKey);
|
|
|
|
if (TryComp(uid, out SpriteComponent? sprite))
|
|
_sprite.SetOffset((uid, sprite), jittering.StartOffset);
|
|
}
|
|
|
|
private void OnAnimationCompleted(EntityUid uid, JitteringComponent jittering, AnimationCompletedEvent args)
|
|
{
|
|
if (args.Key != _jitterAnimationKey)
|
|
return;
|
|
|
|
if (!args.Finished)
|
|
return;
|
|
|
|
if (TryComp(uid, out AnimationPlayerComponent? animationPlayer)
|
|
&& TryComp(uid, out SpriteComponent? sprite))
|
|
_animationPlayer.Play((uid, animationPlayer), GetAnimation(jittering, sprite), _jitterAnimationKey);
|
|
}
|
|
|
|
private Animation GetAnimation(JitteringComponent jittering, SpriteComponent sprite)
|
|
{
|
|
var amplitude = MathF.Min(4f, jittering.Amplitude / 100f + 1f) / 10f;
|
|
var offset = new Vector2(_random.NextFloat(amplitude / 4f, amplitude),
|
|
_random.NextFloat(amplitude / 4f, amplitude / 3f));
|
|
|
|
offset.X *= _random.Pick(_sign);
|
|
offset.Y *= _random.Pick(_sign);
|
|
|
|
if (Math.Sign(offset.X) == Math.Sign(jittering.LastJitter.X)
|
|
|| Math.Sign(offset.Y) == Math.Sign(jittering.LastJitter.Y))
|
|
{
|
|
// If the sign is the same as last time on both axis we flip one randomly
|
|
// to avoid jitter staying in one quadrant too much.
|
|
if (_random.Prob(0.5f))
|
|
offset.X *= -1;
|
|
else
|
|
offset.Y *= -1;
|
|
}
|
|
|
|
var length = 0f;
|
|
// avoid dividing by 0 so animations don't try to be infinitely long
|
|
if (jittering.Frequency > 0)
|
|
length = 1f / jittering.Frequency;
|
|
|
|
jittering.LastJitter = offset;
|
|
|
|
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(jittering.StartOffset + offset, length),
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
}
|
|
}
|