Fix eye zoom lerp (#19377)

This commit is contained in:
metalgearsloth
2023-08-25 14:41:04 +10:00
committed by GitHub
parent a8bac1f91f
commit 1ba517ec84
5 changed files with 57 additions and 61 deletions

View File

@@ -1,11 +1,10 @@
using Content.Shared.Follower.Components;
using System.Numerics;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Physics;
using Robust.Client.Player;
using Robust.Shared.Collections;
using Robust.Shared.Timing;
namespace Content.Client.Eye;
@@ -15,9 +14,10 @@ public sealed class EyeLerpingSystem : EntitySystem
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly SharedMoverController _mover = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
// Convenience variable for for VV.
[ViewVariables]
[ViewVariables, UsedImplicitly]
private IEnumerable<LerpingEyeComponent> ActiveEyes => EntityQuery<LerpingEyeComponent>();
public override void Initialize()
@@ -59,8 +59,14 @@ public sealed class EyeLerpingSystem : EntitySystem
lerpInfo.LastRotation = lerpInfo.TargetRotation;
lerpInfo.ManuallyAdded |= !automatic;
lerpInfo.TargetZoom = component.Zoom;
lerpInfo.LastZoom = lerpInfo.TargetZoom;
if (component.Eye != null)
{
component.Eye.Rotation = lerpInfo.TargetRotation;
component.Eye.Zoom = lerpInfo.TargetZoom;
}
}
public void RemoveEye(EntityUid uid)
@@ -107,9 +113,29 @@ public sealed class EyeLerpingSystem : EntitySystem
{
lerpInfo.LastRotation = lerpInfo.TargetRotation;
lerpInfo.TargetRotation = GetRotation(uid, xform);
lerpInfo.LastZoom = lerpInfo.TargetZoom;
lerpInfo.TargetZoom = UpdateZoom(uid, frameTime);
}
}
private Vector2 UpdateZoom(EntityUid uid, float frameTime, EyeComponent? eye = null, ContentEyeComponent? content = null)
{
if (!Resolve(uid, ref content, ref eye, false))
return Vector2.One;
var diff = content.TargetZoom - eye.Zoom;
if (diff.LengthSquared() < 0.00001f)
{
return content.TargetZoom;
}
var change = diff * 8f * frameTime;
return eye.Zoom + change;
}
/// <summary>
/// Does the eye need to lerp or is its rotation matched.
/// </summary>
@@ -138,7 +164,7 @@ public sealed class EyeLerpingSystem : EntitySystem
// if not tied to a mover then lock it to map / grid
var relative = xform.GridUid ?? xform.MapUid;
if (relative != null)
return -Transform(relative.Value).WorldRotation;
return -_transform.GetWorldRotation(relative.Value);
return Angle.Zero;
}
@@ -151,6 +177,19 @@ public sealed class EyeLerpingSystem : EntitySystem
while (query.MoveNext(out var entity, out var lerpInfo, out var eye, out var xform))
{
// Handle zoom
var zoomDiff = Vector2.Lerp(lerpInfo.LastZoom, lerpInfo.TargetZoom, tickFraction);
if ((zoomDiff - lerpInfo.TargetZoom).Length() < lerpMinimum)
{
eye.Zoom = lerpInfo.TargetZoom;
}
else
{
eye.Zoom = zoomDiff;
}
// Handle Rotation
TryComp<InputMoverComponent>(entity, out var mover);
// This needs to be recomputed every frame, as if this is simply the grid rotation, then we need to account for grid angle lerping.