Smooth docking traversal (#10822)
This commit is contained in:
@@ -40,8 +40,30 @@ namespace Content.Shared.Movement.Components
|
||||
|
||||
public MoveButtons HeldMoveButtons = MoveButtons.None;
|
||||
|
||||
/// <summary>
|
||||
/// Entity our movement is relative to.
|
||||
/// </summary>
|
||||
public EntityUid? RelativeEntity;
|
||||
|
||||
/// <summary>
|
||||
/// Although our movement might be relative to a particular entity we may have an additional relative rotation
|
||||
/// e.g. if we've snapped to a different cardinal direction
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public Angle LastGridAngle { get; set; } = new(0);
|
||||
public Angle TargetRelativeRotation = Angle.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// The current relative rotation. This will lerp towards the <see cref="TargetRelativeRotation"/>.
|
||||
/// </summary>
|
||||
[ViewVariables] public Angle RelativeRotation;
|
||||
|
||||
/// <summary>
|
||||
/// If we traverse on / off a grid then set a timer to update our relative inputs.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float LerpAccumulator;
|
||||
|
||||
public const float LerpTime = 1.0f;
|
||||
|
||||
public bool Sprinting => (HeldMoveButtons & MoveButtons.Walk) == 0x0;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ using Content.Shared.CCVar;
|
||||
using Content.Shared.Input;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Movement.Events;
|
||||
using Content.Shared.Shuttles.Components;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Input;
|
||||
using Robust.Shared.Input.Binding;
|
||||
@@ -17,6 +17,8 @@ namespace Content.Shared.Movement.Systems
|
||||
/// </summary>
|
||||
public abstract partial class SharedMoverController
|
||||
{
|
||||
public bool CameraRotationLocked { get; private set; }
|
||||
|
||||
private void InitializeInput()
|
||||
{
|
||||
var moveUpCmdHandler = new MoverDirInputCmdHandler(this, Direction.North);
|
||||
@@ -30,6 +32,9 @@ namespace Content.Shared.Movement.Systems
|
||||
.Bind(EngineKeyFunctions.MoveRight, moveRightCmdHandler)
|
||||
.Bind(EngineKeyFunctions.MoveDown, moveDownCmdHandler)
|
||||
.Bind(EngineKeyFunctions.Walk, new WalkInputCmdHandler(this))
|
||||
.Bind(EngineKeyFunctions.CameraRotateLeft, new CameraRotateInputCmdHandler(this, Direction.West))
|
||||
.Bind(EngineKeyFunctions.CameraRotateRight, new CameraRotateInputCmdHandler(this, Direction.East))
|
||||
.Bind(EngineKeyFunctions.CameraReset, new CameraResetInputCmdHandler(this))
|
||||
// TODO: Relay
|
||||
// Shuttle
|
||||
.Bind(ContentKeyFunctions.ShuttleStrafeUp, new ShuttleInputCmdHandler(this, ShuttleButtons.StrafeUp))
|
||||
@@ -44,6 +49,14 @@ namespace Content.Shared.Movement.Systems
|
||||
SubscribeLocalEvent<InputMoverComponent, ComponentInit>(OnInputInit);
|
||||
SubscribeLocalEvent<InputMoverComponent, ComponentGetState>(OnInputGetState);
|
||||
SubscribeLocalEvent<InputMoverComponent, ComponentHandleState>(OnInputHandleState);
|
||||
SubscribeLocalEvent<InputMoverComponent, EntParentChangedMessage>(OnInputParentChange);
|
||||
|
||||
_configManager.OnValueChanged(CCVars.CameraRotationLocked, SetCameraRotationLocked, true);
|
||||
}
|
||||
|
||||
private void SetCameraRotationLocked(bool obj)
|
||||
{
|
||||
CameraRotationLocked = obj;
|
||||
}
|
||||
|
||||
private void SetMoveInput(InputMoverComponent component, MoveButtons buttons)
|
||||
@@ -55,27 +68,98 @@ namespace Content.Shared.Movement.Systems
|
||||
|
||||
private void OnInputHandleState(EntityUid uid, InputMoverComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not InputMoverComponentState state) return;
|
||||
if (args.Current is not InputMoverComponentState state)
|
||||
return;
|
||||
|
||||
component.HeldMoveButtons = state.Buttons;
|
||||
component.LastInputTick = GameTick.Zero;
|
||||
component.LastInputSubTick = 0;
|
||||
component.CanMove = state.CanMove;
|
||||
|
||||
component.RelativeRotation = state.RelativeRotation;
|
||||
component.TargetRelativeRotation = state.TargetRelativeRotation;
|
||||
component.RelativeEntity = state.RelativeEntity;
|
||||
component.LerpAccumulator = state.LerpAccumulator;
|
||||
}
|
||||
|
||||
private void OnInputGetState(EntityUid uid, InputMoverComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new InputMoverComponentState(component.HeldMoveButtons, component.CanMove);
|
||||
args.State = new InputMoverComponentState(
|
||||
component.HeldMoveButtons,
|
||||
component.CanMove,
|
||||
component.RelativeRotation,
|
||||
component.TargetRelativeRotation,
|
||||
component.RelativeEntity,
|
||||
component.LerpAccumulator);
|
||||
}
|
||||
|
||||
private void ShutdownInput()
|
||||
{
|
||||
CommandBinds.Unregister<SharedMoverController>();
|
||||
_configManager.UnsubValueChanged(CCVars.CameraRotationLocked, SetCameraRotationLocked);
|
||||
}
|
||||
|
||||
public bool DiagonalMovementEnabled => _configManager.GetCVar(CCVars.GameDiagonalMovement);
|
||||
|
||||
protected virtual void HandleShuttleInput(EntityUid uid, ShuttleButtons button, ushort subTick, bool state) {}
|
||||
|
||||
public void RotateCamera(EntityUid uid, Angle angle)
|
||||
{
|
||||
if (CameraRotationLocked || !TryComp<InputMoverComponent>(uid, out var mover))
|
||||
return;
|
||||
|
||||
mover.TargetRelativeRotation += angle;
|
||||
Dirty(mover);
|
||||
}
|
||||
|
||||
public void ResetCamera(EntityUid uid)
|
||||
{
|
||||
if (CameraRotationLocked || !TryComp<InputMoverComponent>(uid, out var mover) || mover.TargetRelativeRotation.Equals(Angle.Zero))
|
||||
return;
|
||||
|
||||
mover.TargetRelativeRotation = Angle.Zero;
|
||||
Dirty(mover);
|
||||
}
|
||||
|
||||
public Angle GetParentGridAngle(InputMoverComponent mover)
|
||||
{
|
||||
var rotation = mover.RelativeRotation;
|
||||
|
||||
if (TryComp<TransformComponent>(mover.RelativeEntity, out var relativeXform))
|
||||
return (relativeXform.WorldRotation + rotation);
|
||||
|
||||
return rotation;
|
||||
}
|
||||
|
||||
private void OnInputParentChange(EntityUid uid, InputMoverComponent component, ref EntParentChangedMessage args)
|
||||
{
|
||||
// If we change our grid / map then delay updating our LastGridAngle.
|
||||
var relative = args.Transform.GridUid;
|
||||
relative ??= args.Transform.MapUid;
|
||||
|
||||
if (component.LifeStage < ComponentLifeStage.Running)
|
||||
{
|
||||
component.RelativeEntity = relative;
|
||||
Dirty(component);
|
||||
return;
|
||||
}
|
||||
|
||||
// If we go on a grid and back off then just reset the accumulator.
|
||||
if (relative == component.RelativeEntity)
|
||||
{
|
||||
if (component.LerpAccumulator != 0f)
|
||||
{
|
||||
component.LerpAccumulator = 0f;
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
component.LerpAccumulator = InputMoverComponent.LerpTime;
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
private void HandleDirChange(EntityUid entity, Direction dir, ushort subTick, bool state)
|
||||
{
|
||||
// Relayed movement just uses the same keybinds given we're moving the relayed entity
|
||||
@@ -124,9 +208,11 @@ namespace Content.Shared.Movement.Systems
|
||||
{
|
||||
var xform = Transform(uid);
|
||||
|
||||
if (!xform.ParentUid.IsValid()) return;
|
||||
if (!xform.ParentUid.IsValid())
|
||||
return;
|
||||
|
||||
component.LastGridAngle = Transform(xform.ParentUid).WorldRotation;
|
||||
component.RelativeEntity = xform.GridUid ?? xform.MapUid;
|
||||
component.TargetRelativeRotation = Angle.Zero;
|
||||
}
|
||||
|
||||
private void HandleRunChange(EntityUid uid, ushort subTick, bool walking)
|
||||
@@ -300,9 +386,53 @@ namespace Content.Shared.Movement.Systems
|
||||
return (buttons & flag) == flag;
|
||||
}
|
||||
|
||||
private sealed class CameraRotateInputCmdHandler : InputCmdHandler
|
||||
{
|
||||
private readonly SharedMoverController _controller;
|
||||
private readonly Angle _angle;
|
||||
|
||||
public CameraRotateInputCmdHandler(SharedMoverController controller, Direction direction)
|
||||
{
|
||||
_controller = controller;
|
||||
_angle = direction.ToAngle();
|
||||
}
|
||||
|
||||
public override bool HandleCmdMessage(ICommonSession? session, InputCmdMessage message)
|
||||
{
|
||||
if (message is not FullInputCmdMessage full || session?.AttachedEntity == null) return false;
|
||||
|
||||
if (full.State != BoundKeyState.Up)
|
||||
return false;
|
||||
|
||||
_controller.RotateCamera(session.AttachedEntity.Value, _angle);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class CameraResetInputCmdHandler : InputCmdHandler
|
||||
{
|
||||
private readonly SharedMoverController _controller;
|
||||
|
||||
public CameraResetInputCmdHandler(SharedMoverController controller)
|
||||
{
|
||||
_controller = controller;
|
||||
}
|
||||
|
||||
public override bool HandleCmdMessage(ICommonSession? session, InputCmdMessage message)
|
||||
{
|
||||
if (message is not FullInputCmdMessage full || session?.AttachedEntity == null) return false;
|
||||
|
||||
if (full.State != BoundKeyState.Up)
|
||||
return false;
|
||||
|
||||
_controller.ResetCamera(session.AttachedEntity.Value);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class MoverDirInputCmdHandler : InputCmdHandler
|
||||
{
|
||||
private SharedMoverController _controller;
|
||||
private readonly SharedMoverController _controller;
|
||||
private readonly Direction _dir;
|
||||
|
||||
public MoverDirInputCmdHandler(SharedMoverController controller, Direction dir)
|
||||
@@ -344,17 +474,33 @@ namespace Content.Shared.Movement.Systems
|
||||
public MoveButtons Buttons { get; }
|
||||
public readonly bool CanMove;
|
||||
|
||||
public InputMoverComponentState(MoveButtons buttons, bool canMove)
|
||||
/// <summary>
|
||||
/// Our current rotation for movement purposes. This is lerping towards <see cref="TargetRelativeRotation"/>
|
||||
/// </summary>
|
||||
public Angle RelativeRotation;
|
||||
|
||||
/// <summary>
|
||||
/// Target rotation relative to the <see cref="RelativeEntity"/>. Typically 0
|
||||
/// </summary>
|
||||
public Angle TargetRelativeRotation;
|
||||
public EntityUid? RelativeEntity;
|
||||
public float LerpAccumulator = 0f;
|
||||
|
||||
public InputMoverComponentState(MoveButtons buttons, bool canMove, Angle relativeRotation, Angle targetRelativeRotation, EntityUid? relativeEntity, float lerpAccumulator)
|
||||
{
|
||||
Buttons = buttons;
|
||||
CanMove = canMove;
|
||||
RelativeRotation = relativeRotation;
|
||||
TargetRelativeRotation = targetRelativeRotation;
|
||||
RelativeEntity = relativeEntity;
|
||||
LerpAccumulator = lerpAccumulator;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ShuttleInputCmdHandler : InputCmdHandler
|
||||
{
|
||||
private SharedMoverController _controller;
|
||||
private ShuttleButtons _button;
|
||||
private readonly SharedMoverController _controller;
|
||||
private readonly ShuttleButtons _button;
|
||||
|
||||
public ShuttleInputCmdHandler(SharedMoverController controller, ShuttleButtons button)
|
||||
{
|
||||
|
||||
@@ -44,6 +44,8 @@ namespace Content.Shared.Movement.Systems
|
||||
private const float FootstepVolume = 3f;
|
||||
private const float FootstepWalkingAddedVolumeMultiplier = 0f;
|
||||
|
||||
protected ISawmill Sawmill = default!;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.StopSpeed"/>
|
||||
/// </summary>
|
||||
@@ -59,6 +61,7 @@ namespace Content.Shared.Movement.Systems
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
Sawmill = Logger.GetSawmill("mover");
|
||||
InitializeFootsteps();
|
||||
InitializeInput();
|
||||
InitializeMob();
|
||||
@@ -87,14 +90,6 @@ namespace Content.Shared.Movement.Systems
|
||||
UsedMobMovement.Clear();
|
||||
}
|
||||
|
||||
protected Angle GetParentGridAngle(TransformComponent xform, InputMoverComponent mover)
|
||||
{
|
||||
if (!_mapManager.TryGetGrid(xform.GridUid, out var grid))
|
||||
return mover.LastGridAngle;
|
||||
|
||||
return grid.WorldRotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Movement while considering actionblockers, weightlessness, etc.
|
||||
/// </summary>
|
||||
@@ -102,7 +97,8 @@ namespace Content.Shared.Movement.Systems
|
||||
InputMoverComponent mover,
|
||||
PhysicsComponent physicsComponent,
|
||||
TransformComponent xform,
|
||||
float frameTime)
|
||||
float frameTime,
|
||||
EntityQuery<TransformComponent> xformQuery)
|
||||
{
|
||||
DebugTools.Assert(!UsedMobMovement.ContainsKey(mover.Owner));
|
||||
|
||||
@@ -134,12 +130,6 @@ namespace Content.Shared.Movement.Systems
|
||||
if (!touching && TryComp<MobMoverComponent>(xform.Owner, out var mobMover))
|
||||
touching |= IsAroundCollider(PhysicsSystem, xform, mobMover, physicsComponent);
|
||||
}
|
||||
|
||||
if (!touching)
|
||||
{
|
||||
if (xform.GridUid != null)
|
||||
mover.LastGridAngle = GetParentGridAngle(xform, mover);
|
||||
}
|
||||
}
|
||||
|
||||
// Regular movement.
|
||||
@@ -152,7 +142,92 @@ namespace Content.Shared.Movement.Systems
|
||||
|
||||
var total = walkDir * walkSpeed + sprintDir * sprintSpeed;
|
||||
|
||||
var parentRotation = GetParentGridAngle(xform, mover);
|
||||
// Update relative movement
|
||||
if (mover.LerpAccumulator > 0f)
|
||||
{
|
||||
Dirty(mover);
|
||||
mover.LerpAccumulator -= frameTime;
|
||||
|
||||
if (mover.LerpAccumulator <= 0f)
|
||||
{
|
||||
mover.LerpAccumulator = 0f;
|
||||
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))
|
||||
{
|
||||
// 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 = oldRelativeXform.WorldRotation + mover.RelativeRotation;
|
||||
}
|
||||
|
||||
if (xformQuery.TryGetComponent(relative, out var relativeXform))
|
||||
{
|
||||
// This is our current rotation relative to our new parent.
|
||||
mover.RelativeRotation = (currentRotation - relativeXform.WorldRotation).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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var angleDiff = Angle.ShortestDistance(mover.RelativeRotation, mover.TargetRelativeRotation);
|
||||
|
||||
// if we've just traversed then lerp to our target rotation.
|
||||
if (!angleDiff.EqualsApprox(Angle.Zero, 0.005))
|
||||
{
|
||||
var adjustment = angleDiff * 5f * frameTime;
|
||||
var minAdjustment = 0.005 * frameTime;
|
||||
|
||||
if (angleDiff < 0)
|
||||
{
|
||||
adjustment = Math.Min(adjustment, minAdjustment);
|
||||
adjustment = Math.Clamp(adjustment, angleDiff, -angleDiff);
|
||||
}
|
||||
else
|
||||
{
|
||||
adjustment = Math.Max(adjustment, minAdjustment);
|
||||
adjustment = Math.Clamp(adjustment, -angleDiff, angleDiff);
|
||||
}
|
||||
|
||||
mover.RelativeRotation += adjustment;
|
||||
Dirty(mover);
|
||||
}
|
||||
else if (!angleDiff.Equals(Angle.Zero))
|
||||
{
|
||||
mover.RelativeRotation = mover.TargetRelativeRotation;
|
||||
Dirty(mover);
|
||||
}
|
||||
|
||||
var parentRotation = GetParentGridAngle(mover);
|
||||
var worldTotal = _relativeMovement ? parentRotation.RotateVec(total) : total;
|
||||
|
||||
DebugTools.Assert(MathHelper.CloseToPercent(total.Length, worldTotal.Length));
|
||||
@@ -190,17 +265,11 @@ namespace Content.Shared.Movement.Systems
|
||||
var minimumFrictionSpeed = moveSpeedComponent?.MinimumFrictionSpeed ?? MovementSpeedModifierComponent.DefaultMinimumFrictionSpeed;
|
||||
Friction(minimumFrictionSpeed, frameTime, friction, ref velocity);
|
||||
|
||||
if (xform.GridUid != EntityUid.Invalid)
|
||||
mover.LastGridAngle = parentRotation;
|
||||
|
||||
if (worldTotal != Vector2.Zero)
|
||||
{
|
||||
// This should have its event run during island solver soooo
|
||||
xform.DeferUpdates = true;
|
||||
|
||||
xform.LocalRotation = xform.GridUid != null
|
||||
? total.ToWorldAngle()
|
||||
: worldTotal.ToWorldAngle();
|
||||
xform.WorldRotation = worldTotal.ToWorldAngle();
|
||||
xform.DeferUpdates = false;
|
||||
|
||||
if (!weightless && TryComp<MobMoverComponent>(mover.Owner, out var mobMover) &&
|
||||
|
||||
Reference in New Issue
Block a user