Misc lerping changes (#3472)

This commit is contained in:
Leon Friedrich
2022-11-14 21:42:30 +11:00
committed by GitHub
parent 1068458beb
commit 8bd47cd7f8
8 changed files with 64 additions and 90 deletions
@@ -35,16 +35,33 @@ namespace Robust.Client.GameObjects
// Much faster than iterating 3000+ transforms every frame.
[ViewVariables] private readonly List<TransformComponent> _lerpingTransforms = new();
public override void Initialize()
public void Reset()
{
base.Initialize();
SubscribeLocalEvent<TransformStartLerpMessage>(TransformStartLerpHandler);
foreach (var xform in _lerpingTransforms)
{
xform.ActivelyLerping = false;
xform.NextPosition = null;
xform.NextRotation = null;
xform.LerpParent = EntityUid.Invalid;
}
_lerpingTransforms.Clear();
}
private void TransformStartLerpHandler(TransformStartLerpMessage ev)
public override void ActivateLerp(TransformComponent xform)
{
_lerpingTransforms.Add(ev.Transform);
if (xform.ActivelyLerping)
return;
xform.ActivelyLerping = true;
_lerpingTransforms.Add(xform);
}
public override void DeactivateLerp(TransformComponent component)
{
// this should cause the lerp to do nothing
component.NextPosition = null;
component.NextRotation = null;
component.LerpParent = EntityUid.Invalid;
}
public override void FrameUpdate(float frameTime)
@@ -56,6 +73,8 @@ namespace Robust.Client.GameObjects
for (var i = 0; i < _lerpingTransforms.Count; i++)
{
var transform = _lerpingTransforms[i];
if (transform.Deleted)
continue;
var found = false;
// Only lerp if parent didn't change.
@@ -63,32 +82,32 @@ namespace Robust.Client.GameObjects
if (transform.LerpParent == transform.ParentUid &&
transform.ParentUid.IsValid())
{
if (transform.LerpDestination != null)
if (transform.NextPosition != null)
{
var lerpDest = transform.LerpDestination.Value;
var lerpSource = transform.LerpSource;
var lerpDest = transform.NextPosition.Value;
var lerpSource = transform.PrevPosition;
var distance = (lerpDest - lerpSource).LengthSquared;
if (distance is > MinInterpolationDistanceSquared and < MaxInterpolationDistanceSquared)
{
transform.LocalPosition = Vector2.Lerp(lerpSource, lerpDest, step);
// Setting LocalPosition clears LerpPosition so fix that.
transform.LerpDestination = lerpDest;
transform.NextPosition = lerpDest;
found = true;
}
}
if (transform.LerpAngle != null)
if (transform.NextRotation != null)
{
var lerpDest = transform.LerpAngle.Value;
var lerpSource = transform.LerpSourceAngle;
var lerpDest = transform.NextRotation.Value;
var lerpSource = transform.PrevRotation;
var distance = Math.Abs(Angle.ShortestDistance(lerpDest, lerpSource));
if (distance is > MinInterpolationAngle and < MaxInterpolationAngle)
{
transform.LocalRotation = Angle.Lerp(lerpSource, lerpDest, step);
// Setting LocalRotation clears LerpAngle so fix that.
transform.LerpAngle = lerpDest;
transform.NextRotation = lerpDest;
found = true;
}
}