using Content.Shared.StatusEffect;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
namespace Content.Shared.Jittering
{
///
/// A system for applying a jitter animation to any entity.
///
public abstract class SharedJitteringSystem : EntitySystem
{
[Dependency] protected readonly IGameTiming GameTiming = default!;
[Dependency] protected readonly StatusEffectsSystem StatusEffects = default!;
public float MaxAmplitude = 300f;
public float MinAmplitude = 1f;
public float MaxFrequency = 10f;
public float MinFrequency = 1f;
public override void Initialize()
{
SubscribeLocalEvent(OnGetState);
SubscribeLocalEvent(OnHandleState);
}
private void OnGetState(EntityUid uid, JitteringComponent component, ref ComponentGetState args)
{
args.State = new JitteringComponentState(component.Amplitude, component.Frequency);
}
private void OnHandleState(EntityUid uid, JitteringComponent component, ref ComponentHandleState args)
{
if (args.Current is not JitteringComponentState jitteringState)
return;
component.Amplitude = jitteringState.Amplitude;
component.Frequency = jitteringState.Frequency;
}
///
/// Applies a jitter effect to the specified entity.
/// You can apply this to any entity whatsoever, so be careful what you use it on!
///
///
/// If the entity is already jittering, the jitter values will be updated but only if they're greater
/// than the current ones and is false.
///
/// Entity in question.
/// For how much time to apply the effect.
/// The status effect cooldown should be refreshed (true) or accumulated (false).
/// Jitteriness of the animation. See and .
/// Frequency for jittering. See and .
/// Whether to change any existing jitter value even if they're greater than the ones we're setting.
/// The status effects component to modify.
public void DoJitter(EntityUid uid, TimeSpan time, bool refresh, float amplitude = 10f, float frequency = 4f, bool forceValueChange = false,
StatusEffectsComponent? status = null)
{
if (!Resolve(uid, ref status, false))
return;
amplitude = Math.Clamp(amplitude, MinAmplitude, MaxAmplitude);
frequency = Math.Clamp(frequency, MinFrequency, MaxFrequency);
if (StatusEffects.TryAddStatusEffect(uid, "Jitter", time, refresh, status))
{
var jittering = EntityManager.GetComponent(uid);
if(forceValueChange || jittering.Amplitude < amplitude)
jittering.Amplitude = amplitude;
if (forceValueChange || jittering.Frequency < frequency)
jittering.Frequency = frequency;
}
}
}
}