Re-implement recoil (#9406)

* Re-implement recoil

Playing around with the values atm

* Update constants

* final tweaks
This commit is contained in:
metalgearsloth
2022-07-14 22:01:25 +10:00
committed by GitHub
parent d9e423cebb
commit 38fc066fb9
8 changed files with 76 additions and 52 deletions

View File

@@ -5,36 +5,34 @@ using Robust.Shared.Serialization;
namespace Content.Shared.Camera;
[UsedImplicitly]
public sealed class CameraRecoilSystem : EntitySystem
public abstract class SharedCameraRecoilSystem : EntitySystem
{
/// <summary>
/// Maximum rate of magnitude restore towards 0 kick.
/// </summary>
private const float RestoreRateMax = 15f;
private const float RestoreRateMax = 30f;
/// <summary>
/// Minimum rate of magnitude restore towards 0 kick.
/// </summary>
private const float RestoreRateMin = 1f;
private const float RestoreRateMin = 0.1f;
/// <summary>
/// Time in seconds since the last kick that lerps RestoreRateMin and RestoreRateMax
/// </summary>
private const float RestoreRateRamp = 0.1f;
private const float RestoreRateRamp = 4f;
/// <summary>
/// The maximum magnitude of the kick applied to the camera at any point.
/// </summary>
private const float KickMagnitudeMax = 2f;
protected const float KickMagnitudeMax = 1f;
private readonly ISawmill _log;
private ISawmill _log = default!;
private CameraRecoilSystem(IEntityManager entityManager)
: base(entityManager)
public override void Initialize()
{
_log = Logger.GetSawmill($"ecs.systems.{nameof(CameraRecoilSystem)}");
SubscribeNetworkEvent<CameraKickEvent>(HandleCameraKick);
base.Initialize();
_log = Logger.GetSawmill($"ecs.systems.{nameof(SharedCameraRecoilSystem)}");
}
/// <summary>
@@ -44,15 +42,7 @@ public sealed class CameraRecoilSystem : EntitySystem
/// If the entity is missing <see cref="CameraRecoilComponent" /> and/or <see cref="SharedEyeComponent" />,
/// this call will have no effect. It is safe to call this function on any entity.
/// </remarks>
/// <param name="euid">Entity to apply the kickback to.</param>
/// <param name="kickback">The amount of kick to offset the view of the entity. World coordinates, in meters.</param>
public void KickCamera(EntityUid euid, Vector2 kickback)
{
if (!EntityManager.HasComponent<CameraRecoilComponent>(euid))
return;
RaiseNetworkEvent(new CameraKickEvent(euid, kickback), Filter.Entities(euid));
}
public abstract void KickCamera(EntityUid euid, Vector2 kickback, CameraRecoilComponent? component = null);
public override void FrameUpdate(float frameTime)
{
@@ -85,31 +75,6 @@ public sealed class CameraRecoilSystem : EntitySystem
}
}
}
private void HandleCameraKick(CameraKickEvent args)
{
if (!EntityManager.TryGetComponent(args.Euid, out CameraRecoilComponent? recoil))
{
_log.Warning($"Received a kick for euid {args.Euid}, but it is missing required components.");
return;
}
if (!float.IsFinite(args.Recoil.X) || !float.IsFinite(args.Recoil.Y))
{
_log.Error($"CameraRecoilComponent on entity {recoil.Owner} passed a NaN recoil value. Ignoring.");
return;
}
// Use really bad math to "dampen" kicks when we're already kicked.
var existing = recoil.CurrentKick.Length;
var dampen = existing / KickMagnitudeMax;
recoil.CurrentKick += args.Recoil * (1 - dampen);
if (recoil.CurrentKick.Length > KickMagnitudeMax)
recoil.CurrentKick = recoil.CurrentKick.Normalized * KickMagnitudeMax;
recoil.LastKickTime = 0;
}
}
[Serializable]