Skip to content

Commit

Permalink
Jittering System Fix + Cleanup (space-wizards#20921)
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychpsyo authored Oct 11, 2023
1 parent b53f10f commit 1508f51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
21 changes: 9 additions & 12 deletions Content.Client/Jittering/JitteringSystem.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using System;
using System.Collections.Immutable;
using System.Numerics;
using Content.Shared.Jittering;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
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!;

private readonly float[] _sign = { -1, 1 };
private readonly string _jitterAnimationKey = "jittering";
Expand All @@ -35,13 +30,13 @@ private void OnStartup(EntityUid uid, JitteringComponent jittering, ComponentSta

var animationPlayer = EntityManager.EnsureComponent<AnimationPlayerComponent>(uid);

animationPlayer.Play(GetAnimation(jittering, sprite), _jitterAnimationKey);
_animationPlayer.Play(animationPlayer, GetAnimation(jittering, sprite), _jitterAnimationKey);
}

private void OnShutdown(EntityUid uid, JitteringComponent jittering, ComponentShutdown args)
{
if (EntityManager.TryGetComponent(uid, out AnimationPlayerComponent? animationPlayer))
animationPlayer.Stop(_jitterAnimationKey);
_animationPlayer.Stop(animationPlayer, _jitterAnimationKey);

if (EntityManager.TryGetComponent(uid, out SpriteComponent? sprite))
sprite.Offset = Vector2.Zero;
Expand All @@ -52,9 +47,9 @@ private void OnAnimationCompleted(EntityUid uid, JitteringComponent jittering, A
if(args.Key != _jitterAnimationKey)
return;

if(EntityManager.TryGetComponent(uid, out AnimationPlayerComponent? animationPlayer)
if (EntityManager.TryGetComponent(uid, out AnimationPlayerComponent? animationPlayer)
&& EntityManager.TryGetComponent(uid, out SpriteComponent? sprite))
animationPlayer.Play(GetAnimation(jittering, sprite), _jitterAnimationKey);
_animationPlayer.Play(animationPlayer, GetAnimation(jittering, sprite), _jitterAnimationKey);
}

private Animation GetAnimation(JitteringComponent jittering, SpriteComponent sprite)
Expand All @@ -77,8 +72,10 @@ private Animation GetAnimation(JitteringComponent jittering, SpriteComponent spr
offset.Y *= -1;
}

// Animation length shouldn't be too high so we will cap it at 2 seconds...
var length = Math.Min((1f/jittering.Frequency), 2f);
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;

Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Jittering/SharedJitteringSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void AddJitter(EntityUid uid, float amplitude = 10f, float frequency = 4f
var jitter = EnsureComp<JitteringComponent>(uid);
jitter.Amplitude = amplitude;
jitter.Frequency = frequency;
Dirty(jitter);
Dirty(uid, jitter);
}
}
}

0 comments on commit 1508f51

Please sign in to comment.