Mobmover tweaks (#15942)
This commit is contained in:
@@ -83,7 +83,7 @@ namespace Content.Shared.Movement.Systems
|
||||
component.RelativeRotation = state.RelativeRotation;
|
||||
component.TargetRelativeRotation = state.TargetRelativeRotation;
|
||||
component.RelativeEntity = state.RelativeEntity;
|
||||
component.LerpAccumulator = state.LerpAccumulator;
|
||||
component.LerpTarget = state.LerpAccumulator;
|
||||
}
|
||||
|
||||
private void OnInputGetState(EntityUid uid, InputMoverComponent component, ref ComponentGetState args)
|
||||
@@ -94,7 +94,7 @@ namespace Content.Shared.Movement.Systems
|
||||
component.RelativeRotation,
|
||||
component.TargetRelativeRotation,
|
||||
component.RelativeEntity,
|
||||
component.LerpAccumulator);
|
||||
component.LerpTarget);
|
||||
}
|
||||
|
||||
private void ShutdownInput()
|
||||
@@ -121,13 +121,73 @@ namespace Content.Shared.Movement.Systems
|
||||
|
||||
public void ResetCamera(EntityUid uid)
|
||||
{
|
||||
if (CameraRotationLocked || !TryComp<InputMoverComponent>(uid, out var mover) || mover.TargetRelativeRotation.Equals(Angle.Zero))
|
||||
if (CameraRotationLocked ||
|
||||
!TryComp<InputMoverComponent>(uid, out var mover))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// If we updated parent then cancel the accumulator and force it now.
|
||||
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||
|
||||
if (!TryUpdateRelative(mover, xformQuery.GetComponent(uid), xformQuery) && mover.TargetRelativeRotation.Equals(Angle.Zero))
|
||||
return;
|
||||
|
||||
mover.LerpTarget = TimeSpan.Zero;
|
||||
mover.TargetRelativeRotation = Angle.Zero;
|
||||
Dirty(mover);
|
||||
}
|
||||
|
||||
private bool TryUpdateRelative(InputMoverComponent mover, TransformComponent xform, EntityQuery<TransformComponent> xformQuery)
|
||||
{
|
||||
var relative = xform.GridUid;
|
||||
relative ??= xform.MapUid;
|
||||
|
||||
// So essentially what we want:
|
||||
// 1. If we go from grid to map then preserve our rotation and continue as usual
|
||||
// 2. If we go from grid -> grid then (after lerp time) snap to nearest cardinal (probably imperceptible)
|
||||
// 3. If we go from map -> grid then (after lerp time) snap to nearest cardinal
|
||||
|
||||
if (mover.RelativeEntity.Equals(relative))
|
||||
return false;
|
||||
|
||||
// Okay need to get our old relative rotation with respect to our new relative rotation
|
||||
// e.g. if we were right side up on our current grid need to get what that is on our new grid.
|
||||
var currentRotation = Angle.Zero;
|
||||
var targetRotation = Angle.Zero;
|
||||
|
||||
// Get our current relative rotation
|
||||
if (xformQuery.TryGetComponent(mover.RelativeEntity, out var oldRelativeXform))
|
||||
{
|
||||
currentRotation = _transform.GetWorldRotation(oldRelativeXform, xformQuery) + mover.RelativeRotation;
|
||||
}
|
||||
|
||||
if (xformQuery.TryGetComponent(relative, out var relativeXform))
|
||||
{
|
||||
// This is our current rotation relative to our new parent.
|
||||
mover.RelativeRotation = (currentRotation - _transform.GetWorldRotation(relativeXform, xformQuery)).FlipPositive();
|
||||
}
|
||||
|
||||
// If we went from grid -> map we'll preserve our worldrotation
|
||||
if (relative != null && _mapManager.IsMap(relative.Value))
|
||||
{
|
||||
targetRotation = currentRotation.FlipPositive().Reduced();
|
||||
}
|
||||
// If we went from grid -> grid OR grid -> map then snap the target to cardinal and lerp there.
|
||||
// OR just rotate to zero (depending on cvar)
|
||||
else if (relative != null && _mapManager.IsGrid(relative.Value))
|
||||
{
|
||||
if (CameraRotationLocked)
|
||||
targetRotation = Angle.Zero;
|
||||
else
|
||||
targetRotation = mover.RelativeRotation.GetCardinalDir().ToAngle().Reduced();
|
||||
}
|
||||
|
||||
mover.RelativeEntity = relative;
|
||||
mover.TargetRelativeRotation = targetRotation;
|
||||
return true;
|
||||
}
|
||||
|
||||
public Angle GetParentGridAngle(InputMoverComponent mover, EntityQuery<TransformComponent> xformQuery)
|
||||
{
|
||||
var rotation = mover.RelativeRotation;
|
||||
@@ -140,12 +200,7 @@ namespace Content.Shared.Movement.Systems
|
||||
|
||||
public Angle GetParentGridAngle(InputMoverComponent mover)
|
||||
{
|
||||
var rotation = mover.RelativeRotation;
|
||||
|
||||
if (TryComp<TransformComponent>(mover.RelativeEntity, out var relativeXform))
|
||||
return (relativeXform.WorldRotation + rotation);
|
||||
|
||||
return rotation;
|
||||
return GetParentGridAngle(mover, GetEntityQuery<TransformComponent>());
|
||||
}
|
||||
|
||||
private void OnFollowedParentChange(EntityUid uid, FollowedComponent component, ref EntParentChangedMessage args)
|
||||
@@ -185,7 +240,7 @@ namespace Content.Shared.Movement.Systems
|
||||
component.RelativeEntity = relative;
|
||||
component.TargetRelativeRotation = Angle.Zero;
|
||||
component.RelativeRotation = Angle.Zero;
|
||||
component.LerpAccumulator = 0f;
|
||||
component.LerpTarget = TimeSpan.Zero;
|
||||
Dirty(component);
|
||||
return;
|
||||
}
|
||||
@@ -193,16 +248,16 @@ namespace Content.Shared.Movement.Systems
|
||||
// If we go on a grid and back off then just reset the accumulator.
|
||||
if (relative == component.RelativeEntity)
|
||||
{
|
||||
if (component.LerpAccumulator != 0f)
|
||||
if (component.LerpTarget >= Timing.CurTime)
|
||||
{
|
||||
component.LerpAccumulator = 0f;
|
||||
component.LerpTarget = TimeSpan.Zero;
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
component.LerpAccumulator = InputMoverComponent.LerpTime;
|
||||
component.LerpTarget = TimeSpan.FromSeconds(InputMoverComponent.LerpTime) + Timing.CurTime;
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
@@ -233,18 +288,17 @@ namespace Content.Shared.Movement.Systems
|
||||
|
||||
// Relay the fact we had any movement event.
|
||||
// TODO: Ideally we'd do these in a tick instead of out of sim.
|
||||
var owner = moverComp.Owner;
|
||||
var moveEvent = new MoveInputEvent(entity);
|
||||
RaiseLocalEvent(owner, ref moveEvent);
|
||||
RaiseLocalEvent(entity, ref moveEvent);
|
||||
|
||||
// For stuff like "Moving out of locker" or the likes
|
||||
// We'll relay a movement input to the parent.
|
||||
if (_container.IsEntityInContainer(owner) &&
|
||||
TryComp<TransformComponent>(owner, out var xform) &&
|
||||
if (_container.IsEntityInContainer(entity) &&
|
||||
TryComp<TransformComponent>(entity, out var xform) &&
|
||||
xform.ParentUid.IsValid() &&
|
||||
_mobState.IsAlive(owner))
|
||||
_mobState.IsAlive(entity))
|
||||
{
|
||||
var relayMoveEvent = new ContainerRelayMovementEntityEvent(owner);
|
||||
var relayMoveEvent = new ContainerRelayMovementEntityEvent(entity);
|
||||
RaiseLocalEvent(xform.ParentUid, ref relayMoveEvent);
|
||||
}
|
||||
|
||||
@@ -531,16 +585,16 @@ namespace Content.Shared.Movement.Systems
|
||||
/// </summary>
|
||||
public Angle TargetRelativeRotation;
|
||||
public EntityUid? RelativeEntity;
|
||||
public float LerpAccumulator = 0f;
|
||||
public TimeSpan LerpAccumulator;
|
||||
|
||||
public InputMoverComponentState(MoveButtons buttons, bool canMove, Angle relativeRotation, Angle targetRelativeRotation, EntityUid? relativeEntity, float lerpAccumulator)
|
||||
public InputMoverComponentState(MoveButtons buttons, bool canMove, Angle relativeRotation, Angle targetRelativeRotation, EntityUid? relativeEntity, TimeSpan lerpTarget)
|
||||
{
|
||||
Buttons = buttons;
|
||||
CanMove = canMove;
|
||||
RelativeRotation = relativeRotation;
|
||||
TargetRelativeRotation = targetRelativeRotation;
|
||||
RelativeEntity = relativeEntity;
|
||||
LerpAccumulator = lerpAccumulator;
|
||||
LerpAccumulator = lerpTarget;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user