mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Misc lerping changes (#3472)
This commit is contained in:
@@ -7,8 +7,8 @@ public sealed partial class TransformSystem
|
||||
{
|
||||
public override void SetLocalPosition(TransformComponent xform, Vector2 value)
|
||||
{
|
||||
xform._prevPosition = xform._localPosition;
|
||||
xform._nextPosition = value;
|
||||
xform.PrevPosition = xform._localPosition;
|
||||
xform.NextPosition = value;
|
||||
xform.LerpParent = xform.ParentUid;
|
||||
base.SetLocalPosition(xform, value);
|
||||
ActivateLerp(xform);
|
||||
@@ -16,15 +16,15 @@ public sealed partial class TransformSystem
|
||||
|
||||
public override void SetLocalPositionNoLerp(TransformComponent xform, Vector2 value)
|
||||
{
|
||||
xform._nextPosition = null;
|
||||
xform.NextPosition = null;
|
||||
xform.LerpParent = EntityUid.Invalid;
|
||||
base.SetLocalPositionNoLerp(xform, value);
|
||||
}
|
||||
|
||||
public override void SetLocalRotation(TransformComponent xform, Angle angle)
|
||||
{
|
||||
xform._prevRotation = xform._localRotation;
|
||||
xform._nextRotation = angle;
|
||||
xform.PrevRotation = xform._localRotation;
|
||||
xform.NextRotation = angle;
|
||||
xform.LerpParent = xform.ParentUid;
|
||||
base.SetLocalRotation(xform, angle);
|
||||
ActivateLerp(xform);
|
||||
@@ -32,10 +32,10 @@ public sealed partial class TransformSystem
|
||||
|
||||
public override void SetLocalPositionRotation(TransformComponent xform, Vector2 pos, Angle rot)
|
||||
{
|
||||
xform._prevPosition = xform._localPosition;
|
||||
xform._nextPosition = pos;
|
||||
xform._prevRotation = xform._localRotation;
|
||||
xform._nextRotation = rot;
|
||||
xform.PrevPosition = xform._localPosition;
|
||||
xform.NextPosition = pos;
|
||||
xform.PrevRotation = xform._localRotation;
|
||||
xform.NextRotation = rot;
|
||||
xform.LerpParent = xform.ParentUid;
|
||||
base.SetLocalPositionRotation(xform, pos, rot);
|
||||
ActivateLerp(xform);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -455,6 +455,7 @@ namespace Robust.Client.GameStates
|
||||
|
||||
// This is terrible, and I hate it.
|
||||
_entitySystemManager.GetEntitySystem<SharedGridTraversalSystem>().QueuedEvents.Clear();
|
||||
_entitySystemManager.GetEntitySystem<TransformSystem>().Reset();
|
||||
|
||||
foreach (var entity in system.DirtyEntities)
|
||||
{
|
||||
@@ -472,7 +473,7 @@ namespace Robust.Client.GameStates
|
||||
|
||||
var netComps = _entityManager.GetNetComponentsOrNull(entity);
|
||||
if (netComps == null)
|
||||
return;
|
||||
continue;
|
||||
|
||||
foreach (var (netId, comp) in netComps.Value)
|
||||
{
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Robust.Client.GameStates
|
||||
continue;
|
||||
|
||||
// This entity isn't lerping, no need to draw debug info for it
|
||||
if(transform.LerpDestination == null)
|
||||
if(transform.NextPosition == null)
|
||||
continue;
|
||||
|
||||
var aabb = boundingBox.GetWorldAABB();
|
||||
@@ -54,7 +54,7 @@ namespace Robust.Client.GameStates
|
||||
|
||||
timing.InSimulation = true;
|
||||
|
||||
var boxOffset = transform.LerpDestination.Value - transform.LocalPosition;
|
||||
var boxOffset = transform.NextPosition.Value - transform.LocalPosition;
|
||||
var boxPosWorld = transform.WorldPosition + boxOffset;
|
||||
|
||||
timing.InSimulation = false;
|
||||
|
||||
@@ -67,11 +67,17 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
// used for lerping
|
||||
|
||||
internal Vector2? _nextPosition;
|
||||
internal Angle? _nextRotation;
|
||||
[ViewVariables]
|
||||
public Vector2? NextPosition { get; internal set; }
|
||||
|
||||
internal Vector2 _prevPosition;
|
||||
internal Angle _prevRotation;
|
||||
[ViewVariables]
|
||||
public Angle? NextRotation { get; internal set; }
|
||||
|
||||
[ViewVariables]
|
||||
public Vector2 PrevPosition { get; internal set; }
|
||||
|
||||
[ViewVariables]
|
||||
public Angle PrevRotation { get; internal set; }
|
||||
|
||||
// Cache changes so we can distribute them after physics is done (better cache)
|
||||
internal EntityCoordinates? _oldCoords;
|
||||
@@ -419,31 +425,6 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
[ViewVariables] public int ChildCount => _children.Count;
|
||||
|
||||
[ViewVariables]
|
||||
public Vector2? LerpDestination
|
||||
{
|
||||
get => _nextPosition;
|
||||
internal set
|
||||
{
|
||||
_nextPosition = value;
|
||||
ActivelyLerping = true;
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables]
|
||||
internal Angle? LerpAngle
|
||||
{
|
||||
get => _nextRotation;
|
||||
set
|
||||
{
|
||||
_nextRotation = value;
|
||||
ActivelyLerping = true;
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables] internal Vector2 LerpSource => _prevPosition;
|
||||
[ViewVariables] internal Angle LerpSourceAngle => _prevRotation;
|
||||
|
||||
[ViewVariables] internal EntityUid LerpParent { get; set; }
|
||||
|
||||
internal EntityUid? FindGridEntityId(EntityQuery<TransformComponent> xformQuery)
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace Robust.Shared.GameObjects
|
||||
{
|
||||
internal sealed class TransformStartLerpMessage : EntityEventArgs
|
||||
{
|
||||
public TransformStartLerpMessage(TransformComponent transform)
|
||||
{
|
||||
Transform = transform;
|
||||
}
|
||||
|
||||
public TransformComponent Transform { get; }
|
||||
}
|
||||
}
|
||||
@@ -540,15 +540,9 @@ public abstract partial class SharedTransformSystem
|
||||
#endregion
|
||||
|
||||
#region States
|
||||
public virtual void ActivateLerp(TransformComponent xform) { }
|
||||
|
||||
protected void ActivateLerp(TransformComponent xform)
|
||||
{
|
||||
if (xform.ActivelyLerping)
|
||||
return;
|
||||
|
||||
xform.ActivelyLerping = true;
|
||||
RaiseLocalEvent(xform.Owner, new TransformStartLerpMessage(xform), true);
|
||||
}
|
||||
public virtual void DeactivateLerp(TransformComponent xform) { }
|
||||
|
||||
internal void OnGetState(EntityUid uid, TransformComponent component, ref ComponentGetState args)
|
||||
{
|
||||
@@ -615,8 +609,8 @@ public abstract partial class SharedTransformSystem
|
||||
RaiseLocalEvent(xform.Owner, ref ev, true);
|
||||
}
|
||||
|
||||
xform._prevPosition = newState.LocalPosition;
|
||||
xform._prevRotation = newState.Rotation;
|
||||
xform.PrevPosition = newState.LocalPosition;
|
||||
xform.PrevRotation = newState.Rotation;
|
||||
xform._noLocalRotation = newState.NoLocalRotation;
|
||||
|
||||
DebugTools.Assert(xform.ParentUid == newState.ParentID, "Transform state failed to set parent");
|
||||
@@ -625,8 +619,8 @@ public abstract partial class SharedTransformSystem
|
||||
|
||||
if (args.Next is TransformComponentState nextTransform)
|
||||
{
|
||||
xform._nextPosition = nextTransform.LocalPosition;
|
||||
xform._nextRotation = nextTransform.Rotation;
|
||||
xform.NextPosition = nextTransform.LocalPosition;
|
||||
xform.NextRotation = nextTransform.Rotation;
|
||||
xform.LerpParent = nextTransform.ParentID;
|
||||
ActivateLerp(xform);
|
||||
}
|
||||
@@ -636,14 +630,6 @@ public abstract partial class SharedTransformSystem
|
||||
}
|
||||
}
|
||||
|
||||
private void DeactivateLerp(TransformComponent component)
|
||||
{
|
||||
// this should cause the lerp to do nothing
|
||||
component._nextPosition = null;
|
||||
component._nextRotation = null;
|
||||
component.LerpParent = EntityUid.Invalid;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region World Matrix
|
||||
@@ -966,8 +952,8 @@ public abstract partial class SharedTransformSystem
|
||||
_lookup.RemoveFromEntityTree(xform.Owner, xform, xformQuery);
|
||||
|
||||
// Stop any active lerps
|
||||
xform._nextPosition = null;
|
||||
xform._nextRotation = null;
|
||||
xform.NextPosition = null;
|
||||
xform.NextRotation = null;
|
||||
xform.LerpParent = EntityUid.Invalid;
|
||||
|
||||
if (xform.Anchored && metaQuery.TryGetComponent(xform.GridUid, out var meta) && meta.EntityLifeStage <= EntityLifeStage.MapInitialized)
|
||||
|
||||
@@ -101,7 +101,6 @@ public partial class SharedPhysicsSystem
|
||||
// So transform doesn't apply MapId in the HandleComponentState because ??? so MapId can still be 0.
|
||||
// Fucking kill me, please. You have no idea deep the rabbit hole of shitcode goes to make this work.
|
||||
|
||||
Dirty(component);
|
||||
SetLinearVelocity(component, newState.LinearVelocity);
|
||||
SetAngularVelocity(component, newState.AngularVelocity);
|
||||
SetBodyType(component, newState.BodyType);
|
||||
|
||||
Reference in New Issue
Block a user